【C++】继承语法&&继承方式

何为继承?

继承是类与类之间的关系,与现实世界的继承类似。被继承的类称为父类或基类,继承的类称为子类或派生类。例如类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属性,所以可以直接赋值。

多级继承

多级继承的规则与上面相同。各成员在不同类中的访问属性在这里就不举例了,有兴趣的同学可以自己列一个表格看一下。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值