叮當(dāng)貓咪
2021-10-06 11:07:15
完整題:定義貓和狗兩個(gè)類,二者都有age屬性,定義二者的一個(gè)友員函數(shù) getTotalAgeO,計(jì)算二者的年齡和,要求給出構(gòu)造函數(shù)和析構(gòu)函數(shù)請(qǐng)快點(diǎn)告訴我哦 ,急用
1 回答

慕斯王
TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
#include<iostream>
using namespace std;
class Cat;
class Dog
{
private:
int Dogwe;
public:
Dog() //無(wú)參數(shù)構(gòu)造函數(shù)
{
Dogwe=8;
}
friend int getTotalAge(Dog &,Cat &);
};
class Cat
{
private:
int Catwe;
public:
Cat( ) //無(wú)參數(shù)構(gòu)造函數(shù)
{
Catwe=2;
}
friend int getTotalAge(Dog &,Cat &);
};
int getTotalAge(Dog &x,Cat &y)
{
return x.Dogwe+y.Catwe;
}
int main()
{
Dog a;
Cat b;
cout<<"總年齡為"<<getTotalAge(a,b)<<endl;
return 0;
}
添加回答
舉報(bào)
0/150
提交
取消