Last active
December 15, 2015 15:49
-
-
Save Battleroid/5284817 to your computer and use it in GitHub Desktop.
Polymorphism in C++.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Triangle.h" | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
cout << "Enter three sides: "; | |
double side1, side2, side3; | |
cin >> side1 >> side2 >> side3; | |
Triangle triangle(side1, side2, side3); | |
cout << "Enter the color: "; | |
string color; | |
cin >> color; | |
cout << "Enter 1/0 for filled (1: true, 0: false): "; | |
bool filled; | |
cin >> filled; | |
triangle.setColor(color); | |
triangle.setFilled(filled); | |
cout << "Area is " << triangle.getArea() << endl; | |
cout << "Perimeter is " << triangle.getPerimeter() << endl; | |
cout << "Color is " << triangle.getColor() << endl; | |
cout << "Filled is " << | |
(triangle.isFilled() ? "true" : "false") << endl; | |
system("pause"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "GeometricObject.h" | |
GeometricObject::GeometricObject() { | |
color = "white"; | |
filled = false; | |
} | |
GeometricObject::GeometricObject(const string& color, bool filled) { | |
this->color = color; | |
this->filled = filled; | |
} | |
string GeometricObject::getColor() const { | |
return color; | |
} | |
void GeometricObject::setColor(const string& color) { | |
this->color = color; | |
} | |
bool GeometricObject::isFilled() const { | |
return filled; | |
} | |
void GeometricObject::setFilled(bool filled) { | |
this->filled = filled; | |
} | |
string GeometricObject::toString() const { | |
return "Geometric Object"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef GEOMETRICOBJECT_H | |
#define GEOMETRICOBJECT_H | |
#include <string> | |
using namespace std; | |
class GeometricObject { | |
public: | |
GeometricObject(); | |
GeometricObject(const string& color, bool filled); | |
string getColor() const; | |
void setColor(const string& color); | |
bool isFilled() const; | |
void setFilled(bool filled); | |
string toString() const; | |
private: | |
string color; | |
bool filled; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Triangle.h" | |
Triangle::Triangle() { | |
this->side1 = 1.0; | |
this->side2 = 1.0; | |
this->side3 = 1.0; | |
} | |
Triangle::Triangle(double side1, double side2, double side3) { | |
this->side1 = side1; | |
this->side2 = side2; | |
this->side3 = side3; | |
} | |
double Triangle::getSide1() const { | |
return side1; | |
} | |
double Triangle::getSide2() const { | |
return side2; | |
} | |
double Triangle::getSide3() const { | |
return side3; | |
} | |
double Triangle::getArea() const { | |
// a = sqrt(p(p-a)(p-b)(p-c)) [p = perimeter / 2] | |
double p = (this->getPerimeter()) / 2.0; | |
return sqrt(p * (p - side1) * (p - side2) * (p - side3)); | |
} | |
double Triangle::getPerimeter() const { | |
return side1 + side2 + side3; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TRIANGLE_H | |
#define TRIANGLE_H | |
#include "GeometricObject.h" | |
class Triangle: public GeometricObject { | |
public: | |
Triangle(); | |
Triangle(double side1, double side2, double side3); | |
double getSide1() const; | |
double getSide2() const; | |
double getSide3() const; | |
double getArea() const; | |
double getPerimeter() const; | |
private: | |
double side1, side2, side3; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment