C++繼承與派生--訪問控制

繼承與派生

說明:如下實驗證實一個是出自以Person類爲父類,Student爲子類的源代碼,另外一個出自以Point爲父類,Recetangle爲子類的源代碼ios

派生類生成過程

1.吸取基類成員

2.改造基類成員

3.添加新成員
函數

過程三步驟詳述

  • 吸取基類成員

    派生過程當中構造函數和析構函數都不被繼承,所以在對派生類新增成員進行初始化,須要在派生類寫構造函數負責對新增成員的初始化,而從基類繼承的成員,依舊由基類的構造函數完成測試

  • 改造基類成員

    (1)基類成員的訪問問題,靠繼承方式控制

    (2)對基類數據或函數成員的覆蓋和隱藏,若是派生類聲明瞭一個和某個基類成員同名的新成員(注:函數要參數相同,若是不一樣的話,就是函數重載),派生的新成員會隱藏基類中同名的函數,而且在類外經過生成對象來直接使用成員名時,基類的成員就會被隱藏,即同名隱藏
    this

同名隱藏

spa

同名隱藏前:

3d

同名隱藏後:

code

  • 添加新成員

    經過派生類中新成員派生類新功能的實現:

Person類中的成員函數:

對象

Student類中的成員函數:

blog

訪問控制

  • 繼承語法:

    Class 派生類名:繼承方式(public private protected) 基類名{}
  • 分類:

    (1)公有繼承:(經常使用)

    當類的繼承方式是公有繼承,即public時,父類中public和protected中的成員訪問屬性不變,而父類private中的成員在子類中不能直接訪問,可經過從父類中繼承的公共接口對此進行操做

私有成員報錯

繼承

公有和保護成員不報錯

(2)私有繼承:(不經常使用)

當類的繼承方式是私有繼承,即private時,父類的公有成員和保護成員以私有成員的身份出如今子類中,在子類內部能夠互相互訪問,但在類外經過對象就不能訪問和操做,而父類private中的成員在子類中仍是子類外都不能直接訪問通過私有繼承後,父類中的成員成爲子類私有成員或者不可直接訪問的成員,那麼若是進一步派生的話,基類的所有成員就沒法在新的派生類中直接訪問

子類內部不報錯

經過對象訪問報錯

(3)保護繼承:

當類的繼承方式是保護繼承,即protected時,父類的公有成員和保護成員以保護成員的身份出如今子類中,在子類內部能夠互相訪問,但在類外經過對象就不能訪問和操做,而父類private中的成員在子類中仍是子類外都不能直接訪問

子類內部不報錯

經過對象訪問報錯

以上全部實驗的代碼

以Point類爲父類,Recetangle爲子類的源代碼

頭文件聲明:

#ifndef POINT_H
#define POINT_H

//Point類定義

class Point
{
public:
    void initP(float xx , float yy ) ;
    void move(float xoff, float yoff);
    float getX() const;
    float getY() const;
private:
    float x, y;
};

//retangle類定義
class Recetangle:private Point
{
public:
    void initR(float x, float y, float w, float h);
    float getWidth() const;
    float getHeight() const;
private:
    float w, h;
};

頭文件函數實現:

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

using namespace std;

//point類函數實現
 void Point::initP(float xx=0, float yy = 0) 
{
    x=xx;
    y=yy;
}
void Point::move(float xoff, float yoff)
{
    x += xoff;
    y += yoff;
}
float Point::getX() const
{
    return x;
}
float Point::getY() const
{
    return y;
}


//Recetangle類函數實現
 void Recetangle::initR(float x, float y, float w, float h)
{
    initP(x, y);//私有繼承,公有成員子類內部能夠訪問
    this->w = w;
    this->h = h;
}
float Recetangle::getHeight() const
{
    return h;
}
float Recetangle::getWidth() const
{
    return w;
}

main函數:

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

using namespace std;

int main()
{
    /*Recetangle test;
    test.initR(2, 3, 20, 30);
    test.move(3, 2);
    cout << test.getX() << "  "
        << test.getY ()<< "  "
        << test.getWidth() << "  "
        <<test.getHeight() << "  "
        << endl;*/

    //Recetangle test2;
    //test2.getX();//以私有繼承,在類外經過對象進行訪問,編譯器報錯
    //test2.getY();

    //Recetangle test3;
    //test3.getX();
    //test3.initP();//以保護繼承,在類外不能經過對象訪問
            
    return 0;
}

以Person類爲父類,Student爲子類的源代碼

頭文件聲明:

#ifndef PCH_H
#define PCH_H

#include<string>

using namespace std;

class Person
{
public:
    Person();
    ~Person();
    void Inputinfo();
    void Showinfo() const;
private:
    string name;
protected:
    int age;
    
};
class Student:public Person
{
public:
    Student();
    void Inputinfo2();
    //void Inputinfo();//同名隱藏實驗
    void Showinfo2() const;

private:
    float  grades;
    int id;
};

頭文件中函數實現:

#include "pch.h"
#include<iostream>
#include<string>

using namespace std;


/****Person類成員函數實現****/

//功能:構造函數
Person::Person():name(name),age(age){}

//功能:析構函數,人數減1
Person::~Person(){}

//功能:輸入用戶名字、年齡
void Person::Inputinfo()
{
    cout << "Pleasing input your name" << endl;
    cin >> name;
    cout << "Pleasing input your age" << endl;
    cin >> age;
}

//功能:顯示用戶名字、年齡
void Person::Showinfo() const
{
    cout << "用戶名: " << name << " \t " << "年齡 :" << age;
    
}



/****Student類成員函數的實現****/

//功能:Student類中構造函數實現
Student::Student():id(id),grades(grades){}

//功能:輸入學生名字、年齡、學號、成績
void Student::Inputinfo2()
{
    Inputinfo();
    cout << "Pleasing input your id" << endl;
    cin >> id;
    cout << "Pleasing input your grades" << endl;
    cin >> grades;
}

////功能:輸入學生學號、成績(實現同名隱藏,經過對象操做時同名函數採起就近原則)
//void Student::Inputinfo()
//{
//  cout << "Pleasing input your id" << endl;
//  cin >> id;
//  cout << "Pleasing input your grades" << endl;
//  cin >> grades;
//}

//功能:顯示學生名字、年齡、學號、成績
void Student::Showinfo2() const
{
    Showinfo();
    cout << "\t";
    cout << "學號: " << id << " \t " << "成績: " << grades << endl;
}

////測試子類不能直接訪問父類私有成員,編譯器報錯
//void Student::Showinfo2() const
//{
//  cout << name
//      << age;
//}


////測試以public繼承,父類的公有成員和保護成員在子類內部能夠直接訪問,編譯器不報錯
//void Student::Showinfo2() const
//{
//  Showinfo();//父類公有成員
//  cout << age;//在作這步實驗時,將本來私有成員的age改爲了保護成員
//  cout << "\t";
//  cout << "學號: " << id << " \t " << "成績: " << grades << endl;
//}

main函數:

#include "pch.h"
#include <iostream>
#include<string>

using namespace std;


int main()
{
    /*Person p1;
    p1.Inputinfo();
    p1.Showinfo();*/

    Student stu1;
    //stu1.Inputinfo();//同名隱藏前,本來Person類中Inputinfo函數輸入的是name和age
    stu1.Inputinfo2();
    stu1.Showinfo2();

    return 0;
}
相關文章
相關標籤/搜索