| #include<iostream.h> class Line { protected : float length; public : void setLength(float len) { length=len;}; float getLength () {return length;}; void showLength(){ cout<<"length is "<<length<<"\n";}; virtual void showDim (){ cout<<"Length is "<<getLength()<<"\n\n";}; }; class Rectangle : public Line{ protected : float height; public: void setHeight(float h ){height =h;}; float getHeight(){ return height;}; void showHeight(){cout<<"Heigth is "<<height; }; float rArea() { return height*length;}; virtual void showDim(){ cout<<"\nLength is "<<getLength(); cout<<"\nheight is "<<getHeight()<<"\n"; }; }; class Cube : public Rectangle{ protected : float depth; public : void setDepth(float d){ depth = d;}; float getDepth(){ return depth;}; void showDepth(){ cout<<"\nDepth is "<<depth;}; float volume () { return (depth *rArea());}; virtual void showDim() { showLength(); showHeight(); showDepth(); }; }; void main(){ cout<<" Define 3 classes Line , Rectangle and Cube that inherits from each other"; Line len ; Rectangle rec; Cube cub; cout<<"\n\nLine data\n"; len.setLength(1.5); len.showDim(); cout<<"Rectangle data"; rec.setLength(1); rec.setHeight(2); rec.showDim(); cout<<"\nCube data\n"; cub.setLength(3); cub.setHeight(4); cub.setDepth(5); cub.showDim(); } |