类:类是一个模板,它描述一类对象的行为和属性。
对象:对象是类的实例化。
面向对象的三大特性:封装,继承,多态。
封装:将客观事物抽象成类,属性和行为作为一个整体表示事物
class Person
{
public: //访问权限
//属性 成员变量,成员属性
int age;
string name;
//行为,函数,成员方法
void showinfo()
{
cout<<"名字"<<name<<endl;
}
}
访问权限:
struct默认权限公共
class默认权限私有
//struct 和 class 的区别
//struct默认权限公共
//class默认权限私有
class Person
{
int id;
};
struct c2
{
int id;
};
int main()
{
Person p1;
c2 c;
//p1.id=10;私有无法访问 设置成public可
c.id = 2;
cout << c.id << endl;
system("pause");
return 0;
}
class Person
{
private://成员属性私有
int age;
string name;
public://读写权限
void setname(string m_namw)
{
name=m_name;
cout<<"名字是"<<name<<endl;
}
}
原文:https://www.cnblogs.com/panq/p/14878496.html