C++之继承与派生类的关系

  • 子类对象会继承基类的属性的行为,任何时候子类对象都可以被当做基类类型的对象,通过子类对象可以直接访问基类中的成员,如同是基类对象在访问它们一样

向上造型和向下造型

  • 向上造型(upcast):将子类类型的指针或引用转换为基类类型的指针或引用;这种操作性缩小的类型转换,在编译器看来是安全的,可以隐式转换
  • 向下造型(downcast):将基类类型的指针或引用转换为子类类型的指针或引用;这种操作性放大的类型转换,在编译器看来是危险的,不能隐式转化,但是可以显式转换
cpp 复制代码
#include <iostream>
using namespace std;
class Human{
private:
    int m_private;
protected:
    string m_name;
    int m_age;
    const int& get(void){
        return m_private;
    }
public:
    Human(const string &name, int age){
        m_name = name;
        m_age = age;
        m_private = 1234;
    }
    void eat(const string& food){
        cout << "我在吃: " << food << endl;
    }
    void sleep(int hour){
        cout << "我睡了" << hour << "小时" <<endl;
    }
};
class Student: public Human{
private:
    int m_no; //学号
public:
    Student(const string& name, int age, int no):Human(name, age){
        m_no = no;
    }
    void who(void){
        cout << "我叫: " << m_name << ", 今年" <<m_age<<"岁,学号是: "<<m_no << endl;
        //cout << m_private << endl; //error
        cout << get() << endl;
    }
    void learn(const string& course){
        cout << "我在学" << course << endl;
    }
    };
class Teacher: public Human{
private:
    int m_salary;
public:
    Teacher(const string& name, int age, int salary):Human(name, age),m_salary(salary){
    
    }
    void teach(const string& course){
        cout << "我正在讲 " << course << endl;
    }
    void who(void){
        cout << "我叫 "<<m_name << ",今年" << m_age << "岁, 工资是" << m_salary << endl;
    }
};
int main(void){
    Student s("张飞", 28, 100011);
    cout << "sizeof(s) = "<<sizeof(s) << endl;
    s.who();
    s.eat("宫保鸡丁");
    s.sleep(8);
    s.learn("C++编程");
    Teacher t("诸葛亮", 34, 200000);
    t.who();
    t.teach("嵌入式");
    t.sleep(7);
    t.eat("汉堡");
    // Student * -----> Human *:向上造型
    Human *ph = &s;
    ph->eat("香蕉");
    ph->sleep(10);
    //ph->who(); //error
    // Human * --------> Student *: 向下造型(合理)
    Student *ps = static_cast<Student *>(ph);
    ps->who();
    Human h("赵云", 22);
    //Human * -------> Student *: 向下造型 (不合理)
    Student *ps2 = static_cast<Student *>(&h);
    ps2->who();
    return 0;
}

成员函数的重定义(名字隐藏)

  • 重定义: 简单的说就是子类中定义了和父类的同名函数,对父类的成员函数造成了隐藏
cpp 复制代码
#include <iostream>
using namespace std;
class Base{
private:
    int x;
public:
    void set(int i){
        x = i;
    }
    void print(){
        cout << "Base class " << "x= " << x << endl;
    }
};
class Derived: public Base{
private:
    int m, n;
public:
    void set(int p, int k){
        m = p;
        n = k;
    }
    void print(){
        Base::print();
        cout << "Derived class "<< "m = "<< m <<", n=" << n << endl;
    }
};
int main(void){
    Derived d;
    d.set(10,20);
    //d.set(100); // error 名字隐藏
    d.Base::set(100);
    d.print();
    return 0;
}
相关推荐
我变成萤火虫1 小时前
河南萌新联赛2026第(四)场:南阳理工学院
数据结构·c++·算法·贪心算法·stl·动态规划
余额瞒着我当琳5 小时前
C++--深拷贝三件套 + swap + 写时拷贝 + vector 扩容 + reserve
java·开发语言·c++
ShineWinsu6 小时前
对于C++:布隆过滤器(bloomfilter)的详细解析
c++·面试·位图·位运算·布隆过滤器·海量数据·比特位
牛油果子哥q10 小时前
C++内存模型与深浅拷贝万字详解:栈堆静态内存布局、深浅拷贝底层差异、内存泄漏根治、拷贝崩溃踩坑、手写深拷贝实战
java·开发语言·c++
大鹏的NLP博客10 小时前
通用 Linux 嵌入式板端 C/C++ ABI 与 Glibc 依赖治理规范
linux·c++·交叉编译
ysa05103011 小时前
c++常用自带函数用法与注意
c++·笔记·算法
qq_1508419913 小时前
从CVI(C)到Pyside(python)/Qt(C++)/VS(C#)的一点吐槽
c++·qt·c#
鸿芯微控科技15 小时前
MFC模拟量信号异常怎么排查?0-5V、4-20mA接线与量程换算
c++·5g·mfc·故障排查·质量流量控制器·4-20ma·模拟量信号
三十岁老牛再出发15 小时前
08.18每日总结
c++·python·numpy·pandas
余额瞒着我当琳17 小时前
C++--vector底层,leetcode118杨辉三角实战,对于传统reserve的坑点,迭代器失效
开发语言·c++