Inheritance

I have a homework assignment about inheritance. When I do it with just one .cpp file everything works the way I want it. When I make a .cpp or .h file with the class etc I get compile errors with dev c++ (something about .pdata) and if I do the same exact thing with Visual Studio 2010 it says it cannot recognize cout. All I tried to do it all under one .cpp file and I can get it to work but not when I have header files or separate .cpp files.

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
  #include <iostream>
#include<string>
#include<cstdlib>


using namespace std;


int main()
{
	
	cout << lastName.getlastName() << endl;
	cout << firstName.getfirstName() << endl;
	cout << middleInitial.getmiddleInitial() << endl;
	cout << address.getaddress() << endl;
	cout << city.getcity() << endl;
	cout << state.getstate() << endl;
	cout << zip.getzip() << endl;
	cout << phone.getphone() << endl;
	cout << Course.getCourse() << endl;
	cout << Grade.getGrade() << endl;
	cout << GPA.getGPA() << endl;	
	
	
	return 0;
}


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
#include <iostream>
#include<string>
#include "Person.h"
#include "Student.h"


Person::Person();
{
	
}

void Person::getlastName()
{
	lastName.setlastName("Smith");
}

void Person::getfirstName()
{
	firstName.setfirstName("Billy");
}

void Person::getmiddleInitial()
{
	middleInitial.setmiddleInitial("B");
}
	
void Person::getAddress()
{
	address.setAddress("8134 Hick Ln");
}		
	
void Person::getCity()
{
	city.setCity("Tuscaloosa");
}	
	
void Person::getState()
{
	state.setState("Alabama");
}	
	
void Person::getZip()
{
	zip.setZip(23179);
}	
	
void Person::getPhone()
{
	phone.setPhone(2066598965);
}



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
84
85
86
#ifndef PERSON_H
#define PERSON_H

class Person

{

	public:
		
		
		void setlastName(string x)
		{
			lastName = x;
		}
		string getlastName()
		{
			return lastName;
		}
		
		void setfirstName(string x)
		{
			firstName = x;
		}
		string getfirstName()
		{
			return firstName;
		}
		
		void setmiddleInitial(string x)
		{
			middleInitial = x;
		}
		string getmiddleInitial()
		{
			return middleInitial;
		}
		
		void setaddress(string x)
		{
			lastName = x;
		}
		string getaddress()
		{
			return lastName;
		}
		
		void setcity(string x)
		{
			city = x;
		}
		string getcity()
		{
			return city;
		}
		
		void setstate(string x)
		{
			state = x;
		}
		string getstate()
		{
			return state;
		}
		
		void setzip(int x)
		{
			zip = x;
		}
		int getzip()
		{
			return zip;
		}
		
		void setphone(int x)
		{
			phone = x;
		}
		int getphone()
		{
			return phone;
		}
};

		
};
#endif 



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
#include <iostream>
#include<string>
#include "Person.h"
#include "Student.h"

Student::Student();
{
 	
}

void Student::getCourse()
{
	Course.setCourse("Chemistry");
}

void Student::getGrade()
{
	Grade.setGrade("C");
}

void Student::getGPA()
{
	GPA.setGPA(2.7);
}
	
	
	

	




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
#ifndef STUDENT_H
#define STUDENT_H

class Student
{

	public:
		void setGrade(string x)
		{
			Grade = x;
		}
		string getGrade()
		{
			return Grade;
		}
		
		void setCourse(string x)
		{
			Course = x;
		}
		string getCourse()
		{
			return Course;
		}
		
		void setGPA(double x)
		{
			GPA = x;
		}
		double getGPA()
		{
			return GPA;
		}
};

#endif 


Homework assignment description.


Create a class person, which has a name, address, city, state, zip and phone number, with all the proper constructors, and public accessor functions, like “string getName(){return name;}” for all the data members.
Create a class student, which inherits from person, and has additionally grade, course, and GPA, with all proper constructors and public accessor functions. Let the constructor take both the student’s variables and its parent’s variables. You can use the technique given in the text book page 343 “Calling Base Class Constructors” or “initializer list” to initialize the base class data members.
Test your classes in the main function to prove that your inheritance is working. Create an object of student class with constructor argument values. Print the variables that are inherited from the parent class using the accessor functions defined in the parent class. Or, you can use a display function.

Why is this not working?
Several things:

1.) In your
person.h
file, you have an unneccesary closing brace and semi-colon on line 85.

2.) Your main file (in which the main() function driver program is contained) does not include any headers, so there's no way it could know about any of your classes.

3.) Lines 12 - 22 of your main() function attempt to use objects that you never declared. Judging by the methods you attempt to call on those objects, that would mean that "lastName" is the identifier of a person or student object? Same goes for all the other ones.

4.) Your student class doesn't inherit from your person class, like the assignment asks you to do.

5.) Your get functions should return the members, not set them. The instructions mention nothing about set methods.

6.) The student class should have a constructor which takes enough parameters to initialize all of its members - even the ones it inherits from the person class. If you use the initializer list to initialize the members as the instructions suggest, you'll have to write a constructor for the person class as well which takes those parameters.
Last edited on
Do I put the set in the header or .cpp section. The number #5 and #6 is what I am confused on.

If I understand you correctly instead of the way I have it just put it something like this?

void Person::setlastName("Smith") or is there a way I do it differently?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include<string>
#include<cstdlib>
#include "Person.h"
#include "Student.h"

using namespace std;


int main()
{
	

	
	
	return 0;
}


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
#include <iostream>
#include<string>
#include "Person.h"
#include "Student.h"


Person::Person()
{
	
}

void Person::setlastName()
{
	lastName.setlastName("Smith");
}

void Person::setfirstName()
{
	firstName.setfirstName("Billy");
}

void Person::setmiddleInitial()
{
	middleInitial.setmiddleInitial("B");
}
	
void Person::setAddress()
{
	address.setAddress("8134 Hick Ln");
}		
	
void Person::setCity()
{
	city.setCity("Tuscaloosa");
}	
	
void Person::setState()
{
	state.setState("Alabama");
}	
	
void Person::setZip()
{
	zip.setZip(23179);
}	
	
void Person::setPhone()
{
	phone.setPhone(2066598965);
}


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
#ifndef PERSON_H
#define PERSON_H

class Person
{
	public:
		
		void setlastName(string x)
		{
			lastName = x;
		}
		string getlastName()
		{
			return lastName;
		}
		
		void setfirstName(string x)
		{
			firstName = x;
		}
		string getfirstName()
		{
			return firstName;
		}
		
		void setmiddleInitial(string x)
		{
			middleInitial = x;
		}
		string getmiddleInitial()
		{
			return middleInitial;
		}
		
		void setaddress(string x)
		{
			lastName = x;
		}
		string getaddress()
		{
			return lastName;
		}
		
		void setcity(string x)
		{
			city = x;
		}
		string getcity()
		{
			return city;
		}
		
		void setstate(string x)
		{
			state = x;
		}
		string getstate()
		{
			return state;
		}
		
		void setzip(int x)
		{
			zip = x;
		}
		int getzip()
		{
			return zip;
		}
		
		void setphone(int x)
		{
			phone = x;
		}
		int getphone()
		{
			return phone;
		}
};

#endif 


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
#include <iostream>
#include<string>
#include "Person.h"
#include "Student.h"

Student::Student();
{
 	
}

void Student::setCourse()
{
	Course.setCourse("Chemistry");
}

void Student::setGrade()
{
	Grade.setGrade("C");
}

void Student::setGPA()
{
	GPA.setGPA(2.7);
}


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
#ifndef STUDENT_H
#define STUDENT_H

class Student: Public Person
{

	public:
		void setGrade(string x)
		{
			Grade = x;
		}
		string getGrade()
		{
			return Grade;
		}
		
		void setCourse(string x)
		{
			Course = x;
		}
		string getCourse()
		{
			return Course;
		}
		
		void setGPA(double x)
		{
			GPA = x;
		}
		double getGPA()
		{
			return GPA;
		}
};

#endif 

1.) The assignment only tells you to make accessor methods (get), it says nothing about mutator methods (set). Therefore, it's likely that adding set methods to your class, like you have, will take its toll on your grade.

2.) Here is what a get method might look like:

1
2
3
std::string Student::getLastName() {
	return lastName;
}


3.) Here is what a constructor might look like:

1
2
3
4
5
6
7
8
9
10
Person::Person(std::string _firstName, std::string _lastName) {
	firstName = _firstName;
	lastName = _lastName;
}

class Student {
	public :

	Student(std::string _firstname, std::string _lastName, int _GPA) : Person(_firstName, _lastName) {GPA = _GPA;}
	/*...*/


*EDIT* Also, why don't your classes have any actual data members? You have functions that attempt to modify non-existent data members. Additionally, you attempt to redefine member functions in several different places - where the definitions don't even match. Do you need help understanding classes and functions?
Last edited on
In essence yes. I've read all about them in the class and watched bucky's c++ tutorials and it's still not sinking in. From what I understand I put the constructors in the .cpp and the methods in the header file? Like this.
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include<string>
#include<cstdlib>
#include "Person.h"
#include "Student.h"

using namespace std;


int main()
{
	Person Smith;
	Smith.lastName();
	

	
	
	return 0;
}


	

Person.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PERSON_H
#define PERSON_H

class Person

{
	public:
		Person();
		std::string Student::getLastName() 
		{
			return lastName;
		}
		
};

#endif 


person.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include<string>
#include "Person.h"
#include "Student.h"

using namespace std;

Person::Person()

Person::Person(std::string_lastName)

{
	lastName = _lastName
	
}	

student.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef STUDENT_H
#define STUDENT_H

class Student: Public Person
{
	public:
		Student();	
	
};

#endif 

student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include<string>
#include "Person.h"
#include "Student.h"

using namespace std;

Student::Student();
public:
{
	 	
}
	
So I was going back through some old assignments and I think this is what is wanted but in it's own header/.cpp file.
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
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Student
{
	friend void Name(const Student& aStudent);
	friend ostream& operator <<(ostream& os, const Student& aStudent);
	
	private:
	string lastName;		//data member
	string firstName;		//data member
	string middleName;		//data member
	string currentGrade;	//data member
	string Course;			//data member
	int studentNumber;		//data member
	double GPA;				//data member
	string collegeName;
	string courseName;
	
	public:
	Student()
	{
		lastName = "Smith";
		firstName = "Sam";
		middleName = "Steve";
		currentGrade = "A-";
		Course = "Chemistry";
		studentNumber = 24680;
		GPA = 2.9;
	}
	Student(string lastName, string firstName, string middleName, string currentGrade, string Course, int studentNumber, double GPA)
	{
		this->lastName = lastName;
		this->firstName = firstName;
		this->middleName = middleName;
		this->currentGrade = currentGrade;
		this->Course = Course;
		this->studentNumber = studentNumber;
		this->GPA = GPA;
	}

	

	
	void Display()
	{
		cout << "This is your students information. "  << endl;
		cout << " Last name is " << lastName << endl;
		cout << " First name is " << firstName << endl;
		cout << " Middle initial is " << middleName << endl;
		cout << " Current grade is " << currentGrade << endl;
		cout << " Current course is " << Course << endl;
		cout << " The student number is " << studentNumber << endl;
		cout << " The students current GPA is " << GPA << endl;
	};
};


int main()
{
	Student student1;
	Student student2("Turnham","James","D", "A-","Programming II",12345,3.5);
	
	student1.Display();
	student2.Display();
	
	
	
	return 0;
}


I've got it down but now my display function doesn't work. What can be done so that the display function works for both classes?

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Person
{

	public:
		
	string lastName;		//data member
	string firstName;		//data member
	string middleName;		//data member
	string Address;
	string City;
	string State;
	int Zip;
	int Phone;
		
	Person()
	{
		lastName = "Smith";
		firstName = "Sam";
		middleName = "Steve";
		Address = "2586 Billy Bob Ln";
		City = "Tuscaloosa";
		State = "AL";
		Zip = 26549;
		Phone = 2136546578;		
		
	}
	Person(string lastName, string firstName, string middleName, string Address, string City, string State, int Zip, int Phone)
	{
		this->lastName = lastName;
		this->firstName = firstName;
		this->middleName = middleName;
		this->Address = Address;
		this->City = City;
		this->State = State;
		this->Zip = Zip;
		this->Phone = Phone;
		
	}
	void Display()
	{
		cout << "This is your Persons information. "  << endl;
		cout << " Last name is " << lastName << endl;
		cout << " First name is " << firstName << endl;
		cout << " Middle initial is " << middleName << endl;
		cout << " Address is " << Address << endl;
		cout << " City is  " << City << endl;
		cout << " State is  " << State << endl;
		cout << " Zip is  " << Zip << endl;
		cout << " Phone is  " << Phone << endl;
	};
};
	
	class Student: public Person
	{
	
	public:
		string currentGrade;	//data member
		string Course;			//data member
		double GPA;				//data member
		
	Student()	
		{
			currentGrade = "A-";
			Course = "Chemistry";
			GPA = 2.9;
		}
	
	Student(string lastName, string firstName, string middleName, string Address, string City, string State, int Zip, int Phone, string currentGrade, string Course, double GPA)
	{
		this->lastName = lastName;
		this->firstName = firstName;
		this->middleName = middleName;
		this->Address = Address;
		this->City = City;
		this->State = State;
		this->Zip = Zip;
		this->Phone = Phone;
		this->currentGrade = currentGrade;
		this->Course = Course;
		this->GPA = GPA;
	};
		void Display()
	{
		cout << "This is your Persons information. "  << endl;
		cout << " Last name is " << lastName << endl;
		cout << " First name is " << firstName << endl;
		cout << " Middle initial is " << middleName << endl;
		cout << " Current grade is " << currentGrade << endl;
		cout << " Current course is " << Course << endl;
		cout << " The students current GPA is " << GPA << endl;
	};
};
	


int main()
{
	
	Person person();
	Student student();
	
	person.Display();
	student.Display();
	
	return 0;
}
On lines 105 and 106, you are creating two function prototypes - functions which return Person and Student objects respectively. That's not what you want, so remove the parentheses.
Thank you. I knew it would be something basic.
Topic archived. No new replies allowed.