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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include<iostream>
#include<string>
using namespace std;
class Shape{
float density;
string name;
public:
Shape(string title, float n)
{
name = title;
density = n;
}
virtual float dimension()=0;
string getName(void){return name;}
float getDensity(void){return density;}
virtual string getColor()=0;
};
class Rectangle:public Shape{
public:
float length;
float width;
string rectColor;
Rectangle(float le, float wi, string co, float de, string tye)
:Shape(tye, de)
{
length = le;
width = wi;
rectColor = co;
}
string getColor(void)
{
string r;
r = rectColor;
return r;
}
float dimension(void)
{
float t;
t=length*width;
return t;
}
};
class Cube: public Rectangle{
float height;
public:
Cube(float len, float wid, float hei, string colo, float dens, string typ)
:Rectangle(len,wid,colo,dens,typ)
{
height = hei;
}
float dimension(void)
{
float k;
k = height*width*length;
return k;
}
};
void find_Mass(Shape *Sobj_ref)
{
cout<<"Name : " <<Sobj_ref->getName()<<endl;
cout<<"Color : "<<Sobj_ref->getColor()<<endl;
cout<<"Dimension : " <<Sobj_ref->dimension()<<endl;
cout<<"Density : " <<Sobj_ref->getDensity()<<endl;
}
int main()
{
Shape*n[4];
n[0]=new Rectangle(30,40,"Blue",0.34,"Rectangle A");
n[1]=new Cube(10,20,5,"Green",1.5,"Cube 1");
n[2]=new Rectangle(70,9,"Yellow",2.1,"Rectangle B");
n[3]=new Cube(33,5,15,"Black",0.5,"Cube 2");
for (int i=0; i<=3; i++)
find_Mass(n[i]);
system("PAUSE");
}
|