Hello I need help in accessing and displaying the results of the three classes derived from the base class. They are private and I'm having difficulty in doing so. I have searched and tried on my own from what I understand about set and get but it's not working.
#include<iostream>
using namespace std;
class Shape
{
private:
double height, width;
double radius;
public:
void set_data(int i)
{
height, width, radius = i;
}
void get_data()
{
cin>>height>>width>>radius;
}
virtual void display_area()=0;
};
class Triangle: public Shape
{
public:
void display_area()
{
cout<<"\nArea of triangle = "<<(height*width)/2;
};
};
class Rectangle : public Shape
{
public:
void display_area()
{
cout<<"\nArea of rectangle = "<<height*width;
}
};
class Circle: public Shape
{
public:
void display_area()
{
cout<<"\nArea of circle = "<<3.14*radius*radius;
}
};
int main()
{
Triangle t;
Shape *st = &t;
Rectangle r;
Shape *sr = &r;
Circle c;
Shape *sc = &c;
cout<<"Enter height and width: ";
t.set_data(2,6);
r.set_data(9,10);
c.set_data(3,6,4);
cout<<t.get_data()<<r.get_data()<<c.get_data();
return 0;
}
t.set_data(2,6);
r.set_data(9,10);
c.set_data(3,6,4);
cout<<t.get_data()<<r.get_data()<<c.get_data();
I understand to access a private class within a public one will bring errors so I tried the whole set and get but I think I'm still doing it wrong and I need some guidance/assistance.