How to calculate area of rectangle and kite in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class Shape {
protected:
	float width, height;
public:
	void init(float w, float h)
	{
		width = w; height = h;
	}
};

class Rectangle : public Shape {
public:
	float area()
	{
		return width * height;
	}
};

class kite : public Shape {
public:
	float area()
	{
		return (width * height) / 2;
	}
};


int main()
{
	Rectangle rect;
	kite kt;

	Shape *prect = &rect;
	Shape *pkt = &kt;

	prect->init(7, 3);
	pkt->init(7, 3);

	cout << rect.area() << endl;
	cout << kt.area() << endl;

	return 0;
}

/*
run:

21
10.5

*/

 



answered Feb 16, 2018 by avibootz
edited Feb 17, 2018 by avibootz
...