c++中类的类成员的访问权限以及简单的构造函数与析构函数
#include <stdio.h>
#include <string.h>
class Test
{
public:
char m_name[50];
int m_age;
void show1();
private:
int m_height;
void show();
public:
Test();
~Test();
};
int main()
{
Test test;
strcpy(test.m_name,"xiaoliyu");
test.m_age = 19;
test.show1();
}
void Test::show()
{
printf("private show()!!! \n" );
printf("name = %s,age = %d,height = %d \n",m_name,m_age,m_height);
}
void Test::show1()
{
show();
printf("public show1()!!! \n");
printf("name = %s,age = %d,height = %d \n",m_name,m_age,m_height);
}
printf("public Test() 构造函数!!!\n" );
}
//构造函数
Test::Test()
{
m_ height = 180;//初始化private中变量
printf("public Test() 构造函数!!!\n" );
}
//析构函数
Test::~Test()
{
printf("public ~Test() 析构函数!!!\n" );
}
总结:
public内部外部(类对象)均可以访问
private:仅仅内部可以访问,外部不可以访问
构造函数在类对象初始化时进行,第一个进行
析构函数在类对象调用完成后进行
运行结果:
public Test() 构造函数!!!
private show()!!!
name = xiaoliyu,age = 19,height = 180
public show1()!!!
name = xiaoliyu,age = 19,height = 180
public ~Test() 析构函数!!!