Ciaran O'Malley
/
CPSLab4_Area_Perim
Polymorphism technique Shape Classes
Revision 1:911e25976e68, committed 2020-12-05
- Comitter:
- ciaranom
- Date:
- Sat Dec 05 15:48:33 2020 +0000
- Parent:
- 0:5c1338f5f742
- Commit message:
- Oops! (object oriented programming) classes, polymorphism.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 5c1338f5f742 -r 911e25976e68 main.cpp --- a/main.cpp Thu Dec 03 20:59:09 2020 +0000 +++ b/main.cpp Sat Dec 05 15:48:33 2020 +0000 @@ -6,11 +6,11 @@ class Shape //Creating class called Shape { protected: - int width, height, radius; //Creating protcted variables within class Shape + float 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 + Shape (float a=0, float b=0, float c=0, float d=0) //assigning values to protected variables in public domain { width = a; height = b; @@ -31,7 +31,7 @@ class Rectangle: public Shape //Creating class rectangle within the class of shape { public: - Rectangle (int a=0, int b=0):Shape(a,b,0) {} + Rectangle (float a=0, float b=0):Shape(a,b,0) {} int area () { return (width*height); //Calculation for rectangle area @@ -45,21 +45,22 @@ class Triangle: public Shape //Creating class triangle within the class of shape { public: - Triangle(int a=0, int b=0): Shape (a,b,0) {} + Triangle(float a=0, float 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 - // } + int perimeter () + { + return width+(2*(sqrt((width/2)*(width/2)+(height*height)))); //Calculation for Triangle perimeter + // 2 * 2 + 6 * 6 + } }; 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 + Circle (float 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 @@ -75,7 +76,7 @@ { 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 + Triangle tri(8,3); //Assign x and y values for Triangle Circle cir(5); //Assign radius values for Circle @@ -84,11 +85,11 @@ //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 + float Rval = Vshape->area(); //Assigning return value to Rval + printf("Rectangle area is: %.1f\n\r",Rval); //print value Vshape->perimeter(); - int RvalP = Vshape->perimeter(); - printf("Rectangle perimeter is: %d\n\n\r",RvalP); + float RvalP = Vshape->perimeter(); + printf("Rectangle perimeter is: %.1f\n\n\r",RvalP); //Store address if triangle @@ -96,11 +97,11 @@ //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); + float Tval = Vshape->area(); + printf("Triangle area is: %.1f\n\r",Tval); + Vshape->perimeter(); + float TvalP = Vshape->perimeter(); + printf("Triangle perimeter is: %.1f\n\n\r",TvalP); //Store address if Circle @@ -109,11 +110,11 @@ //Call Circle area Vshape->area(); - int Cval = Vshape->area(); - printf("Circle area is: %d\n\r",Cval); + float Cval = Vshape->area(); + printf("Circle area is: %.1f\n\r",Cval); Vshape->perimeter(); - int CvalP = Vshape->perimeter(); - printf("Circle perimeter is: %d\n\n\n\n\r",CvalP); + float CvalP = Vshape->perimeter(); + printf("Circle perimeter is: %.1f\n\n\n\n\r",CvalP); return 0;