C++中的友元关系

#include<iostream>
#include<string>
class Lovers{
public:
Lovers(std::string name){
this->name=name;
}
void kiss(Lovers*lovers);
void ask(Lovers *lovers,std::string something);
protected:
std::string name;
friend class Others;
};
class Boyfriend:public Lovers{
public:
Boyfriend(std::string name);
};
class Girfriend:public Lovers{
public :
Girfriend(std::string name);
};
class Others{
public :
Others(std::string name){
this->name=name;
}
void kiss(Lovers*lovers);
protected:
std::string name;
};
void Lovers::kiss(Lovers*lovers){
std::cout<<name<<"亲亲我们家的"<<lovers->name<<std::endl;
}
void Lovers::ask(Lovers *lovers,std::string something){
std::cout<<"宝贝儿"<<lovers->name<<"帮我"<<something<<std::endl;
}
Boyfriend::Boyfriend(std::string name):Lovers(name){

}
Girfriend::Girfriend(std::string name):Lovers(name){

}
void Others::kiss(Lovers *lovers){
std::cout<<name<<"亲一下"<<lovers->name<<std::endl;
}
int main(){
Boyfriend boyfriend("A君");
Girfriend x("B妞");
Others others("路人甲");
boyfriend.kiss(&boyfriend);
x.ask(&boyfriend,"洗衣服啦");
std::cout<<"\n当当当!传说中的路人甲登场。。。。。。。";
others.kiss(&x);
return 0;

}

上一篇:右值引用


下一篇:算法训练 纪念品分组