Let\'s define several classes for geometrical figures: %u2022 Square %u2022 Circ
ID: 3540168 • Letter: L
Question
Let's define several classes for geometrical figures:
%u2022 Square
%u2022 Circle
%u2022 Rectangle
%u2022 Oval
Assuming, that we do not need rotation, it is possible to define square and circle using
center of object and only one property - radius for circle and side length for square. This
way, it is possible to define rectangle and oval with position and two properties(width
and height).
So, in our case, each class has position property. Two classes has one property and two
other objects has two properties. Let's try to define these classes using inheritance in
C++/Java:
%u2022 Let's define class "BasePoint" which will have position properties (x, y)
o Let's define class "Figure1P" which will have one property geometrical
figure (p1)
%uDBC0%uDC83 Class Square is based on this class (has x, y and p1)
%uDBC0%uDC83 Class Circle is also based on this class (has x, y and p1)
%uDBC0%uDC83 Class Figure2P extends this class with one more property (p2)
%uDBC0%uDC83 Class Rectangle is based on Figure2P (has x, y, p1, p2)
%uDBC0%uDC83 Class Oval is also based on Figure2P (has x, y, p1, p2)
Explanation / Answer
#include<iostream>
using namespace std;
class basept
{
protected:
int x, y;
};
class fig1p:public basept
{
protected: int p1;
public:
void insert()
{
cout<<"Enter X coord : ";
cin>>x;
cout<<"Enter Y coord : ";
cin>>y;
cout<<" Enter the value of P1 : ";
cin>>p1;
}
};
class fig2p:public fig1p
{
protected: int p2;
public:
void insert()
{
cout<<"Enter X coord : ";
cin>>x;
cout<<"Enter Y coord : ";
cin>>y;
cout<<" Enter the value of P1 : ";
cin>>p1;
cout<<" Enter the value of P2 : ";
cin>>p2;
}
};
class square:public fig1p
{
public:
void display()
{
cout<<" SNo. COORD";
cout<<" --- -------";
cout<<" 1. ("<<x<<","<<y<<")";
cout<<" 2. ("<<x+p1<<","<<y<<")";
cout<<" 3. ("<<x+p1<<","<<y+p1<<")";
cout<<" 4. ("<<x<<","<<y+p1<<")";
}
};
square s;
class circle:public fig1p
{
public:
void display()
{
cout<<" The CIRCLE has RADIUS "<<p1<<" and has CENTRE at ("<<x<<","<<y<<")";
}
};
circle ci;
class rectangle:public fig2p
{
public:
void display()
{
cout<<" SNo. COORD";
cout<<" --- -------";
cout<<" 1. ("<<x<<","<<y<<")";
cout<<" 2. ("<<x+p2<<","<<y<<")";
cout<<" 3. ("<<x+p2<<","<<y+p1<<")";
cout<<" 4. ("<<x<<","<<y+p1<<")";
}
};
rectangle r;
class oval:public fig2p
{
public:
void display()
{
cout<<" The OVAL has CENTRE at ("<<x<<","<<y<<") WIDTH "<<p2<<" HEIGHT "<<p1;
}
};
oval o;
int main()
{
int ch;
char c='y';
while(c=='y')
{
//b.insert();
cout<<"1.Square 2.Circle 3.Rectangle 4.Oval Enter your choice -> ";
cin>>ch;
switch(ch)
{
case 1: s.insert();
s.display();
break;
case 2: ci.insert();
ci.display();
break;
case 3: r.insert();
//f2.insert();
r.display();
break;
case 4: o.insert();
//f2.insert();
o.display();
break;
default:cout<<" Wrong choice entered....";
}
cout<<" Do you want to continue(y/n)? ";
cin>>c;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.