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
Revision 0:3f07a74c8248, committed 2019-02-14
- Comitter:
- kaushalpkk
- Date:
- Thu Feb 14 12:04:39 2019 +0000
- Commit message:
- ...
Changed in this revision
--- /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());
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Shape.h Thu Feb 14 12:04:39 2019 +0000
@@ -0,0 +1,22 @@
+
+/*
+the header file consists of the declaration of the shape class and
+declaration of function and variables in the class
+
+*/
+
+
+class Shape{ //class declaration
+ public: // the content underneath the public are accessible outside the class
+ //constructors
+ Shape(int);
+ Shape(int, int);
+
+ //functions
+ int getArea();
+ int getPerim();
+ void printInfo();
+
+ private: //the private variables are not accessible outside the class.
+ int _a, _b; // a personal choice to have the variable names start with an '_'
+};
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Feb 14 12:04:39 2019 +0000
@@ -0,0 +1,10 @@
+#include "mbed.h"
+#include "Shape.h"
+
+Shape squr(6); //create a squr object with a single input parameter
+Shape rect(2,9); //create a rect object with 2 input parameters
+
+int main() {
+ squr.printInfo(); //print information on squr
+ rect.printInfo(); ///print information on squr
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Feb 14 12:04:39 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc \ No newline at end of file