c++类继承

从一个类派生出另一个类时,原始类称为基类,继承类成为派生类

一,一个简单的基类

1. 使用people作为基类,每个人都有姓名,年龄。 通过display展示个人信息。

people.h

#ifndef PEOPLE_H
#define PEOPLE_H

#include <iostream>
using namespace std;

class People
{
public:
    People();
    People(const string &name, const string &age);

    void display();
private:
    string m_name;
    string m_age;
};

#endif // PEOPLE_H

people.cpp

#include "people.h"

People::People()
{

}

People::People(const string &name, const string &age)
{
    m_name = name;
    m_age = age;
}

void People::display()
{
     cout << m_name << "  " << m_age << endl;
}

main.cpp
使用people类实例化对象

#include <iostream>
#include "people.h"

using namespace std;

int main()
{
    People people("kalu", "24");
    people.display();
    return 0;
}

二,派生一个类

1. 派生介绍

class Student : public  People
{
};
  • 冒号指出Student类的基类是People, public表明People是一个公有基类,这被称为公有派生,派生类对象包含基类对象。
  • 使用公有派生,基类的公有成员将成为派生类的公有成员,基类的私有部分也将称为派生类的一部分。
  • 派生类对象将具有以下特征
    • 派生类对象存储了基类的数据成员(派生类继承了基类的实现)。
    • 派生类对象可以使用基类的方法(派生类继承了基类的接口)。

2. 需要在继承特性中添加什么?

  • 派生类需要自己的构造函数
  • 派生类可以根据需要添加额外的数据成员和成员函数。
class Student : public  People
{
    Student(const string &score, const string &name, const string &age);
    Student(const string &score, const People *p);

    void reSetScore(const string &score);
private:
    string m_score;
};

构造函数必须给新成员和继承的成员提供数据,在第一个Student构造函数中,每个成员对应一个形参;第二个Student构造函数使用一个People参数,该参数包括name和age。

三,访问权限的考虑

  • 派生类不能直接访问基类的私有成员,必须通过基类方法进行访问。Student构造函数必须使用基类构造函数来设置继承的成员。
  • 创建派生类对象时,程序首先创建基类对象,从概念上来说,这意味着基类对象应当在程序进入派生类构造函数之前被创建,c++使用成员初始化列表来完成这种工作。
Student::Student(const string &name, const string &age, const string &score)
                : People (name, age)
{
	m_score = score;
}

假设有如下声明

Student student("kalu", "24", "100");

Student构造函数将把实参kalu, age赋给形参name, age, 然后将这些参数作为实参传递给People构造函数,People将创建一个嵌套对象,并将数据kalu, age 存储在对象中。然后,程序进入Student构造函数体,完成Student对象的创建,并将100赋给m_score成员。

四,使用派生类

方便使用,把基类和派生类放到了同一个文件中

  • people.h
#ifndef PEOPLE_H
#define PEOPLE_H

#include <iostream>
using namespace std;

class People
{
public:
    People();
    People(const string &name, const string &age);

    void display();
private:
    string m_name;
    string m_age;
};

class Student : public  People
{
public:
    Student(const string &name, const string &age, const string &score);
    Student(const string &score, const People *p);

    void reSetScore(const string &score);
private:
    string m_score;
};

#endif // PEOPLE_H
  • people.cpp
#include "people.h"

People::People()
{

}

People::People(const string &name, const string &age)
{
    m_name = name;
    m_age = age;
}

void People::display()
{
     cout << m_name << "  " << m_age << endl;
}

Student::Student(const string &name, const string &age, const string &score)
                : People (name, age)
{
    m_score = score;
}
  • main.cpp
#include <iostream>
#include "people.h"

using namespace std;

int main()
{
    People people("clclu", "23");
    Student student("kalu", "24", "100");
    people.display();
    student.display();
    return 0;
}

输出 :

clclu  23
kalu  24

五,派生类和基类之间的特殊关系

  • 派生类对象可以使用基类的方法,条件是方法不是私有的。
Student student("kalu", "24", "100");
student.display();
  • 基类指针可以在不进行显示类型转换的情况下指向派生类对象。
  • 基类引用可以在不进行显示类型转换的情况下引用派生类对象。
Student student("kalu", "24", "100");
People &rp = student;
People *pp = &student;

student.display();
rp.display();
pp->display();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值