how to merge classes

one class work at a time. how should i select the specific class after detecting the string? the strings should be "S" or "s" for square, "C" or "c" for circle and "r" or "R" for rectangle, the three programs are as follows:

#include <cstdlib>
#include <iostream>

using namespace std;


class rectangle
{
public:

float length;
float breath;
float area;
float perimeter;
};

int main(){
char shape;
cout << "Enter Shape=";
cin >> shape;
rectangle rectangle_shape;

float length, breath;

cout << "Enter length=";
cin >> length;
rectangle_shape.length=length;
cout << "Enter breath=";
cin >> breath;
rectangle_shape.breath=breath;
rectangle_shape.perimeter= 2*rectangle_shape.length+2*rectangle_shape.breath;
rectangle_shape.area=rectangle_shape.length*rectangle_shape.breath;
cout << "Area of rectangle is=" << rectangle_shape.area<<endl;
cout << "Perimeter of rectangle is=" << rectangle_shape.perimeter<<endl;
system("PAUSE");
return 0;
}


SECOND

#include <cstdlib>
#include <iostream>

using namespace std;


class circle
{
public:

float radius;
float area;
float perimeter;
};

int main(){
char shape;
cout << "Enter Shape=";
cin >> shape;
circle circle_shape;

float radius,pi=3.142;

cout << "Enter radius=";
cin >> radius;
circle_shape.radius=radius;
circle_shape.perimeter= 2*pi*circle_shape.radius;
circle_shape.area= 2*pi*circle_shape.radius*circle_shape.radius;
cout << "Area of circle is=" << circle_shape.area<<endl;
cout << "Perimeter of circle is=" << circle_shape.perimeter<<endl;
system("PAUSE");
return 0;
}


Third

#include <cstdlib>
#include <iostream>

using namespace std;


class square
{
public:

float side;
float area;
float perimeter;
};

int main(){
char shape;
cout << "Enter Shape=";
cin >> shape;
square square_shape;

float side;

cout << "Enter side=";
cin >> side;
square_shape.side=side;
square_shape.perimeter= 4*square_shape.side;
square_shape.area= square_shape.side*square_shape.side;
cout << "Area of square is=" << square_shape.area<<endl;
cout << "Perimeter of square is=" << square_shape.perimeter<<endl;
system("PAUSE");
return 0;
}
In my opinion the best approach is to build an hierarchy of classes. The base class will be class Shape with four virtual methods something as input-characteristics, get_area, get_perimeter and ~Shape.
Last edited on
Sorry to say but i still don't get it.... ;(
http://www.cplusplus.com/doc/tutorial/inheritance/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Shape{
   public:
      virtual double getPerimeter()=0; //"=0" makes the function purely virtual, i.e. no definition
        //Will make class "abstract," so you cannot make objects with Shape
      virtual double getArea()=0; //"virtual" tells the compiler to look for the most derived function; requires that derived classes have definitions for virtual functions
                   /*
                         So if Triangle object is called, it will be Triangle::getArea
                         If shape is called (assuming that Shape is not abstract), it will be
                         Shape::getArea
                   */
      virtual void GetNewLength();  //Not purely virtual; requires definition
      double get_side_length()const; //const here denotes read-only function 
                                  //(a guarantee that the object's contents will not be modified)
   private:
      double side_length;
};
class Triangle: public Shape{
   public:
      double getPerimeter();   //An inherited function from Shape that needs to be defined
      double getArea();
      void GetNewLength();
      //double get_side_length()const; this was already inherited from Shape, so no need to declare/define again
   private:
      //double side_length; already inherited from Shape
};
//etc. 


Edit:
Added an example of a non-purely virtual function
Last edited on
Topic archived. No new replies allowed.