首页 > 其他 > 详细

面向对象——继承

时间:2021-05-16 19:25:14      阅读:38      评论:0      收藏:0      [点我收藏+]

友元类增加访问权限

#include <iostream>

using namespace std;

class M {
    friend class N;

   private:
    int i, j;
    void show(void) { cout << "i = " << i << \t << "j = " << j << \t; }

   public:
    M(int a = 0, int b = 0) {
        i = a;
        j = b;
    }
};

class N : M {
   public:
    N(int a = 0, int b = 0) : M(a, b) {}
    void print() {
        show();
        cout << "i + j = " << i + j << endl;
    }
};

int main() {
    N n1(10, 20);
    M m1(100, 200);
    n1.print();
    return 0;
}

 

同名覆盖原则

#include <iostream>
using namespace std;

class Human {
   private:
    int age;

   public:
    void setAge(int a) { age = a; }
    int getAge() { return age; }
    void showAge() { cout << "the human age is  " << getAge() << endl; }
};
class student : public Human {
   public:
    void showAge() { cout << "the student age is  " << getAge() << endl; }
};
int main() {
    student jessic;
    jessic.setAge(12);
    jessic.showAge();
    jessic.Human::showAge();
    Human *hp;
    hp = &jessic;
    // 为什么输出的是Human的年龄?
    // 自动类型转换
    hp->showAge();
    return 0;
}

 

派生类的构造函数和析构函数

// 派生类的构造函数和析构函数
// 构造函数和析构函数是不能够被继承的

#include <iostream>

using namespace std;

class Human {
   public:
    Human() { cout << "a person is constructed!!!!\n"; }
    ~Human() { cout << "a person is deconstructed!!!!\n"; }
};
class Student : public Human {
   public:
    Student() { cout << "a student is constructed!!!!\n"; }
    ~Student() { cout << "a student is deconstructed!!!!\n"; }
};
void main() {
    Student jessic;
    cout << "this is main function\n";
}

 

基类构造函数传参

#include <iostream>

using namespace std;

class Human {
   private:
    int age;

   public:
    Human(int n) {
        this->age = n;
        cout << "a person is constructed!!!!\n";
    }
    ~Human() { cout << "a person is deconstructed!!!!\n"; }
};
class Student : public Human {
   private:
    int sno;

   public:
    Student(int no, int n) : Human(n) {
        this->sno = no;
        cout << "a student is constructed!!!!\n";
    }
    ~Student() { cout << "a student is deconstructed!!!!\n"; }
};
int main() {
    Student jessic(10000, 24);
    cout << "this is main function\n";
    return 0;
}

 

组合类的构造函数的先后顺序

// 添加Pad类
// 组合类对象的构造过程:先基类,后整体;先部分,后整体;(先别人,后自己)

#include <iostream>

using namespace std;

class Pad {
   public:
    Pad() { cout << "A Pad is constructed!!!\n"; }
    ~Pad() { cout << "A Pad is deconstructed!!!\n"; }
};

class Human {
   private:
    int age;

   public:
    Human(int n) {
        this->age = n;
        cout << "a person is constructed!!!!\n";
    }
    ~Human() { cout << "a person is deconstructed!!!!\n"; }
};
class Student : public Human {
   private:
    int sno;
    Pad pd;

   public:
    Student(int no, int n) : Human(n) {
        this->sno = no;
        cout << "a student is constructed!!!!\n";
    }
    ~Student() { cout << "a student is deconstructed!!!!\n"; }
};
int main() {
    Student jessic(10000, 24);
    cout << "this is main function\n";
    return 0;
}

 

组合类构造函数传参

#include <iostream>

using namespace std;

class Pad {
   private:
    int color;

   public:
    Pad(int c) {
        this->color = c;
        cout << "A Pad is constructed!!!\n";
    }
    ~Pad() { cout << "A Pad is deconstructed!!!\n"; }
};

class Human {
   private:
    int age;

   public:
    Human(int n) {
        this->age = n;
        cout << "a person is constructed!!!!\n";
    }
    ~Human() { cout << "a person is deconstructed!!!!\n"; }
};
class Student : public Human {
   private:
    int sno;
    Pad pd;

   public:
    Student(int no, int n, int c)
        : Human(n), pd(c) {  // 注意:这里是使用对象的实例进行传参
        this->sno = no;
        cout << "a student is constructed!!!!\n";
    }
    ~Student() { cout << "a student is deconstructed!!!!\n"; }
};
int main() {
    Student jessic(10000, 24, 1);
    cout << "this is main function\n";
    return 0;
}

 

面向对象——继承

原文:https://www.cnblogs.com/h-hkai/p/14773864.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!