04.15

#include <iostream>
#include <cstring>
using namespace std;
class Room;
class WAD//定义WAD类
{
    friend double count(Room &,WAD *,int );//声明友元函数
    //friend class A;//友元类
    double length,width;//数据成员
public://访问权限  公有的
    WAD(double length=0,double width=0);//构造函数 带默认形参值的
    ~WAD(); //析构函数
    double get();//无参的普通成员函数
};
class Room //类声明,类名Room
{   //缺省的访问权限private
    friend double count(Room &,WAD *,int);
    double length,width,height;
public:
    Room(double length,double width,double height);//构造函数
    ~Room();
    double get();
};
WAD::WAD(double length,double width)//成员函数类外实现
{
    this->length=length;//this 指针  ->
    this->width=width;
}
WAD::~WAD() {}
double WAD::get()
{
    return length*width;
}
Room::Room(double length,double width,double height)
{
    this->length=length;
    this->width=width;
    this->height=height;
}
Room::~Room() {}
double Room::get()
{
    double area=length*width+2*(length*height+width*height);
    return area;
}
double count(Room &x,WAD *y,int n)//引用传参 指针传参
{
    int i,sum=0;
    for(i=0; i<n; i++)
        sum+=y[i].get();//指向数组的指针也可以像数组一样
    return x.get()-sum;
}
int main()
{
    double a,b,c,length,width;
    int n;
    cout<<"请输入窗户和门的总数"<<endl;
    cin>>n;
    WAD*p=new WAD[n];//变量定义并初始化
    //定义一个对象指针
    //动态内存申请
    //动态创建对象,动态创建数组
    WAD arr[10];

    for(int i=0; i<n; i++)
    {
        if(length==0&&width==0)
            break;
        cout<<"请输入第"<<i+1<<"个窗户/门的长度:";
        cin>>length;
        cout<<"请输入第"<<i+1<<"个窗户/门的宽度:";
        cin>>width;
        p[i]=WAD(length,width);//匿名对象

    }
    cout<<"请输入房间长度:";
    cin>>a;
    cout<<"请输入房间宽度:";
    cin>>b;
    cout<<"请输入房间高度:";
    cin>>c;
    Room room(a,b,c);
    cout<<"房间需粉刷面积为:"<<count(room,p,n);//将n加在这里
    delete []p;//释放空间
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值