dimanche 14 juillet 2019

uninitialized member variable and non standard syntax use & to create a pointer to member

So i have created this example to simplify the question. I have 3 classes where 2 inherits from the same one.

MAN CLASS

class Man {
private:
    int age;
public:
    string name;

    Man() { //constructor
        this->age = 0;
        this->name = "default";
        cout << "created Man in default constructor" << endl;
    }
    Man(string, int);// constructor
};
Man::Man(string, int) {
    this->name = name;
    this->age = age;
    cout << "created Man: " << this->name << endl;
}

TEACHER CLASS

class Teacher : Man {
public:
    string field;
    Teacher(string, string) {
        this->name = name;
        this->field = field;
        cout << "created Teacher: " << this->name << endl;
    }

    Teacher() {
        this->field = "not declared";
        cout << "created Teacher in default constructor" << endl;
    }

    string getName() {
        return name;
    }
};

STUDENT CLASS

class Student : Man {
public:
    Teacher tutor;
    int age;

    Student(string, Teacher, int) {
        this->name = name;
        this->tutor = tutor;
        this->age = age;
        cout << "created student: " << this->name << " teacher is: " << this->tutor.getName << endl;
    }

};

Main

    int main(){

    Man erlichBlachman(12, "Erlich Blachman");
    Teacher richardHendricks("Richard Hendricks", "compression algorithms");
    Student dinesh("Dinesh", richardHendricks, 27);
    Student gilfoyle("Gilfoyle", richardHendricks, 32);
    }

ERRORS

enter image description here

Aucun commentaire:

Enregistrer un commentaire