Guest User

ARVEfest

a guest
May 7th, 2010
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. ///////////////////
  2. //shape.h:
  3. ///////////////////
  4.  
  5. #ifndef SHAPE_H
  6. #define SHAPE_H
  7.  
  8. #include <string>
  9. #include <cmath>
  10. #include "part2.h"
  11.  
  12. using namespace std;
  13.  
  14. #define PI 4.0 * atan(1.0)  //This line enables you to use PI in your calculations
  15.  
  16. class Shape {
  17. private:
  18.     string color;
  19.     string line;
  20. public:
  21.     //Shape(){}
  22.     Shape(string color, string line);   //Constructor
  23.     void setColor(string color);        //Used to change the color
  24.     void setLine(string line);          //Used to change the line type
  25.     string getColor();              //Returns the color
  26.     string getLine();               //Returns the line type
  27.     string toString();              //Returns a string that descibes the shape in this format "Color = blue, Line = dotted"
  28. };
  29.  
  30. #endif
  31.  
  32. /*---------------------------*/
  33.  
  34.  
  35. ///////////////////
  36. //shape.cpp:
  37. ///////////////////
  38. #include <string>
  39. #include <sstream>
  40.  
  41. #include "shape.h"
  42.  
  43. using namespace std;
  44.  
  45.  
  46. Shape::Shape(string color, string line){
  47.     this->color = color;
  48.     this->line = line;
  49. }
  50.  
  51. void Shape::setColor(string color){
  52.     this->color = color;
  53. }
  54.  
  55. string Shape::getColor(){
  56.     return color;
  57. }
  58.  
  59. void Shape::setLine(string line){
  60.     this->line = line;
  61. }
  62.  
  63. string Shape::getLine(){
  64.     return line;
  65. }
  66.  
  67. string Shape::toString(){
  68.     stringstream ss;
  69.     ss << "Color = " << color << ", Line = " << line;
  70.     return ss.str();
  71. }
  72.  
  73. /*---------------------------*/
  74.  
  75.  
  76. ///////////////////
  77. //part2.h:
  78. ///////////////////
  79.  
  80. #include "shape.h"
  81.  
  82. using namespace std;
  83.  
  84. class Circle : public Shape
  85. {
  86. private:
  87.     double radius;
  88. public:
  89.     //Circle() : Shape(){ }
  90.     Circle(string color, string line, double radius) : Shape(color, line)   , radius(radius) { }
  91.     void setRadius(double radius)
  92.     {
  93.         if(radius<0)
  94.             this->radius=0;
  95.         else
  96.             this->radius=radius;
  97.     }
  98.     double getRadius(){return radius;}
  99.     double getCirc(){return 2*PI*radius;}
  100.     double getArea(){return radius*radius*PI;}
  101. };
  102.  
  103. /*---------------------------*/
  104.  
  105.  
  106. ///////////////////
  107. //mainfunc.cpp:
  108. ///////////////////
  109.  
  110. #include "part1.h"
  111. #include "shape.h"
  112. #include "part2.h"
  113. #include <iostream>
  114. #include <string>
  115.  
  116. using namespace std;
  117.    
  118. int main()
  119. {
  120.     // Inheritance problem
  121.  
  122.     //Circle rund("red", "dotted", 2);
  123.     Circle* rund = new Circle("red", "dotted", 2);
  124.    
  125.     cout << "\nFeatures of the circle using the Circle object's get-funcs:" << endl;
  126.     cout << "Color: " << rund->getColor() << ", Line: " << rund->getLine();
  127.     cout << ", Radius: " << rund->getRadius() << endl;
  128.     cout << "Features of the circle using the Shape class' toString-func:" << endl;
  129.     cout << rund->toString() << endl;
  130.  
  131.     delete rund;
  132.  
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment