Ciaran O'Malley
/
CPSLab4_Area_Perim
Polymorphism technique Shape Classes
main.cpp@0:5c1338f5f742, 2020-12-03 (annotated)
- Committer:
- ciaranom
- Date:
- Thu Dec 03 20:59:09 2020 +0000
- Revision:
- 0:5c1338f5f742
- Child:
- 1:911e25976e68
CPS Lab 4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ciaranom | 0:5c1338f5f742 | 1 | #include "mbed.h" |
ciaranom | 0:5c1338f5f742 | 2 | //#include "C12832.h" |
ciaranom | 0:5c1338f5f742 | 3 | #include <iostream> |
ciaranom | 0:5c1338f5f742 | 4 | #include <math.h> |
ciaranom | 0:5c1338f5f742 | 5 | |
ciaranom | 0:5c1338f5f742 | 6 | class Shape //Creating class called Shape |
ciaranom | 0:5c1338f5f742 | 7 | { |
ciaranom | 0:5c1338f5f742 | 8 | protected: |
ciaranom | 0:5c1338f5f742 | 9 | int width, height, radius; //Creating protcted variables within class Shape |
ciaranom | 0:5c1338f5f742 | 10 | |
ciaranom | 0:5c1338f5f742 | 11 | |
ciaranom | 0:5c1338f5f742 | 12 | public: |
ciaranom | 0:5c1338f5f742 | 13 | Shape (int a=0, int b=0, int c=0, int d=0) //assigning values to protected variables in public domain |
ciaranom | 0:5c1338f5f742 | 14 | { |
ciaranom | 0:5c1338f5f742 | 15 | width = a; |
ciaranom | 0:5c1338f5f742 | 16 | height = b; |
ciaranom | 0:5c1338f5f742 | 17 | radius = c; |
ciaranom | 0:5c1338f5f742 | 18 | |
ciaranom | 0:5c1338f5f742 | 19 | } |
ciaranom | 0:5c1338f5f742 | 20 | |
ciaranom | 0:5c1338f5f742 | 21 | 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 |
ciaranom | 0:5c1338f5f742 | 22 | { |
ciaranom | 0:5c1338f5f742 | 23 | return 0; |
ciaranom | 0:5c1338f5f742 | 24 | } |
ciaranom | 0:5c1338f5f742 | 25 | virtual int perimeter () //Virtual funtions perimeter |
ciaranom | 0:5c1338f5f742 | 26 | { |
ciaranom | 0:5c1338f5f742 | 27 | return 0; |
ciaranom | 0:5c1338f5f742 | 28 | } |
ciaranom | 0:5c1338f5f742 | 29 | }; |
ciaranom | 0:5c1338f5f742 | 30 | |
ciaranom | 0:5c1338f5f742 | 31 | class Rectangle: public Shape //Creating class rectangle within the class of shape |
ciaranom | 0:5c1338f5f742 | 32 | { |
ciaranom | 0:5c1338f5f742 | 33 | public: |
ciaranom | 0:5c1338f5f742 | 34 | Rectangle (int a=0, int b=0):Shape(a,b,0) {} |
ciaranom | 0:5c1338f5f742 | 35 | int area () |
ciaranom | 0:5c1338f5f742 | 36 | { |
ciaranom | 0:5c1338f5f742 | 37 | return (width*height); //Calculation for rectangle area |
ciaranom | 0:5c1338f5f742 | 38 | } |
ciaranom | 0:5c1338f5f742 | 39 | int perimeter () |
ciaranom | 0:5c1338f5f742 | 40 | { |
ciaranom | 0:5c1338f5f742 | 41 | return ((width+height)*2); //Calculation for rectangle perimeter |
ciaranom | 0:5c1338f5f742 | 42 | } |
ciaranom | 0:5c1338f5f742 | 43 | }; |
ciaranom | 0:5c1338f5f742 | 44 | |
ciaranom | 0:5c1338f5f742 | 45 | class Triangle: public Shape //Creating class triangle within the class of shape |
ciaranom | 0:5c1338f5f742 | 46 | { |
ciaranom | 0:5c1338f5f742 | 47 | public: |
ciaranom | 0:5c1338f5f742 | 48 | Triangle(int a=0, int b=0): Shape (a,b,0) {} |
ciaranom | 0:5c1338f5f742 | 49 | int area () |
ciaranom | 0:5c1338f5f742 | 50 | { |
ciaranom | 0:5c1338f5f742 | 51 | return (width*height/2); //Calculation for Triangle area |
ciaranom | 0:5c1338f5f742 | 52 | } |
ciaranom | 0:5c1338f5f742 | 53 | //int perimeter () |
ciaranom | 0:5c1338f5f742 | 54 | // { |
ciaranom | 0:5c1338f5f742 | 55 | // return (width+(2*(sqrt((width/2)^2)+(height^2))); //Calculation for Triangle perimeter |
ciaranom | 0:5c1338f5f742 | 56 | // } |
ciaranom | 0:5c1338f5f742 | 57 | }; |
ciaranom | 0:5c1338f5f742 | 58 | |
ciaranom | 0:5c1338f5f742 | 59 | class Circle: public Shape //Creating class circle within the class of shape |
ciaranom | 0:5c1338f5f742 | 60 | { |
ciaranom | 0:5c1338f5f742 | 61 | public: |
ciaranom | 0:5c1338f5f742 | 62 | Circle (int c=0): Shape (0,0,c) {} // c is the third calue in the constructor |
ciaranom | 0:5c1338f5f742 | 63 | int area () |
ciaranom | 0:5c1338f5f742 | 64 | { |
ciaranom | 0:5c1338f5f742 | 65 | return ((radius)*2*3.14); //Calculation for circle area |
ciaranom | 0:5c1338f5f742 | 66 | } |
ciaranom | 0:5c1338f5f742 | 67 | int perimeter () |
ciaranom | 0:5c1338f5f742 | 68 | { |
ciaranom | 0:5c1338f5f742 | 69 | return (3.14*radius*radius); //Calculation for rectangle perimeter |
ciaranom | 0:5c1338f5f742 | 70 | } |
ciaranom | 0:5c1338f5f742 | 71 | }; |
ciaranom | 0:5c1338f5f742 | 72 | |
ciaranom | 0:5c1338f5f742 | 73 | // Main function |
ciaranom | 0:5c1338f5f742 | 74 | int main () |
ciaranom | 0:5c1338f5f742 | 75 | { |
ciaranom | 0:5c1338f5f742 | 76 | Shape *Vshape; //Vshape is the pointer towards Shape. shape is how we will draw from the class shape |
ciaranom | 0:5c1338f5f742 | 77 | Rectangle rec(3,4); //Assign x and y values for Rectangle |
ciaranom | 0:5c1338f5f742 | 78 | Triangle tri(4,6); //Assign x and y values for Triangle |
ciaranom | 0:5c1338f5f742 | 79 | Circle cir(5); //Assign radius values for Circle |
ciaranom | 0:5c1338f5f742 | 80 | |
ciaranom | 0:5c1338f5f742 | 81 | |
ciaranom | 0:5c1338f5f742 | 82 | //Store address if rectangle |
ciaranom | 0:5c1338f5f742 | 83 | Vshape = &rec; |
ciaranom | 0:5c1338f5f742 | 84 | |
ciaranom | 0:5c1338f5f742 | 85 | //Call rectangle and print area |
ciaranom | 0:5c1338f5f742 | 86 | Vshape->area(); //Calling Cshapes area function |
ciaranom | 0:5c1338f5f742 | 87 | int Rval = Vshape->area(); //Assigning return value to Rval |
ciaranom | 0:5c1338f5f742 | 88 | printf("Rectangle area is: %d\n\r",Rval); //print value |
ciaranom | 0:5c1338f5f742 | 89 | Vshape->perimeter(); |
ciaranom | 0:5c1338f5f742 | 90 | int RvalP = Vshape->perimeter(); |
ciaranom | 0:5c1338f5f742 | 91 | printf("Rectangle perimeter is: %d\n\n\r",RvalP); |
ciaranom | 0:5c1338f5f742 | 92 | |
ciaranom | 0:5c1338f5f742 | 93 | |
ciaranom | 0:5c1338f5f742 | 94 | //Store address if triangle |
ciaranom | 0:5c1338f5f742 | 95 | Vshape = &tri; |
ciaranom | 0:5c1338f5f742 | 96 | |
ciaranom | 0:5c1338f5f742 | 97 | //Call triangle and print area |
ciaranom | 0:5c1338f5f742 | 98 | Vshape->area(); |
ciaranom | 0:5c1338f5f742 | 99 | int Tval = Vshape->area(); |
ciaranom | 0:5c1338f5f742 | 100 | printf("Triangle area is: %d\n\n\r",Tval); |
ciaranom | 0:5c1338f5f742 | 101 | //Vshape->perimeter(); |
ciaranom | 0:5c1338f5f742 | 102 | //int TvalP = Vshape->perimeter(); |
ciaranom | 0:5c1338f5f742 | 103 | //printf("Triangle area is: %d\n\n\r",TvalP); |
ciaranom | 0:5c1338f5f742 | 104 | |
ciaranom | 0:5c1338f5f742 | 105 | |
ciaranom | 0:5c1338f5f742 | 106 | //Store address if Circle |
ciaranom | 0:5c1338f5f742 | 107 | Vshape = ○ |
ciaranom | 0:5c1338f5f742 | 108 | |
ciaranom | 0:5c1338f5f742 | 109 | |
ciaranom | 0:5c1338f5f742 | 110 | //Call Circle area |
ciaranom | 0:5c1338f5f742 | 111 | Vshape->area(); |
ciaranom | 0:5c1338f5f742 | 112 | int Cval = Vshape->area(); |
ciaranom | 0:5c1338f5f742 | 113 | printf("Circle area is: %d\n\r",Cval); |
ciaranom | 0:5c1338f5f742 | 114 | Vshape->perimeter(); |
ciaranom | 0:5c1338f5f742 | 115 | int CvalP = Vshape->perimeter(); |
ciaranom | 0:5c1338f5f742 | 116 | printf("Circle perimeter is: %d\n\n\n\n\r",CvalP); |
ciaranom | 0:5c1338f5f742 | 117 | |
ciaranom | 0:5c1338f5f742 | 118 | return 0; |
ciaranom | 0:5c1338f5f742 | 119 | |
ciaranom | 0:5c1338f5f742 | 120 | } |