您的位置:首页 > 编程语言 > C语言/C++

【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属性,所以可以直接赋值。

多级继承

多级继承的规则与上面相同。各成员在不同类中的访问属性在这里就不举例了,有兴趣的同学可以自己列一个表格看一下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: