Haste makes waste

Nano01(自動運転)-【C++】-Lesson11-Classes and Inheritance类与继承

Posted on By lijun

1. Inheritance Introduction

main.hpp:

/*The header file for inheritance.*/

#include<iostream>
#include<string>
using namespace std;

//The base class
class Student
{
    private:
        int id;
    public:
        void setId(int idIn);
        int getId();
        Student();
};

Student::Student()
{
    id = 000000000;
}

void Student::setId(int idIn)
{
    id = idIn;
}

int Student::getId()
{
    return id;
}

//The derived class with Student as base class
class GradStudent : public Student
{
    private:
        string degree;
    public:
        GradStudent();
        void setDegree(string degreeIn);
        string getDegree();
};

GradStudent::GradStudent()
{
    degree = "undelcared";
}
void GradStudent::setDegree(string degreeIn)
{
    degree = degreeIn;
}
string GradStudent::getDegree()
{
    return degree;
}

main.cpp:

/*Goal: study a simple example if inheritance. 
*/
#include"main.hpp"

int main()
{
    //Create an instance of the derived class, 
    //GradStudent
    GradStudent gs1;
    
    //Set the id of the grad student
    //using the function in the base class
    gs1.setId(123456789);
    
    //Set the degree of the grad student
    //using the function in the derived class
    gs1.setDegree("BSEE");
    
    cout<<gs1.getId()<<" "<<gs1.getDegree();
    return 0;
}

2. Inheritance: Access Control

There are three types of access control: public, private, and protected.三种类的继承方式:

  • Public Inheritance means all public members of the base class are accessible to the derived class 能访问基类的公有成员变量
  • Private Inheritance means all members of the base class are private to the derived class 不能访问基类的成员变量
  • Protected Inheritance means all members of the base class are protected to the derived class.

上面的示例代码中,因为是public继承,所以子类能直接使用父类的公有函数,但是下面的是private继承,所以不能使用父类的getId()了。

main.hpp:

/*The header file for inheritance.*/

#include<iostream>
#include<string>
using namespace std;

//The base class
class Student
{
    private:
        int id;
    public:
        void setId(int idIn);
        int getId();
        Student();
        void setId();
};

Student::Student()
{
    id = 000000000;
}

void Student::setId(int idIn)
{
    
    id = idIn;
}

int Student::getId()
{
    return id;
}

//The derived class with Student as base class
class GradStudent : private Student
{
    private:
        string degree;
    public:
        GradStudent();
        void setDegree(string degreeIn);
        string getDegree();
        void setStudentId(int idIn);
        int getStudentId();
};

GradStudent::GradStudent()
{
    degree = "undelcared";
}

int GradStudent::getStudentId()
{
    return Student::getId();
}
void GradStudent::setStudentId(int idIn)
{
    Student::setId(idIn);
}
void GradStudent::setDegree(string degreeIn)
{
    degree = degreeIn;
}
string GradStudent::getDegree()
{
    return degree;
}

main.cpp:

/*Goal: study a simple example if inheritance. 
*/
#include"main.hpp"

int main()
{
    //Create an instance of the derived class, 
    //GradStudent
    GradStudent gs1;
    
    //Set the id of the grad student
    //using the function in the base class
    gs1.setStudentId(123456789);
    
    //Set the degree of the grad student
    //using the function in the derived class
    gs1.setDegree("BSEE");
    
    cout<<gs1.getStudentId()<<" "<<gs1.getDegree();
    return 0;
}

5. Multiple Inheritance

C++能多重继承,下面的代码中,从父类Staff和GradStudent中,继承得到了TA子类:

//header file for main.cpp
#include<iostream>
#include<string>
using namespace std;

class Staff
{
    private:
        string title;
    public:
        Staff();
        void setTitle(string input);
        string getTitle();
};

Staff::Staff()
{
    title = "NA";
}

void Staff::setTitle(string input)
{
    title = input;
}

string Staff::getTitle()
{
    return title;
}

class GradStudent
{
    private: 
        int studentId;
    public:
        GradStudent();
        void setId(int input);
        int getId();
    
};

GradStudent::GradStudent()
{
    studentId = 000000;
}

void GradStudent::setId(int input)
{
    studentId = input;
}

int GradStudent::getId()
{
    return studentId;
}

class TA: public Staff, public GradStudent
{
    private:
        string supervisor;
    public:
        TA();
        void setSupervisor(string input);
        string getSupervisor();
};

TA::TA()
{
    supervisor = "NA";
}

void TA::setSupervisor(string input)
{
    supervisor = input;
}

string TA::getSupervisor()
{
    return supervisor;
}

/*Goal: understand multiple inheritance*/

#include "main.hpp"

int main()
{
    TA t1;
    t1.setSupervisor("Dr. Caohuu");
    t1.setId(55555);
    t1.setTitle("Adjunct Prof.");
    cout<<t1.getSupervisor()<<" "<<t1.getId()<<" "<<t1.getTitle();
    return 0;
}

7. Student Playground

  • Compile the code and create an execuable file called main.out
g++ main.cpp -o main.out
  • Execute the executable file using input.txt as the input
./main.out input.txt