Question #207465

Create a class GeometricShape including

• A pure virtual function named show() is create in GeometricShape class

Derive a class Rectangle from class GeometricShape including

• data members for length and width of rectangle,

• respective mutator and accessor functions to set and get values of length and width.

• overriding function computeArea() to compute area of rectangle (length x width)

• overriding function show() to display details associated with instance of class Rectangle.

From Rectangle class, derive a class Cuboid containing

• a data field height in addition to length and width,

• two functions setHeight and getHeight() to set and get value of height data field.

• overriding function computeArea() to calculate the area of a cuboid (length x width x height)

• overriding function show() to display the details associated with an instance of class Cuboid.

In main function, create instance of derived classes to access respective show() function using dynamic binding.


Expert's answer

#include <iostream>
#include <string>
using namespace std;


class GeometricShape{
public:
	GeometricShape(){}
	//A pure virtual function named show() is create in GeometricShape class
	virtual void show()=0;
};
class Rectangle : public GeometricShape{
private:
	//data members for length and width of rectangle,
	float length,width;
public:
	Rectangle(){}
	//respective mutator and accessor functions to set and get values of length and width.
	void setLength(float length){
		this->length=length;
	}
	void setWidth(float width){
		this->width=width;
	}


	float getLength(){
		return this->length;
	}
	float getWidth(){
		return this->width;
	}
	//overriding function computeArea() to compute area of rectangle (length x width)
	float computeArea(){
		return this->length*this->width;
	}
	//overriding function show() to display details associated with instance of class Rectangle.
	void show(){
		cout<<"Rectangle length: "<<this->length<<"\n";
		cout<<"Rectangle width: "<<this->width<<"\n";
		cout<<"Rectangle area: "<<this->computeArea()<<"\n";
	}
};
class Cuboid: public Rectangle{
private:
	//a data field height in addition to length and width,
	float height;
public:
	Cuboid(){}
	// two functions setHeight and getHeight() to set and get value of height data field.
	void setHeight(float height){
		this->height=height;
	}




	float getHeight(){
		return this->height;
	}
	// overriding function computeArea() to calculate the area of a cuboid (length x width x height)
	float computeArea(){
		return Rectangle::computeArea()*this->height;
	}
	//overriding function show() to display the details associated with an instance of class Cuboid.
	void show(){
		cout<<"Cuboid length: "<<this->getLength()<<"\n";
		cout<<"Cuboid width: "<<this->getWidth()<<"\n";
		cout<<"Cuboid height: "<<this->height<<"\n";
		cout<<"Cuboid area: "<<this->computeArea()<<"\n";
		
	}
};
int main(){
	GeometricShape *geometricShape;
	Rectangle rectangle;
	rectangle.setLength(5);
	rectangle.setWidth(4);
	geometricShape = &rectangle;
	geometricShape->show();
	cout<<"\n";
	Cuboid cuboid;
	cuboid.setLength(3);
	cuboid.setWidth(2);
	cuboid.setHeight(5);
	geometricShape = &cuboid;
	geometricShape->show();






	system("pause");
	return 0;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS