Previous Lecture Lecture 9 Next Lecture

Lecture 9, Thu 05/03

Inheritance and Polymorphism

Inheritance

Example (Person / Student Inheritance)

// Person.h
#ifndef PERSON_H
#define PERSON_H
class Person {
public:
	Person(std::string name, int age);
	std::string getName();
	int getAge();
	void setName(std::string name);
	void setAge(int age);
private:
	std::string name;
	int age;
};
#endif
// Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include "Person.h"

class Student : public Person {
public:
	Student(std::string name, int age, int studentId);
	int getStudentId();
	void setStudentId(int id);
private:
	int studentId;
};
#endif
// Person.cpp
#include <string>
#include "Person.h"

using namespace std;

Person::Person(string name, int age) {
	this->name = name;
	this->age = age;
}

string Person::getName() { return name; }

int Person::getAge() { return age; }

void Person::setName(string name) { this->name = name; }

void Person::setAge(int age) { this->age = age; }
---------
// Student.cpp
#include <string>
#include "Student.h"

using namespace std;

Student::Student(string name, int age, int id) : Person(name, age){
	studentId = id;
}

int Student::getStudentId() { return studentId; }

void Student::setStudentId(int id) { studentId = id; }

Note:

Note on constructors and inheritance

Redefining inherited functions

// in Student.h
std::string getName();

// in Student.cpp
string Student::getName() {
	return "STUDENT: " + name; //ERROR, name isn’t inherited
	return "STUDENT: " + Person::getName; // OK!
} 

Inheritance Types

Person p1(“Chris Gaucho”, 21);
Student s1(“John Doe”, 22, 12345678);

Person p2  = s1; // Legal, a student is a person
Student s2 = p1; // illegal, a person may not be a student

Memory Slicing

cout << p2.getName() << endl;
cout << p2.getAge() << endl;
cout << p2.getStudentId() << endl; // ERROR! p2 doesn’t have studentID

Pointers of base types

Person* p1 = new Person("R1", 10);
Student* s1 = new Student("JD", 21, 1234567);

// Student* s2 = p1; 	// Illegal!
Person* p2 = s1;		// OK.
cout << p2->getName() << endl;
cout << p2->getAge() << endl;
cout << p2->getStudentId() << endl; // illegal

Destructors and Inheritance

Example

// in Student.h
~Student();
-----
// in Student.cpp
Student::~Student() {
	cout << "in Student Destructor" << endl;
}
-----
// in Person.h
~Person();
-----
// in Person.cpp
Person::~Person() {
	cout << "in Person destructor" << endl;
}
-----
// in main.cpp
Person* p1 = new Person("R1", 10);
Student* s1 = new Student("JD", 21, 1234567);
delete p1;
cout << "---" << endl;
delete s1;