#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;
}
04.15
最新推荐文章于 2024-04-22 16:16:54 发布