-
类的分类
- 表示
- 使用
-
C++中类的封装
- 成员变量:C++中用于表示类属性的变量
- 成员函数:C++中用于表示类行为的函数
- C++中可以给成员变量和成员函数定义访问级别
- public
- 成员变量和函数可以在类的内部和外界都能访问和调用
- private
- 成员变量和函数只能在类的内部被访问和调用
- public
#include <stdio.h>
#include <stdio.h>
struct Biology
{
bool living;
};
struct Animal : Biology
{
bool movable;
void findFood()
{
}
};
struct Plant : Biology
{
bool growable;
};
struct Beast : Animal
{
void sleep()
{
}
};
struct Human : Animal
{
void sleep()
{
printf("I'm sleeping...\n");
}
void work()
{
printf("I'm working...\n");
}
};
struct Girl : Human
{
private:
int age;
int weight;
public:
void print()
{
age = 22;
weight = 48;
printf("I'm a girl, I'm %d years old.\n", age);
printf("My weight is %d kg.\n", weight);
}
};
struct Boy : Human
{
private:
int height;
int salary;
public:
int age;
int weight;
void print()
{
height = 175;
salary = 9000;
printf("I'm a boy, my height is %d cm.\n", height);
printf("My salary is %d RMB.\n", salary);
}
};
int main()
{
Girl g;//创建类的对象
Boy b;
g.print();
b.age = 19;
b.weight = 120;
//b.height = 180;
b.print();
return 0;
}