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
Diff: Shape.cpp
- Revision:
- 0:3f07a74c8248
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Shape.cpp Thu Feb 14 12:04:39 2019 +0000
@@ -0,0 +1,59 @@
+// the .cpp file contains the implementation of the shape class
+
+
+#include "mbed.h"
+#include "Shape.h"
+
+//shape constructor with single input parameter
+Shape::Shape(int a){
+ _a = a;
+ _b = -1;
+}
+
+
+//shape constructor with 2 input parameters,
+//NOTE: the constructors do have a return type like the ones seen below
+Shape::Shape(int a, int b){
+ _a = a;
+ _b = b;
+}
+
+//calculates area and returns and int type result
+int Shape::getArea(){
+ //calculate area
+
+ int res = 0; //initialize variable to store result
+ if(_b == -1){ //check if _b is -1
+ res = _a * _a;
+ }else{
+ res = _a * _b;
+ }
+ return res;
+}
+
+
+//calculates perimeter and returns and int type result
+int Shape::getPerim(){
+ //calculate perimeter
+ int res = 0;
+ if(_b == -1){ //check if _b is -1
+ res = 4 * _a;
+ }else{
+ res = 2*(_a + _b);
+ }
+ return res;
+}
+
+
+//prints information and does not return anything 'void'
+void Shape::printInfo(){
+ //print information
+ if(_b == -1){ //check if _b is -1
+ printf("*** this is a square !\n");
+ }else{
+ printf("*** this is a rectangle!\n");
+ }
+ printf(" Area : %d \n", getArea());
+ printf(" Perimeter : %d \n", getPerim());
+}
+