您的位置:首页 > 其它

对象成员及初始化列表的使用demo

2017-09-09 11:40 176 查看
demo2_man.h

#include<string>
using namespace std;
class Man
{
public:
Man(string game);
~Man();
void like();
private:
string Man_game;
};

demo2_man.cpp
#include<iostream>
#include "demo2_man.h"
using namespace std;
Man::Man(string game)
{
Man_game=game;
cout<<"Man(string game)"<<endl;
}
Man::~Man()
{
cout<<"~Man()"<<endl;
}

void Man::like()
{
cout<<"man always like playing "+Man_game<<endl;
}

demo2.cpp
#include<iostream>
#include<string>
#include "demo2_man.h";
using namespace std;

class Human
{
public:
Human(string hat,string clothes,string game);
~Human();
public:
string Human_hat;
string Human_clothes;
Man man;
};
Human::Human(string hat,string clothes,string game):Human_hat(hat),Human_clothes(clothes),man(game)
{
cout<<"Human(string hat,string clothes,string game)"<<endl;
}
Human::~Human()
{
cout<<"~Human()"<<endl;
}

int main()
{
Human *p=new Human("red hat","green clothes","LOL");
cout<<"hat:"<<p->Human_hat+" "<<"clothes:"<<p->Human_clothes<<endl;
p->man.like();
delete p;
cin.get();
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: