![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Polymorphism technique Shape Classes
Diff: main.cpp
- Revision:
- 0:5c1338f5f742
- Child:
- 1:911e25976e68
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Dec 03 20:59:09 2020 +0000 @@ -0,0 +1,120 @@ +#include "mbed.h" +//#include "C12832.h" +#include <iostream> +#include <math.h> + +class Shape //Creating class called Shape +{ + protected: + int width, height, radius; //Creating protcted variables within class Shape + + + public: + Shape (int a=0, int b=0, int c=0, int d=0) //assigning values to protected variables in public domain + { + width = a; + height = b; + radius = c; + + } + + virtual int area () // "virtual int.." used as "int.." was setting the variable here and therefore retuning shape areas = 0. The virtual prefix (function?) essential says that int area will be used by another class + { + return 0; + } + virtual int perimeter () //Virtual funtions perimeter + { + return 0; + } +}; + +class Rectangle: public Shape //Creating class rectangle within the class of shape + { + public: + Rectangle (int a=0, int b=0):Shape(a,b,0) {} + int area () + { + return (width*height); //Calculation for rectangle area + } + int perimeter () + { + return ((width+height)*2); //Calculation for rectangle perimeter + } + }; + +class Triangle: public Shape //Creating class triangle within the class of shape + { + public: + Triangle(int a=0, int b=0): Shape (a,b,0) {} + int area () + { + return (width*height/2); //Calculation for Triangle area + } + //int perimeter () + // { + // return (width+(2*(sqrt((width/2)^2)+(height^2))); //Calculation for Triangle perimeter + // } + }; + +class Circle: public Shape //Creating class circle within the class of shape + { + public: + Circle (int c=0): Shape (0,0,c) {} // c is the third calue in the constructor + int area () + { + return ((radius)*2*3.14); //Calculation for circle area + } + int perimeter () + { + return (3.14*radius*radius); //Calculation for rectangle perimeter + } + }; + +// Main function +int main () +{ + Shape *Vshape; //Vshape is the pointer towards Shape. shape is how we will draw from the class shape + Rectangle rec(3,4); //Assign x and y values for Rectangle + Triangle tri(4,6); //Assign x and y values for Triangle + Circle cir(5); //Assign radius values for Circle + + + //Store address if rectangle + Vshape = &rec; + + //Call rectangle and print area + Vshape->area(); //Calling Cshapes area function + int Rval = Vshape->area(); //Assigning return value to Rval + printf("Rectangle area is: %d\n\r",Rval); //print value + Vshape->perimeter(); + int RvalP = Vshape->perimeter(); + printf("Rectangle perimeter is: %d\n\n\r",RvalP); + + + //Store address if triangle + Vshape = &tri; + + //Call triangle and print area + Vshape->area(); + int Tval = Vshape->area(); + printf("Triangle area is: %d\n\n\r",Tval); + //Vshape->perimeter(); + //int TvalP = Vshape->perimeter(); + //printf("Triangle area is: %d\n\n\r",TvalP); + + + //Store address if Circle + Vshape = ○ + + + //Call Circle area + Vshape->area(); + int Cval = Vshape->area(); + printf("Circle area is: %d\n\r",Cval); + Vshape->perimeter(); + int CvalP = Vshape->perimeter(); + printf("Circle perimeter is: %d\n\n\n\n\r",CvalP); + + return 0; + +}