Answer to Question #214636 in C++ for shayan abbasi

Question #214636
Write a program having Classes Rectangle with data members’ length and width, Square with data member side ,EquilatralTrainglewith data member side . All these classes are derived from class Shape. Write an independent function which will exhibit polymorphism and show the area of the object passed to the function. All data members of class are private; you can write suitable functions needed for you.
1
Expert's answer
2021-07-07T18:24:21-0400
#include<iostream>
#include<math.h>
using namespace std;
class Shape{
	public:
		void Area(){
			cout<<"The area of the shape must be calculated"<<endl;
		}
};
class Rectangle: public Shape{
	private:
		int length, width;
	public:
		Rectangle(int l, int w){
			length = l;
			width = w;
		}
		Area(){
			cout<<"The area of the rectangle is \t"<<length * width;
		}
};
class Triangle: public Shape{
	private:
		int side;
	public:
		Triangle(int n){
			side = n;
		}
		//Equilateral all angles are 60. Therefore, we use 0.5*side* side * sin 60
		Area(){
			cout<<"The area of the triangle is\t"<<0.5 * side * side * (sqrt(3) /2) <<endl;
		}
};
int main(){
	Triangle t(8);
	t.Area();
	
	Rectangle rec(6,7);
	rec.Area();
}

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS