Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////
- //shape.h:
- ///////////////////
- #ifndef SHAPE_H
- #define SHAPE_H
- #include <string>
- #include <cmath>
- #include "part2.h"
- using namespace std;
- #define PI 4.0 * atan(1.0) //This line enables you to use PI in your calculations
- class Shape {
- private:
- string color;
- string line;
- public:
- //Shape(){}
- Shape(string color, string line); //Constructor
- void setColor(string color); //Used to change the color
- void setLine(string line); //Used to change the line type
- string getColor(); //Returns the color
- string getLine(); //Returns the line type
- string toString(); //Returns a string that descibes the shape in this format "Color = blue, Line = dotted"
- };
- #endif
- /*---------------------------*/
- ///////////////////
- //shape.cpp:
- ///////////////////
- #include <string>
- #include <sstream>
- #include "shape.h"
- using namespace std;
- Shape::Shape(string color, string line){
- this->color = color;
- this->line = line;
- }
- void Shape::setColor(string color){
- this->color = color;
- }
- string Shape::getColor(){
- return color;
- }
- void Shape::setLine(string line){
- this->line = line;
- }
- string Shape::getLine(){
- return line;
- }
- string Shape::toString(){
- stringstream ss;
- ss << "Color = " << color << ", Line = " << line;
- return ss.str();
- }
- /*---------------------------*/
- ///////////////////
- //part2.h:
- ///////////////////
- #include "shape.h"
- using namespace std;
- class Circle : public Shape
- {
- private:
- double radius;
- public:
- //Circle() : Shape(){ }
- Circle(string color, string line, double radius) : Shape(color, line) , radius(radius) { }
- void setRadius(double radius)
- {
- if(radius<0)
- this->radius=0;
- else
- this->radius=radius;
- }
- double getRadius(){return radius;}
- double getCirc(){return 2*PI*radius;}
- double getArea(){return radius*radius*PI;}
- };
- /*---------------------------*/
- ///////////////////
- //mainfunc.cpp:
- ///////////////////
- #include "part1.h"
- #include "shape.h"
- #include "part2.h"
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- // Inheritance problem
- //Circle rund("red", "dotted", 2);
- Circle* rund = new Circle("red", "dotted", 2);
- cout << "\nFeatures of the circle using the Circle object's get-funcs:" << endl;
- cout << "Color: " << rund->getColor() << ", Line: " << rund->getLine();
- cout << ", Radius: " << rund->getRadius() << endl;
- cout << "Features of the circle using the Shape class' toString-func:" << endl;
- cout << rund->toString() << endl;
- delete rund;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment