0

I want to ask you why those two 'Student' constructor commented below don't work.

    #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;
    }

In addition I want to ask...

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

inside the arguments of 'Student' constructor in this sentence, are 'studentID 'and 'name' just arguments and just do works in the scope of 'Student' constructor? Or 'studentID 'and 'name' are the member variables of 'Student' class and 'Person' class?

I don't have idea how this sentence can give argument to the 'Person' constructor

Student student = Student(1, "James");
Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30
Shrew
  • 13
  • 2
  • 4
    What do you mean by "don't work"? – Fred Larson May 11 '21 at 14:58
  • 3
    It's no more simple than this is not how C++ constructors works. Not every bit of grammar that looks like valid C++ code actually does what we want it to do. Member initialization in constructors have requirements, and a strict syntax. There are no variations to this syntax. For example, putting `Person(name)` in the constructor's body does not construct the superclass. C++ does not work this way. It must be specified in the constructor's initialization section. – Sam Varshavchik May 11 '21 at 14:59
  • `Student(int studentID, string name){/**/}` is equivalent to `Student(int studentID, string name) : Person(), Temp() {/**/}` and `Person` is not default constructible. – Jarod42 May 11 '21 at 15:03
  • _inside the arguments of 'Student' constructor in this sentence, are 'studentID 'and 'name' just arguments_ Yes. Due to this, the member variables with same name are eclipsed. Nevertheless, `Student(int studentID, string name) : Person(name), studentID(studentID) { }` will work. – Scheff's Cat May 11 '21 at 15:03
  • Make **full** use of the initialization section, so many problem go away. – sweenish May 11 '21 at 15:04
  • `Person(name);` is not what you think: it is equivalent to `Person name;` create temporary `Person` (which is also invalid, as Person still doesn't have default constructor) and name's conflict with `name`. – Jarod42 May 11 '21 at 15:05
  • BTW, you can get rid of the `this->` syntax by using a naming convention where members are named differently than parameters (or you can use an initialization list). – Thomas Matthews May 11 '21 at 15:15

0 Answers0