inheritance testing project

Hi there. I am trying to learn classes and inheritance, but I am missing something here. I've created two classes and have to prove inheritance between the parent class and the child class. I need to be able to access the data elements from the parent class in the child class and just display to prove inheritance. Simple as it sounds, I'm missing something here. I'm getting linking errors. Not trying to get anyone to do my homework, but if someone could explain what I'm doing wrong here, I would appreciate it.

Here's my code so far:

#include<iostream>
#include<string>


using namespace std;

class Person{

public:
Person();
Person(string theName, string theAddress, string theCity, string theState, int theZip, string thePhone);
~Person();
string getName()const;
protected:
string name;
string address;
string city;
string state;
int zip;
string phone;
};
Person::Person(string, string, string, string, int, string){
name="Mary";
address="99999 1st Street";
city="Tampa";
state="FL";
zip=33333;
phone="813-555-9999";
}

string Person::getName() const{
cout<<name<<" "<<address<<", "<<city<<", "<<state<<" "<<zip;
return name,address,city,state,zip,phone;
}


class Student:public Person{

public:
Student();
Student(string name, string address, string city, string state, int zip, string phone, char grade, string course, string gpa);
string getStudent() const;

protected:
char grade;
string course;
string gpa;

};
Student::Student(string name, string address, string city, string state, int zip, string phone, char grade, string course, string gpa){
grade='A';
course="Programming 2";
gpa="3.89";

}
string Student::getStudent() const{

return grade,course,gpa;
}




int main()
{

cout<<&Student::getStudent;

system("pause");
return 0;
}

You declare but don't define a default constructor for Student.

You don't have an object of the type student.

You can't return multiple values:
1
2
3
4
string Student::getStudent() const{

return grade,course,gpa;
}

If you need to, use an array or a vector.

If you're not going to use any of the values passed to the constructor then don't accept parameters.


I took your code and made some changes to make it compile and show inheritance.

Since Person is the base class it can only see elements within itself. Student inherits from Person so it can see itself and all elements within Person. I hope this code helps you learn how to get classes properly set up. If you need more help feel free to contact me.

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

using namespace std;

class Person{
public:
	Person();
	Person(string theName, string theAddress, string theCity, string theState, int theZip, string thePhone);
	//~Person();
	void printPerson()const;
protected:
	string name;
	string address;
	string city;
	string state;
	int zip;
	string phone;
};

Person::Person(string theName, string theAddress, string theCity, string theState, int theZip, string thePhone){
	name=theName;
	address=theAddress;
	city=theCity;
	state=theState;
	zip=theZip;
	phone=thePhone;
}

void Person::printPerson() const{

	cout<<name<<" "<<address<<", "<<city<<", "<<state<<" "<<zip<<endl;

}


class Student:public Person{

public:
	Student();
	Student(string name, string address, string city, string state, int zip, string phone, char grade, string course, string gpa);
	void printStudent() const;

protected:
	char grade;
	string course;
	string gpa;

};

Student::Student(string theName, string theAddress, string theCity, string theState, int theZip, string thePhone, char theGrade, string theCourse, string theGpa) 
: Person(theName, theAddress, theCity, theState, theZip, thePhone){
	grade=theGrade;
	course=theCourse;
	gpa=theGpa;

}

void Student::printStudent() const{

	printPerson();
	cout<<grade<<" "<<course<<" "<<gpa<<endl;
}

int main()
{
	Person person("karen", "3452 Ave.", "Somewhere", "FL", 83946, "123 456 7890");
	Student student("tony","1111 Ave", "Anywhere", "TX", 74934, "924 654 8789", 'A', "Programming", "4.0");

	person.printPerson();
	//person.printStudent(); Won't work

	student.printStudent();
	student.printPerson();

	system("pause");
	return 0;
}


karen 3452 Ave., Somewhere, FL 83946

tony 1111 Ave, Anywhere, TX 74934
A Programming 4.0

tony 1111 Ave, Anywhere, TX 74934

Press any key to continue . . .
Last edited on
Thank you both, very much. That makes a lot of sense, especially seeing it written out.
Topic archived. No new replies allowed.