用C++语言“定义并实现一个矩形类,有长、宽两个属性,用成员函数计算矩形的面积”。(编出程序拿分)

来源:百度知道 编辑:UC知道 时间:2024/09/20 22:28:39
只要求用C++语言实现这个小程序

我是试试哈
#include "iostream"

using namespace std;

class rectangle//定义矩形类
{
public:
rectangle(float longth,float weight)//构造函数
{
itslongth=longth;itsweight=weight;
}
float getArea(){return itslongth*itsweight;};//计算矩形面积
private:
float itslongth,itsweight;//定义长宽属性
};

int main()
{
rectangle rec1(5,10);//定义矩形类的对象并初始化
cout<<"这个矩形的面积为"<<rec1.getArea()<<endl;
//调用矩形类的成员函数计算面积并输出

return 0;
}

#include<iostream>
using namespace std;
class graph
{
protected:
float high,wide;
public:
graph();
graph(float h,float w)
{
high=h;wide=w;cout<<"高为:"<<h<<"\t宽为:"<<w<<endl;}};

class retangle:public graph
{
public:
retangle(float h,float w):graph(h,w){}