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?