1. Write a C++ program declare a class "polygon" having data members width and
a member function. Calculate area of triangle and rectangle using pointer to
derived class object.
Answers
Hey friend!!
Check the attachment given above
Answer:
Given below is the answer
Explanation:
#include <iostream>
using namespace std;
class Polygon {
public:
Polygon(int, int);
protected:
int height;
int width;
};
class Rectangle: public Polygon {
public:
void calc_area();
};
class Triangle: public Polygon {
public:
void calc_area();
};
Polygon::Polygon(int a, int b) {
height = a;
width = b;
}
void Rectangle::calc_area() {
cout << "Rectangle area: " << (height*width) << endl;
}
void Triangle::calc_area() {
cout << "Triangle area: " << (height*width/2) << endl;
}
int main() {
Rectangle s1(5, 2);
Triangle s2(5, 2);
s1.calc_area();
s2.calc_area();
}
I am trying to make a Polygon class and a Rectangle and Triangle that inherit the first. Polygon class has height and width variables that I want them to be be given values within the constructor. Then, Rectangle and Triangle have area calculation methods. Then, I use a main() to give some examples.
But while everything looks ok to my newbie eyes, I get a series of errors:
12 base Polygon' with only non-default constructor in class without a constructor `
36 no matching function for call to `Rectangle::Rectangle(int, int)
37 no matching function for call to `Triangle::Triangle(int, int)'
Can someone give me some tips? As seen, I am very new to C++
See more:
https://brainly.in/question/3082331
#SPJ3