C++中构造函数的成员初始化

问题描述

我想问你为什么下面评论的那两个“学生”构造函数不起作用。

    #include<iostream>
    #include<string>
    
    using namespace std;
    
    class Temp{
    public:
        void Tempshow(){
            cout << "Mother Class" << endl;
        }
    };
    
    class Person{
    private:
        string name;
    public:
        Person(string name): name(name){}   //member initialization
        string getName(){
            return name;
        }
        void showName(){
            cout << "Name : " << getName() << endl;
        }
    };
    
    class Student : Person,public Temp {
    private:
        int studentID;
    public:
        Student(int studentID,string name) : Person(name){
            this->studentID = studentID;                                                                
        }
    
        // Student(int studentID,string name){
        //  Person(name);
        //  this->studentID = studentID;
        // }
        // Student(int studentID,string name){
        //  this->studentID = studentID;
        // }
        
        void show(){
            cout << "Student Number : " << studentID << endl;
            cout << "Student Name : " << getName() << endl;
            
        }
        void showName(){
            cout << "Inherent Success : " << getName() << endl;
        }
    };
    
    int main(){
        Student student = Student(1,"James");
        student.showName();
        student.Tempshow();
        return 0;
    }

另外我想问...

    Student(int studentID,string name) : Person(name){
            this->studentID = studentID;                                                                
        }

在这句话中'Student'构造函数的参数里面,'studentID'和'name'只是参数,只是在'Student'构造函数的范围内起作用吗? 或者'studentID'和'name'是'Student'类和'Person'类的成员变量?

我不知道这句话如何为“Person”构造函数提供参数

Student student = Student(1,"James");

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)