C++面向对象 继承

发布于:2025-05-07 ⋅ 阅读:(20) ⋅ 点赞:(0)

格式

class 子类:继承方式 父类
{


};

//子类  又称为派生类
//父类  又称为基类

三种继承方式 

继承中的同名成员处理

 继承中的同名静态成员处理

包含子对象的派生类构造函数

作用:

包含子对象的派生类构造函数用于在创建派生类对象时,不仅初始化派生类自身的数据成员,还能正确地初始化其基类子对象和子对象(即成员对象)

构造和析构顺序

  • 构造顺序:基类构造函数 -> 子对象构造函数 -> 派生类构造函数。
  1. 基类构造函数:由于派生类继承自基类,所以基类的构造函数会首先被调用,目的是初始化从基类继承来的成员变量。
  2. 子对象构造函数:在基类构造函数执行完毕后,派生类中包含的子对象的构造函数会依次被调用,以完成子对象的初始化。
  3. 派生类构造函数:最后,派生类自身的构造函数才会执行,对派生类特有的成员变量进行初始化。
  • 析构顺序:派生类析构函数 -> 子对象析构函数 -> 基类析构函数。

模板:

#include <iostream>
class Base 
{
public:
    Base() { std::cout << "基类调用" << std::endl; }
};

class Member 
{
public:
    Member() { std::cout << "子对象调用" << std::endl; }
};

class Derived : public Base 
{
public:
    Member member;
    Derived() { std::cout << "派生类调用" << std::endl; }
};

int main() 
{
    Derived d;
    return 0;
}
//运行结果

基类调用
子对象调用
派生类调用

例题1

#include <iostream>
#include <string>
using namespace std;
// 生日类
class BirthDate
{
public:
    int year;
    int month;
    int day;
    // 构造函数
    BirthDate(int y, int m, int d) : year(y), month(m), day(d) {}
    // 修改生日的函数
    void modifyBirthday(int y, int m, int d)
    {
        year = y;
        month = m;
        day = d;
    }
    // 输出生日信息的函数
    void displayBirthday()
    {
        cout << "year:" << year << endl;
        cout << "month:" << month << endl;
        cout << "day:" << day << endl;
    }
};
// 教师类
class Teacher
{
public:
    int num;
    string name;
    char sex;
    BirthDate birth;//定义子对象
    // 构造函数
    Teacher(int n, string na, char se, int y, int m, int d) : num(n), name(na), sex(se), birth(y, m, d) {}
// 输出教师个人信息的函数
    void displayInfo()
    {
        cout << "num:" << num << endl;
        cout << "name:" << name << endl;
        cout << "sex:" << sex << endl;
        birth.displayBirthday();
    }
    // 修改教师生日的函数
    void modifyTeacherBirthday(int y, int m, int d)
    {
        birth.modifyBirthday(y, m, d);
    }
};
int main()
{
    // 定义 Teacher 类对象 tea1 并给出所有数据的初值
    Teacher tea1(101, "张三", 'm', 1973, 4, 17);
    // 输出教师的初始信息
    tea1.displayInfo();
    // 修改 tea1 的生日数据
    tea1.modifyTeacherBirthday(1586, 5, 25);
    // 输出教师修改后的信息
    tea1.displayInfo();
    return 0;
}

例题2 

#include<iostream>
#include<string>
using namespace std;
class Person
{
    string name;
    char sex;
    int age;
public:
    Person(string na,char se,int ag):name(na),sex(se),age(ag){}
    void IntroduceMe()
    {
        string s=(sex=='M'?"Male":"Female");
        cout<<"My name is"<<name<<", "<<s<<", "<<age<<"years old."<<endl;
    }
    void Eat()
    {
        cout<<"I'm hungry! I'd like something to eat! "<<endl;
    }
    void Rest()
    {
        cout<<"I'm tired. I need to rest."<<endl;
    }
};
class Student:public Person
{
    string num;
    string zhuanye;
public:
    Student(string na,char se,int ag,string nu,string zy):Person(na,se,ag),num(nu),zhuanye(zy)
    {
       cout<<"我的学号是"<<num<<"专业是"<<zhuanye<<endl;
    }
    void shangke()
    {
        cout<<"我正在上课"<<endl;
    }
    void kaoshi()
    {
        cout<<"我正在考试"<<endl;
    }
};
int main()
{

    Person p1("张三",'M',19);
    p1.IntroduceMe();
    p1.Eat();
    p1.Rest();
    cout<<"---------------------------------------"<<endl;
    Student s2("李四",'M',18,"201210101","计算机");
    s2.Eat();
    s2.Rest();
    s2.IntroduceMe();
    s2.shangke();
    s2.kaoshi();



    return 0;
}

多继承语法

作用:允许一个类继承多个类

语法:class 子类:继承方式 父类1,继承方式 父类2...

如果在多继承中引发同名成员出现,需要加作用于区分


网站公告

今日签到

点亮在社区的每一天
去签到