对象成员及初始化列表的使用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;
}
#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;
}
相关文章推荐
- C++中成员初始化列表的使用
- C++中成员初始化列表的使用
- C++中成员初始化列表的使用
- C++中成员初始化列表的使用
- c++高级---C++类构造函数初始化列表以及对象成员的构造
- C++中成员初始化列表的使用 .
- C++中成员初始化列表的使用
- C++中成员初始化列表的使用
- [原创]C++中成员初始化列表的使用!!!
- 黑马程序员_Java基础_面向对象(Static的使用、对象初始化和调用成员过程、单例设计模式)
- 使用初始化成员列表初始化数据与在构造函数中初始化数据
- C++对象模型(四):class成员初始化列表(Member Initialization List)
- C++对象模型(四):class成员初始化列表(Member Initialization List)
- 类成员的初始化--成员初始化列表(常量,引用,成员对象)
- C++中成员初始化列表的使用
- C++成员初始化列表使用总结
- <转载自刘佳ID:freedom0203和waret> C++中成员初始化列表的使用
- 必须使用【初始化列表】初始化数据成员的情况
- &lt;转载自刘佳ID:freedom0203和waret&gt; C++中成员初始化列表的使用
- 拷贝构造函数,也可以使用成员初始化列表语法