Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Shape.cpp@0:3f07a74c8248, 2019-02-14 (annotated)
- Committer:
- kaushalpkk
- Date:
- Thu Feb 14 12:04:39 2019 +0000
- Revision:
- 0:3f07a74c8248
...
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kaushalpkk | 0:3f07a74c8248 | 1 | // the .cpp file contains the implementation of the shape class |
| kaushalpkk | 0:3f07a74c8248 | 2 | |
| kaushalpkk | 0:3f07a74c8248 | 3 | |
| kaushalpkk | 0:3f07a74c8248 | 4 | #include "mbed.h" |
| kaushalpkk | 0:3f07a74c8248 | 5 | #include "Shape.h" |
| kaushalpkk | 0:3f07a74c8248 | 6 | |
| kaushalpkk | 0:3f07a74c8248 | 7 | //shape constructor with single input parameter |
| kaushalpkk | 0:3f07a74c8248 | 8 | Shape::Shape(int a){ |
| kaushalpkk | 0:3f07a74c8248 | 9 | _a = a; |
| kaushalpkk | 0:3f07a74c8248 | 10 | _b = -1; |
| kaushalpkk | 0:3f07a74c8248 | 11 | } |
| kaushalpkk | 0:3f07a74c8248 | 12 | |
| kaushalpkk | 0:3f07a74c8248 | 13 | |
| kaushalpkk | 0:3f07a74c8248 | 14 | //shape constructor with 2 input parameters, |
| kaushalpkk | 0:3f07a74c8248 | 15 | //NOTE: the constructors do have a return type like the ones seen below |
| kaushalpkk | 0:3f07a74c8248 | 16 | Shape::Shape(int a, int b){ |
| kaushalpkk | 0:3f07a74c8248 | 17 | _a = a; |
| kaushalpkk | 0:3f07a74c8248 | 18 | _b = b; |
| kaushalpkk | 0:3f07a74c8248 | 19 | } |
| kaushalpkk | 0:3f07a74c8248 | 20 | |
| kaushalpkk | 0:3f07a74c8248 | 21 | //calculates area and returns and int type result |
| kaushalpkk | 0:3f07a74c8248 | 22 | int Shape::getArea(){ |
| kaushalpkk | 0:3f07a74c8248 | 23 | //calculate area |
| kaushalpkk | 0:3f07a74c8248 | 24 | |
| kaushalpkk | 0:3f07a74c8248 | 25 | int res = 0; //initialize variable to store result |
| kaushalpkk | 0:3f07a74c8248 | 26 | if(_b == -1){ //check if _b is -1 |
| kaushalpkk | 0:3f07a74c8248 | 27 | res = _a * _a; |
| kaushalpkk | 0:3f07a74c8248 | 28 | }else{ |
| kaushalpkk | 0:3f07a74c8248 | 29 | res = _a * _b; |
| kaushalpkk | 0:3f07a74c8248 | 30 | } |
| kaushalpkk | 0:3f07a74c8248 | 31 | return res; |
| kaushalpkk | 0:3f07a74c8248 | 32 | } |
| kaushalpkk | 0:3f07a74c8248 | 33 | |
| kaushalpkk | 0:3f07a74c8248 | 34 | |
| kaushalpkk | 0:3f07a74c8248 | 35 | //calculates perimeter and returns and int type result |
| kaushalpkk | 0:3f07a74c8248 | 36 | int Shape::getPerim(){ |
| kaushalpkk | 0:3f07a74c8248 | 37 | //calculate perimeter |
| kaushalpkk | 0:3f07a74c8248 | 38 | int res = 0; |
| kaushalpkk | 0:3f07a74c8248 | 39 | if(_b == -1){ //check if _b is -1 |
| kaushalpkk | 0:3f07a74c8248 | 40 | res = 4 * _a; |
| kaushalpkk | 0:3f07a74c8248 | 41 | }else{ |
| kaushalpkk | 0:3f07a74c8248 | 42 | res = 2*(_a + _b); |
| kaushalpkk | 0:3f07a74c8248 | 43 | } |
| kaushalpkk | 0:3f07a74c8248 | 44 | return res; |
| kaushalpkk | 0:3f07a74c8248 | 45 | } |
| kaushalpkk | 0:3f07a74c8248 | 46 | |
| kaushalpkk | 0:3f07a74c8248 | 47 | |
| kaushalpkk | 0:3f07a74c8248 | 48 | //prints information and does not return anything 'void' |
| kaushalpkk | 0:3f07a74c8248 | 49 | void Shape::printInfo(){ |
| kaushalpkk | 0:3f07a74c8248 | 50 | //print information |
| kaushalpkk | 0:3f07a74c8248 | 51 | if(_b == -1){ //check if _b is -1 |
| kaushalpkk | 0:3f07a74c8248 | 52 | printf("*** this is a square !\n"); |
| kaushalpkk | 0:3f07a74c8248 | 53 | }else{ |
| kaushalpkk | 0:3f07a74c8248 | 54 | printf("*** this is a rectangle!\n"); |
| kaushalpkk | 0:3f07a74c8248 | 55 | } |
| kaushalpkk | 0:3f07a74c8248 | 56 | printf(" Area : %d \n", getArea()); |
| kaushalpkk | 0:3f07a74c8248 | 57 | printf(" Perimeter : %d \n", getPerim()); |
| kaushalpkk | 0:3f07a74c8248 | 58 | } |
| kaushalpkk | 0:3f07a74c8248 | 59 |