A little bit stuck on implementing child class from parent class

Hello, my entire program (including the header files) all compile. But when I go to test my program, my get is not being shown in the console window, i'm not sure what the issue is. Help is appreciated.

Just some program context: Make a program that uses derived classes "professor" & "student". They come from the parent class "Person". After making the classes test the output by saying all the teachers names & locations. Also display the one students name, and all the courses being taken.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <iostream>
#include <string>
#include "Person.h"
#include "Professor.h"
#include "Student.h"

using namespace std;

int main()
{
    // create object of the Person class
    Professor professor1;
    Student student1;


    // call members of the base class
    professor1.name();
    professor1.Setname("Ms.Puff");

    student1.name();
    student1.Setname("Spongebob");



    // call members from the derived class
    professor1.office();
    professor1.Settype("The Bikini Bottom");

    student1.courses();
    student1.Settype("Driving Lessons, Cooking");

    // use get of the student and professor object as an argument
    professor1.displayInfo(professor1.Getname());

    student1.displayInfo(student1.Getname());
}


Header files for "person"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>

#ifndef PERSON_H
#define PERSON_H

 using namespace std;

class Person // parent class
{
    public:
        Person();
        virtual ~Person();

        string Getname() { return m_name; }
        void Setname(string val) { m_name = val; }

        string Gettype() { return m_type; }
        void Settype(string val) { m_type = val; }

        void office();

        void courses();

    protected:
        string m_type;

    private:
        string m_name;
};

#endif // PERSON_H


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Person.h"
#include <iostream>
#include <string>

Person::Person()
{
    m_type = "";
    m_name = "";
}

Person::~Person()
{
    //dtor
}

void Person::office()
{
    cout << "This teacher is located in office: " << endl;
}

void Person::courses()
{
    cout << "The student is taking these courses:  " << endl;
}


Header File for "professor"
 

#include <string>
#include "Person.h"

#ifndef PROFESSOR_H
#define PROFESSOR_H

using namespace std;


class Professor : public Person // professor is child class
{
public:
Professor();
virtual ~Professor();

void name();

void office();

void displayInfo(string c);

protected:

private:

};

#endif // PROFESSOR_H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include "Professor.h"

#include <string>
#include <iostream>

using namespace std;

Professor::Professor()
{
    //ctor
}

Professor::~Professor()
{
}

void Professor::name()
{

}

void Professor::office()
{

}

void Professor::displayInfo(string c)
{
    cout << "The professors name is: " << endl;
    cout << "Their office is located in: " << endl;
}


Header file for "student"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include "Person.h"

#ifndef STUDENT_H
#define STUDENT_H

using namespace std;

class Student : public Person // student is child class
{
    public:
        Student();
        virtual ~Student();

        void name();

        void courses();

        void displayInfo(string c);


    protected:

    private:

};

#endif // STUDENT_H 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "Student.h"

#include <string>
#include <iostream>

using namespace std;

Student::Student()
{
   // ctor
}

Student::~Student()
{
}

void Student::name()
{

}
void Student::courses()
{

}

void Student::displayInfo(string c)
{
    cout << "The students name is: " << endl;
    cout << "The courses they're attending are: " << endl;
}


MY OUTPUT:


The professors name is:
Their office is located in:
The students name is:
The courses they're attending are:


: Why is the teachers name, location, and the students name and courses not being displayed?
Last edited on
well, it may not be ALL the code, but in what you provided, you only have cout statements with constant strings. Where exactly did you print the variables with the data in them?

well, it may not be ALL the code, but in what you provided, you only have cout statements with constant strings. Where exactly did you print the variables with the data in them?


In the main function, and thats all my files (7)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Professor professor1;
    Student student1;


    // call members of the base class
    professor1.name();
    professor1.Setname("Ms.Puff");

    student1.name();
    student1.Setname("Spongebob");



    // call members from the derived class
    professor1.office();
    professor1.Settype("The Bikini Bottom");

    student1.courses();
    student1.Settype("Driving Lessons, Cooking");
Last edited on
In Professor::displayInfo you print a sentence ... and then forget to actually print the name.

It's also pointless having an argument for that function. It isn't used, because the professor does know his/her own name etc. That's the idea of object-oriented programming.
As one file for ease of compilation, consider as a starter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
using namespace std;

class Person // parent class
{
public:
	Person() {}
	Person(const string& nam);

	string GetName() const { return m_name; }
	void SetName(const string& val) { m_name = val; }

private:
	string m_name;
};

Person::Person(const string& nam) : m_name(nam) {}

class Professor : public Person // professor is child class
{
public:
	Professor() {};
	Professor(const string& nam, const string& off);

	void displayInfo() const;
	string GetOffice() const { return m_office; }
	void SetOffice(const string& val) { m_office = val; }
	void office() const;

private:
	string m_office;
};

Professor::Professor(const string& nam, const string& off) : Person(nam), m_office(off) {}

void Professor::office() const
{
	cout << "The professor is located in office: " << GetOffice() << '\n';
}

void Professor::displayInfo() const
{
	cout << "The professors name is: " << GetName() << '\n';
	cout << "Their office is located in: " << GetOffice() << '\n';
}

class Student : public Person // student is child class
{
public:
	Student() {};
	Student(const string& nam, const string& course);
	string GetCourse() const { return m_courses; }
	void SetCourse(const string& val) { m_courses = val; }

	void courses() const;
	void displayInfo() const;

private:
	string m_courses;
};

Student::Student(const string& nam, const string& course) : Person(nam), m_courses(course) {}

void Student::courses() const
{
	cout << "The student is taking these courses:  " << GetCourse() << '\n';
}

void Student::displayInfo() const
{
	cout << "The students name is: " << GetName() << '\n';
	cout << "The courses they're attending are: " << GetCourse() << '\n';
}

int main()
{
	Professor professor1 ("Ms. Puff", "The Bikini Bottom");
	Student student1("Spongebob", "Driving Lessons, Cooking");

	professor1.displayInfo();
	student1.displayInfo();
}



The professors name is: Ms. Puff
Their office is located in: The Bikini Bottom
The students name is: Spongebob
The courses they're attending are: Driving Lessons, Cooking

Last edited on
As one file for ease of compilation, consider as a starter:


Thank you seeplus! I kept my format, but your code helped me see what I did wrong, it was basically putting some of the child class data in the parent.
Topic archived. No new replies allowed.