友元类

#include<iostream>
using namespace std;
class Boat{
public:
Boat(int Weight){
weight1=Weight;
}
Boat(Boat &p){
weight1=p.weight1;
}
friend class Car;
private:
int weight1;
};
class Car{
public:
Car(int Weight,Boat p){
weight1=p.weight1;
weight2=Weight;
}
int getTotalWeight(){
return weight1+weight2;
}
private:
int weight1,weight2;
};
int main(){
Boat myboat(100);
Car mycar(200,myboat);
cout<<"The total weight is "<<mycar.getTotalWeight()<<endl;
return 0;
}

 

你可能感兴趣的:(C++,c,友元类)