面向对象介绍
C++面向对象三大特性:
- 封装
- 继承
- 多态 C++设计概念认为万事万物皆可为对象,对象有其属性和行为
类介绍
类的基本构成

class 类型名
{
访问权限:
变量;
方法(){/*可以在类内声明,也可以直接实现,一般情况做声明*/};
...
};例子:
class Box
{
public:
int b_height;
int wight;
int length;
void setheight();
};c++中的class和struct
C++中class和struct唯一区别在于默认访问权限不同
struct默认为公有class默认为私有- 继承也是默认
struct公有class私有
继承概要
继承介绍
面向对象三大特性之一
单继承:
class 子类:继承方式[默认:私有] 父类多继承:class 子类:继承方式[默认:私有] 父类, 继承方式[默认:私有] 父类,...title:Example1 ```cpp #include <iostream> using namespace std; class BaseClass { public: void topPage() {cout << "顶部页面" << endl;} void bottomPage() {cout << "底部页面" << endl;} void leftPage() {cout << "左侧页面" << endl;} void rightPage() {cout << "右侧页面" << endl;} }; class Java:public BaseClass { void centerPage() {cout << "Java页面" << endl;} }; class Cpp:public BaseClass { void centerPage() {cout << "Cpp页面" << endl;} }; class Python:public BaseClass { void centerPage() {cout << "Python页面" << endl;} }; int main() { Java java; Cpp cpp; Python python; java.centerPage(); cpp.centerPage(); python.centerPage(); /*output: Java页面 Cpp页面 Python页面 */ }title:Example2 ```cpp #include <iostream> using namespace std; class Base1 { printHello(){cout << "Hello ";} }; class Base2 { printfWorld(){cout << "World" << endl;} }; class Son: public Base1, public Base2; int main() { Son son; son.printHello(); son.printWorld(); return 0; } /*output: Hello World */指向原始笔记的链接总结
父类中的公共权限成员被子类继承后,子类能直接调用
```cpp
class Person
{
string name;
};
struct Person2
{
string name;
};```cpp
//from important
int main()
{
Person p;
Person2 p2;
p.name = "张三";//报错
p2.name = "李四";//编译成功
return 0;
}学习路径
- 上一节:cppOOP知识地图
- 当前阶段:cppOOP
- 下一节:cpp封装