编写图形类(基类),该类有周长、面积等属性和求周长、求面积等成员函数。编写图形类的派生类-----矩形类
编写图形类(基类),该类有周长、面积等属性和求周长、求面积等成员函数。编写图形类的派生类-----矩形类,在该类中给出求矩形长宽比的成员函数。在主函数中进行对象的定义,实...
编写图形类(基类),该类有周长、面积等属性和求周长、求面积等成员函数。编写图形类的派生类-----矩形类,在该类中给出求矩形长宽比的成员函数。在主函数中进行对象的定义,实现相关信息的输入和输出
展开
1个回答
展开全部
#include <iostream>
using namespace std;
class shape
{
public:
float p;
float a;
public:
float GetP(){};
float GetA(){};
};
class rect : public shape
{
public:
float l;
float w;
float p;
float a;
public:
void Set(float a, float b){ l=a, w=b; };
float GetP(){ return 2*(l+w); };
float GetA(){ return l*w; };
float GetR(){ return l/w; };
};
void main()
{
rect rc;
float l, w, p, a, r;
rc.Set(5.9f, 4.3f);
l = rc.l;
w = rc.w;
p = rc.GetP();
a = rc.GetA();
r = rc.GetR();
cout << "长:" << l << endl;
cout << "宽:" << w << endl;
cout << "周长:" << p << endl;
cout << "面积:" << a << endl;
cout << "长宽比:" << r << endl;
}
using namespace std;
class shape
{
public:
float p;
float a;
public:
float GetP(){};
float GetA(){};
};
class rect : public shape
{
public:
float l;
float w;
float p;
float a;
public:
void Set(float a, float b){ l=a, w=b; };
float GetP(){ return 2*(l+w); };
float GetA(){ return l*w; };
float GetR(){ return l/w; };
};
void main()
{
rect rc;
float l, w, p, a, r;
rc.Set(5.9f, 4.3f);
l = rc.l;
w = rc.w;
p = rc.GetP();
a = rc.GetA();
r = rc.GetR();
cout << "长:" << l << endl;
cout << "宽:" << w << endl;
cout << "周长:" << p << endl;
cout << "面积:" << a << endl;
cout << "长宽比:" << r << endl;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询