【C++】继承语法&&继承方式
2016-10-07 17:03
363 查看
何为继承?
继承是类与类之间的关系,与现实世界的继承类似。被继承的类称为父类或基类,继承的类称为子类或派生类。例如类B继承于类A,那么B拥有A的成员变量和成员函数。派生类除了拥有基类的成员还可以定义自己的新成员。
使用继承的场景:
1.要创建的新类与已有的类类似,只是多出几个成员变量或者成员函数2.当要创建多个类,它们拥有很多类似的成员变量或者成员函数时,可以把这些类共同的成员提取出来,定义一个基类,然后由基类继承。
声明派生类的语法:
class 派生类名 : [继承方式] 基类名 {派生类新增加的成员
} ;
来举个继承的小实例
/************************************************************************* > File Name: 继承1.cpp > Author: Tanswer_ > Mail: 98duxm@gmail.com > Created Time: ************************************************************************/ #include <iostream> #include <string> #include <algorithm> #include <vector> #include <set> #include <cstring> #include <cmath> #include <cstdio> #include <stack> #include <queue> using namespace std; //基类 class People { private: string name; int age; public: void setName(string ); void setAge(int ); string getName(); int getAge(); }; void People::setName(string name) { this->name = name; } void People::setAge(int age) { this->age = age; } string People::getName() { return name; } int People::getAge() { return this->age; } //派生类--Student class Student: public People //注意语法 { private: float score; public: void setScore(float ); float getScore(); }; void Student::setScore(float score) { this->score = score; } float Student::getScore() { return score; } int main() { Student stu; /*派生类直接使用基类的成员*/ stu.setName("小明"); stu.setAge(19); stu.setScore(99.9f); cout << stu.getName() << " 年龄是: " << stu.getAge() << " 成绩是:" << stu.getScore() << endl; return 0; }
继承方式
继承方式包括public(公有的)、private(私有的)和protected(受保护的).可写可不写,不写则默认为private.1)public继承方式
基类中所有public成员在派生类中为public属性;
基类中所有protected成员在派生类中为protected属性;
2)protectrd继承方式
基类中所有public、protected成员在派生类中为protected属性;
3)private继承方式
基类中所有public、protected成员在派生类中为private属性;
注意:
1.基类中的private成员在派生类中始终是不可访问的。
2.基类成员在派生类中的访问权限不得高于继承方式中指定的权限。
改变访问属性
使用using 关键字改变基类成员在派生类中的访问属性/************************************************************************* > File Name: 继承_访问属性.cpp > Author: Tanswer_ > Mail: 98duxm@gmail.com > Created Time: ************************************************************************/ #include <iostream> #include <string> #include <algorithm> #include <vector> #include <set> #include <cstring> #include <cmath> #include <cstdio> #include <stack> #include <queue> using namespace std; class People { protected: string name; int age; public: void say() { cout << "hello linux" << endl; } }; class Student: public People { private: using People::say; //say不能加括号 public: using People::name; using People::age; float score; void display(); }; void Student::display() { cout << name << " . Age--" << age << " . Score--" << score << endl; } int main() { Student stu; stu.name = "小明"; stu.age = 19; stu.score = 99.9f; //stu.say(); //error stu.display(); return 0; }
基类People包含两个protected属性的成员变量和一个public属性的成员函数。定义派生类时采用public继承方式,那么基类People成员在派生类Student的访问权限是不变的。
下面我们使用 using 改变了默认的访问权限,将say()改为private属性的,所以代码第54行直接访问 say 函数是错误的;将name age变量修改为public属性,所以可以直接赋值。
多级继承
多级继承的规则与上面相同。各成员在不同类中的访问属性在这里就不举例了,有兴趣的同学可以自己列一个表格看一下。相关文章推荐
- 浅谈C++多态性 & C++虚继承的实现方式与内存布局
- C++构造函数&继承方式
- C++三种继承方式 & is-a & has-a & 隐藏
- C++语法基础--public,protected,private的继承方式及其访问性
- 改善C++程序的建议:语法篇1<从C继承而来的特性>
- 对于C++保护继承方式的一点理解
- C++ 派生类的三种继承方式
- Exception C++中讨论的异常安全堆栈的,封装内存管理的版本,使用私有继承的方式复用
- C++ 继承方式
- c++ 继承方式 合集
- C++中类的三种继承方式
- c++继承方式及对基类的访问权限
- c++基础语法---虚继承
- C++成员访问控制,以及C++继承方式对成员访问控制的影响
- 《C++第十二周实验报告2-1》--分别定义Teacher(教师)类和Cadre(干部采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)
- C++三种继承方式
- 【C/C++语法外功】传值&传引用&传指针
- C++语法细微(四)(运算符重载/继承派生)
- C++中的各种继承方式规则
- C++继承方式区别