友元关系
代码编译不过!#include <iostream>
#include <string>
class Lovers
{
public:
Lovers(std::string theName);
void kiss(Lovers *lover);
void ask(Lovers *lover, std::string something);
protected:
std::string name;
friend class Others;
};
class Boyfriend : public Lovers
{
public:
Boyfriend(std::string theName);
};
class Girlfriend : public Lovers
{
public:
Girlfriend(std::string theName);
};
class Others
{
public:
Others(std::string theName);
void kiss(Lovers *lover);
protected:
std::string name;
};
Lovers::Lovers(std::string theName)
{
name = theName;
}
void Lovers::kiss(Lovers *lover)
{
std::cout << name << "亲亲我们家的" << lover->name << std::endl;
}
void Lovers::ask(Lovers *lover, std::string something)
{
std::cout << "宝贝儿" << lover->name << "帮我" << something << std::endl;
}
Boyfriend::Boyfriend(std::string theName)
{
}
Girlfriend::Girlfriend(std::string theName)
{
}
Others::Others(std::string theName)
{
name = theName;
}
void Others::kiss(Lovers *lover)
{
std::cout << name << "亲一下" << lover->name << std::endl;
}
int main()
{
Boyfriend boyfriend("A君");
Girlfriend girlfriend("B妞");
Others others("路人甲");
girlfriend.kiss(&boyfriend);
girlfriend.ask(&boyfriend, "洗衣服啦~");
std::cout << "\n当当当当! 传说中的路人甲登场......\n";
others.kiss(&girlfriend);
return 0;
}
本帖最后由 Krant5 于 2016-7-14 23:38 编辑
你的两个构造器有问题,boyfriend 类构造器后面应该跟 :Lovers(theName),同理Grilfriend类构造器后面也一样,子类构造器也要初始化父类 Krant5 发表于 2016-7-14 23:37
你的两个构造器有问题,boyfriend 类构造器后面应该跟 :Lovers(theName),同理Grilfriend类构造器后面也一 ...
soo~~ ga~~万分感谢!
的确Boyfriend和Girlfriend的构造器都是继承自Lovers,忘了这个语法,哈哈{:10_277:}
页:
[1]