zoukankan      html  css  js  c++  java
  • C++中的继承

    类和类之间的关系

    has-A:包含关系,用以描述一个类由多个“部件类”构成。实现has-A关系用类成员表示,即一个类中的数据成员是另一种已经定义的类。

    uses-A :一个类部分地使用另一个类。通过类之间成员函数的相互联系,定义友员或对象参数传递实现。

    is-A :机制称为“继承”。关系具有传递性,不具有对称性。

    #include <iostream>
    #include <string>
    using namespace std;
    
    class Student
    {
    private:
        string name;
        int age;
    public:
        Student(string name1, int age1):name(name1),age(age1){}
        void dis()
        {
            cout<<"name: "<<name<<endl;
            cout<<"age: "<<age<<endl;
        }
    };
    
    //继承
    class Student1: public Student
    {
    private:
        char sex;
        double score;
    public:
        Student1(string name1, int age1, char sex1, double score1):Student(name1, age1),sex(sex1),score(score1){}
        void dis()
        {
            Student::dis();
            cout<<"sex: "<<sex<<endl;
            cout<<"score: "<<score<<endl;
        }
    };
    
    int main()
    {
        Student stu("xmm", 26);
        stu.dis();
        cout<<endl;
    
        Student1 stu1("xmm", 26, 'M', 90);
        stu1.dis();
    
        return 0;
    }
    View Code

    不同继承方式的访问控制

  • 相关阅读:
    线程同步-使用CountDownEvent类
    WPF 依赖属性和附加属性
    ef core
    Razor语法
    python-爬虫
    ftp
    泛型
    结对编程作业
    第三次作业
    第二次作业
  • 原文地址:https://www.cnblogs.com/xumaomao/p/12913321.html
Copyright © 2011-2022 走看看