c++ 基础知识-类和对象-对象特性
1.初始化列表
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(int a,int b,int c):m_a(a),m_b(b),m_c(c)
{
cout<<"Person:初始化列表方式初始化"<<endl;
}
int m_a;
int m_b;
int m_c;
};
int main()
{
Person(1,2,3);
return 0;
}
2.类对象作为类成员
先调用后释放,后调用先释放,类似于栈
#include <iostream>
#include <string>
using namespace std;
class Phone
{
public:
Phone(string p)
{
m_p = p;
cout<<"Phone:构造函数"<<endl;
}
~Phone()
{
cout<<"Phone:析构函数"<<endl;
}
string m_p;
};
class Person
{
public:
Person(int a,string p):m_a(a),m_p(p)
{
cout<<"Person:构造函数"<<endl;
}
~Person()
{
cout<<"Person:析构函数"<<endl;
}
int m_a;
Phone m_p;
};
int main()
{
int a = 8;
Person(a,"kk");
return 0;
}