Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- აბსტრაქტული კლასი Shape შეიცავს ორ წმინდა ვირტუალურ ფუნქციას : printName, area; Shape კლასის საფუძველზე აგებულია
- კონკრეტული კლასები Circle და Rectangle. ყოველ კონკრეტულ კლასში printName ფუნქციის დანიშნულებაა კონკრეტული
- გეომეტრიული ფიგურის სახელის ბეჭდვა, ხოლო area-სი კიდევ ფართობის გამოთვლა. დაწერეთ პროგრამა,
- რომელიც მოახდენს კლასების მოცემული იერარქიის ტესტირებას.
- ა) შექმნის Circle კლასის ობიექტს რადიუსით 5 და გამოიყენებს მისთვის printName და area ფუნქციებს.
- ბ) დინამიკურად შექმნის სხვადასხვა ფიგურის რამდენიმე ობიექტს და Shape კლასზე პოინტერების მასივის გამოყენებით
- დაბეჭდავს ყველა ფიგურის დასახელებას და ფართობს.
- */
- #include<iostream>
- using namespace std;
- class Shape{
- public:
- Shape(){}
- ~Shape(){}
- virtual void printName() = 0;
- virtual double area() = 0;
- };
- class Circle :public Shape{
- private:
- double radius;
- public:
- Circle(){ radius = 5; }
- ~Circle(){}
- Circle(double r){ radius = r; }
- virtual void printName(){
- cout << "I am circle " ;
- }
- virtual double area(){
- return 2.14159*radius*radius;
- }
- };
- class Rectangle :public Shape{
- private :
- double x, y;
- public:
- Rectangle(){ x = 0; y = 0; }
- ~Rectangle(){}
- Rectangle(double xx, double yy){
- x = xx; y = yy;
- }
- virtual void printName(){
- cout << "I am rectangle ";
- }
- virtual double area(){
- return x*y;
- }
- };
- int main(){
- Circle* c = new Circle(5);
- cout << "Print info about circle " << endl;
- c->printName();
- cout << "Area = " << c->area() << endl;
- Circle* c1 = new Circle(7.7);
- Rectangle* r1 = new Rectangle(4, 9.3);
- Rectangle* r2 = new Rectangle(1.4, 3.3);
- Shape* shapes[3];
- shapes[0] = r1;
- shapes[1] = r2;
- shapes[2] = c1;
- for (int i = 0; i < 3; i++){
- shapes[i]->printName();
- cout << "Area = " << shapes[i]->area() << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement