0% found this document useful (0 votes)
77 views

Inheritance

This document demonstrates the use of polymorphism in C++ by defining several shapes (square, rectangle, triangle, circle, ellipse, polygon) as subclasses of a base shape class. It allows the user to input details for each shape type and calls the appropriate functions to calculate and output the area, perimeter or circumference. The key aspects are defining a base shape class with pure virtual functions for common operations, overriding these functions in each subclass to handle the specific shape type, and using dynamic binding and polymorphism through pointers to call the correct implementation at runtime based on the object type.

Uploaded by

Divya Prasanna
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Inheritance

This document demonstrates the use of polymorphism in C++ by defining several shapes (square, rectangle, triangle, circle, ellipse, polygon) as subclasses of a base shape class. It allows the user to input details for each shape type and calls the appropriate functions to calculate and output the area, perimeter or circumference. The key aspects are defining a base shape class with pure virtual functions for common operations, overriding these functions in each subclass to handle the specific shape type, and using dynamic binding and polymorphism through pointers to call the correct implementation at runtime based on the object type.

Uploaded by

Divya Prasanna
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

NIVETHA.R.S IT-B 1041888 DYNAMIC POLYMORPHISM #include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<typeinfo> #include<process.

h> class point { public: int x,y; void vertex() { cout<<"\nEnter The Co-Ordinates:"; cin>>x>>y; } void display() { cout<<"The co-ordinates are ("<<x<<","<<y<<")"; } }; class shape:public point { public: void virtual get() { } void virtual area() { } void virtual perimeter() { } void virtual circumference() { } }; class square:public shape { float s; public: void get() {

cout<<"\nEnter The Side of Square:"; cin>>s; } void area() { cout<<"\nThe area of Square is "<<s*s<<" sq.units"; } void perimeter() { cout<<"\nThe perimeter of Square is "<<4*s<<" units"; } }; class rectangle:public shape { float l,b; public: void get() { cout<<"\nEnter The Length & Breadth of the Rectangle:"; cin>>l>>b; } void area() { cout<<"\nThe area of Rectangle is "<<l*b<<" sq.units"; } void perimeter() { cout<<"\nThe perimeter of Rectangle is "<<2*(l+b)<<" units"; } }; class triangle:public shape { float l,b,h; public: void get() { cout<<"\nEnter The base of the triangle:"; cin>>b; cout<<"\nEnter The height of the triangle:"; cin>>h; cout<<"\nEnter The side length of the triangle:"; cin>>l; } void area() { cout<<"\nThe area of square is "<<0.5*b*h<<" sq.units";

} void perimeter() { cout<<"\nThe perimeter of square is "<<3*l<<" units"; } }; class circle:public shape { float r; public: void get() { cout<<"\nEnter The Radius of circle:"; cin>>r; } void area() { cout<<"\nThe area of Circle is "<<3.14*r*r<<" sq.units"; } void circumference() { cout<<"\nThe circumference of circle is "<<2*3.14*r<<" units"; } }; class ellipse:public shape { float m,n,p; public: void get() { cout<<"\nEnter The Major and minor axis length of ellipse:"; cin>>m>>n; } void area() { cout<<"\nThe area of Ellipse is "<<3.14*m*n<<" sq.units"; } void circumference() { p=((m+n)*(m-n))/2; cout<<"\nThe circumference of Ellipse is "<<p<<" units"; } }; class polygon:public shape { float l;

int n; public: void get() { cout<<"\nEnter The Length of the Polygon's side:"; cin>>l; cout<<"\nHow many sides does your Polygon has??"; cin>>n; } void perimeter() { cout<<"\nThe perimeter of Polygon is "<<n*l<<" units"; } }; int main() { int op,ch=1,i; shape *sh; square sq; rectangle rec; triangle tri; circle cir; ellipse ell; polygon po; while(ch==1) { cout<<"\n\tMENU\n1.Square \n2.Rectangle \n3.Equilateral Triangle"; cout<<"\n4.Circle \n5.Ellipse \n6.Polygon \nAny other to exit"; cout<<"\nYour Choice:"; cin>>op; switch(op) { case 1: sh=&sq; cout<<"\n Enter the four coordinates"; for(i=0;i<4;i++) { sh->vertex(); sh->display(); } break; case 2: sh=&rec; cout<<"\n Enter the four coordinates"; for(i=0;i<4;i++) {

sh->vertex(); sh->display(); } break; case 3: sh=&tri; cout<<"\n Enter the three coordinates"; for(i=0;i<3;i++) { sh->vertex(); sh->display(); } break; case 4: sh=dynamic_cast<circle*>(&cir); cout<<"\nEnter the centre of the circle:"; sh->vertex(); sh->display(); break; case 5: sh=dynamic_cast<ellipse*>(&ell); cout<<"\nEnter the centre of the ellipse:"; sh->vertex(); sh->display(); break; case 6: sh=&po; break; default: exit(0); break; } sh->get(); sh->area(); if((typeid(*sh)==typeid(cir))||(typeid(*sh)==typeid(ell))) { sh->circumference(); } else sh->perimeter(); cout<<"\n\n Do you want to continue? Press 1 to continue:"; cin>>ch; if(ch!=1) exit(0); } return 0;

} OUTPUT: MENU 1.Square 2.Rectangle 3.Equilateral Triangle 4.Circle 5.Ellipse 6.Polygon Any other to exit Your Choice:1 Enter the four coordinates Enter The Co-Ordinates:2 3 4 5 The co-ordinates are (2,3) Enter The Co-Ordinates:The co-ordinates are (4,5) Enter The Co-Ordinates:2 3 The co-ordinates are (2,3) Enter The Co-Ordinates: 5 6 The co-ordinates are (5,6) Enter The Side of Square: 2 The area of Square is 4 sq.units The perimeter of Square is 8 units Do you want to continue? Press 1 to continue:1 MENU 1.Square 2.Rectangle 3.Equilateral Triangle 4.Circle 5.Ellipse 6.Polygon Any other to exit Your Choice:2 Enter the four coordinates Enter The Co-Ordinates:23 7 The co-ordinates are (23,7) Enter The Co-Ordinates:1 2 The co-ordinates are (1,2) Enter The Co-Ordinates:1 3 The co-ordinates are (1,3) Enter The Co-Ordinates:1

4 The co-ordinates are (1,4) Enter The Length & Breadth of the Rectangle:3 4 The area of Rectangle is 12 sq.units The perimeter of Rectangle is 14 units Do you want to continue? Press 1 to continue:1 MENU 1.Square 2.Rectangle 3.Equilateral Triangle 4.Circle 5.Ellipse 6.Polygon Any other to exit Your Choice:3 Enter the three coordinates Enter The Co-Ordinates:1 2 The co-ordinates are (1,2) Enter The Co-Ordinates:1 3 The co-ordinates are (1,3) Enter The Co-Ordinates:1 4 The co-ordinates are (1,4) Enter The base of the triangle:1 Enter The height of the triangle:5 Enter The side length of the triangle:3 The area of square is 2.5 sq.units The perimeter of square is 9 units Do you want to continue? Press 1 to continue:1 MENU 1.Square 2.Rectangle 3.Equilateral Triangle 4.Circle 5.Ellipse 6.Polygon Any other to exit Your Choice:4 Enter the centre of the circle: Enter The Co-Ordinates:1 2 The co-ordinates are (1,2) Enter The Radius of circle:1

The area of Circle is 3.14 sq.units The circumference of circle is 6.28 units Do you want to continue? Press 1 to continue:1 MENU 1.Square 2.Rectangle 3.Equilateral Triangle 4.Circle 5.Ellipse 6.Polygon Any other to exit Your Choice:5 ]Enter the centre of the ellipse: Enter The Co-Ordinates:1 2 The co-ordinates are (1,2) Enter The Major and minor axis length of ellipse:2 3 The area of Ellipse is 18.84 sq.units The circumference of Ellipse is -2.5 units Do you want to continue? Press 1 to continue:1 MENU 1.Square 2.Rectangle 3.Equilateral Triangle 4.Circle 5.Ellipse 6.Polygon Any other to exit Your Choice:6 Enter The Length of the Polygon's side:5 How many sides does your Polygon has??6 The perimeter of Polygon is 30 units Do you want to continue? Press 1 to continue:

You might also like