Home > Software engineering >  3. CHouse class of operators> overloading, realize from the flow of housing information. 4. CBuil
3. CHouse class of operators> overloading, realize from the flow of housing information. 4. CBuil

Time:09-17

# include
# include
# include
The class CHouse
{
Private:
Char mName [20].//address
Float mPrice;//unit price
Float mArea.//area
Public:
Float getprice ()
{
Return mPrice;
}
Float getarea ()
{
Return mArea.
}
Void the set (char * name, float price, float area);//modify housing parameter
CHouse (char * name=0, float price=0, float area=0).//the constructor
Friend ifstream& Operator> (ifstream& Is and CHouse & amp; House);//read from the file a house parameter
};
Void CHouse: : set (char * name, float price, float area)
{
Strcpy (mName, name);
MPrice=price;
MArea=area;
}
CHouse: : CHouse (char * name=0, float price=0, float area=0)
{
If (the name!=NULL) strcpy (mName, name);
MPrice=price;
MArea=area;
}
Ifstream& Operator> (ifstream& Is and CHouse & amp; House)
{
Is> House. MName> House. MPrice> House.
MArea.
The return is;
}
The class CBuilding//building
{
Private:
CHouse mHouse [20].
Int mNums;
Public:
CBuilding ()
{
MNums=0;
}
Float Total ();//calculate the total value of all home building
Void AddHouse (char * name, float price, float area);//add to the building to the building
Void ReadData (char * filename);//the building information read from the file
};
Float CBuilding: : Total ()
{
Float n;
for(int i=0; iN +=mHouse [I]. Getprice ();
return n;
}
Void CBuilding: : AddHouse (char * name, float price, float area)
{
MHouse [mNums]=CHouse (name, price, area);
MNums++;
}
Void CBuilding: : ReadData (char * filename)
{
Char buff [20].
Ifstream infile (filename);
if(! Infile)
{
Cout<& lt;" Cannot open input file: "& lt; exit(1);
}
int i=0;
While (infile. Getline (buff, 20)) {
If (I==0) cin> MNums;
The else {
Cin> MHouse [mNums];
i++;
MNums++; }}
Infile. Close ();
}
Void main ()
{
CBuilding Building;
Building. ReadData (" data. TXT ");
Building. AddHouse (" 507 ", 23000);
Cout<& lt;" Total housing: "& lt; }

CodePudding user response:

For bosses to give directions rookie

CodePudding user response:

https://docs.microsoft.com/zh-cn/previous-versions/00hh13h0%28v%3dvs.110%29


CodePudding user response:

refer to the second floor zgl7903 response:
https://docs.microsoft.com/zh-cn/previous-versions/00hh13h0%28v%3dvs.110%29

Can you help me to modify it, and see how to write, you are not derived as this