C++学习笔记-day12
作者:互联网
1、继承的基本语法
#include<iostream> using namespace std; //普通实现页面 //Java页面 //class Java //{ //public: // void head() // { // cout << "公共头部" << endl; // } // void footer() // { // cout << "公告底部" << endl; // } // void left() // { // cout << "公告分类列表" << endl; // } // void content() // { // cout << "java视频" << endl; // } //}; // //class Python //{ //public: // void head() // { // cout << "公共头部" << endl; // } // void footer() // { // cout << "公告底部" << endl; // } // void left() // { // cout << "公告分类列表" << endl; // } // void content() // { // cout << "python视频" << endl; // } //}; //继承实现页面 class Base { public: void head() { cout << "公共头部" << endl; } void footer() { cout << "公告底部" << endl; } void left() { cout << "公告分类列表" << endl; } }; //继承好处:减少重复的代码 //class 子类:继承方式 父类 //子类也称为派生类 父类也称为基类 class Java: public Base { public: void content() { cout << "java视频" << endl; } }; class Python : public Base { public: void content() { cout << "python视频" << endl; } }; void test01() { cout << "java视频页面如下" << endl; Java j; j.head(); j.footer(); j.left(); j.content(); cout << "*********" << endl; cout << "python视频页面如下" << endl; Python p; p.head(); p.footer(); p.left(); p.content(); } int main() { test01(); system("pause"); return 0; }
2、继承方式
#include<iostream> using namespace std; class Base1 { public: int m_A; protected: int m_B; private: int m_C; }; class son1 :public Base1 { public: void func() { //父类中公共权限到子类依然是公共权限 m_A = 10; //父类中保护权限到子类依然是保护权限 m_B = 10; //父类中私有权限 子类访问不到 //m_C = 10; } }; //保护继承 class son2 :protected Base1 { public: void func() { //父类中公共权限到子类是保护权限 m_A = 10; //父类中保护权限到子类依然是保护权限 m_B = 10; //父类中私有权限 子类访问不到 //m_C = 10; } }; class son3 :private Base1 { public: void func() { //父类中公共权限到子类是私有权限 m_A = 10; //父类中保护权限到子类依然是私有权限 m_B = 10; //父类中私有权限 子类访问不到 //m_C = 10; } }; class grandson3 :public son3 { public: void func() { //父类中私有权限 子类访问不到 //m_A = 10; //父类中私有权限 子类访问不到 //m_B = 10; //父类中私有权限 子类访问不到 //m_C = 10; } }; void test01() { son1 s1; s1.m_A = 100; //保护权限类外不可访问 //s1.m_B = 100; son2 s2; //保护权限类外不可访问 //s2.m_A = 100; son3 s3; //私有权限类外不可访问 //s3.m_A = 100; } int main() { system("pause"); return 0; }
标签:10,父类,私有,子类,笔记,C++,day12,权限,public 来源: https://www.cnblogs.com/lq0710/p/16683215.html