Inheritance: (code reuse)
parent class --> child class
base type --> sub type
Polymorphism
static polymorphism ==> function overloading, operator overloading
dynamic polymorphism ==> function overriding (virtual keyword, virtual function)
College:
common to both student and professor ==> id, name, address, displayProfile(), changeAddress()
professor ==> id, name, address, rank, displayProfile(), changeAddress(), promote()
student ==> id, name, address, course, year, displayProfile(), changeAddress(), changeYear(), changeCourse()
College:
SASPerson ==> parent class (id, name, address, displayProfile(), changeAddress())
Child classes:
professor ==> rank, promote()
student ==> course, year, changeYear(), changeCourse()
//Inheritance
#include <iostream>
#include <string>
using namespace std;
//------------parent class----------
class SASPerson
{
protected:
int id;
string name;
string address;
public:
SASPerson(int id, string name, string address);
void displayProfile();
void changeAddress(string newAddress);
};
SASPerson::SASPerson(int id, string name, string address)
{
this->id = id;
this->name = name;
this->address = address;
}
void SASPerson::displayProfile()
{
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Address: " << address << endl << endl;
}
void SASPerson::changeAddress(string newAddress)
{
address = newAddress;
}
//----------child class------------
class Student : public SASPerson
{
int course;
int year; // 1 = freshman, 2 = sophomore, etc.
public:
Student(int id, string name, string address, int course, int year);
void changeCourse(int newCourse);
void changeYear (int newYear);
};
Student::Student(int id, string name, string address, int course, int year) : SASPerson(id, name, address)
{
this->course = course;
this->year = year;
}
void Student::changeCourse(int newCourse)
{
course = newCourse;
}
void Student::changeYear(int newYear)
{
year = newYear;
}
//-----main()------------------------
int main()
{
SASPerson* john = new SASPerson(901289, "John Doe", "500 Massachusetts Ave.");
Student* james = new Student(971232, "James Lee", "32 Vassar St.", 6, 2);
john->displayProfile();
james->displayProfile();
return 0;
}
Output:
Name: John Doe
ID: 901289
Address: 500 Massachusetts Ave.
Name: James Lee
ID: 971232
Address: 32 Vassar St.
Problem: as child is using function displayProfile() coming from parent, we could not display child specific data course and year.
solution: child can override function displayProfile() coming from parent
//Inheritance
#include <iostream>
#include <string>
using namespace std;
//------------parent class----------
class SASPerson
{
protected:
int id;
string name;
string address;
public:
SASPerson(int id, string name, string address);
void displayProfile();
void changeAddress(string newAddress);
};
SASPerson::SASPerson(int id, string name, string address)
{
this->id = id;
this->name = name;
this->address = address;
}
void SASPerson::displayProfile()
{
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Address: " << address << endl << endl;
}
void SASPerson::changeAddress(string newAddress)
{
address = newAddress;
}
//----------child class------------
class Student : public SASPerson
{
int course;
int year; // 1 = freshman, 2 = sophomore, etc.
public:
Student(int id, string name, string address, int course, int year);
void displayProfile();
void changeCourse(int newCourse);
void changeYear (int newYear);
};
Student::Student(int id, string name, string address, int course, int year) : SASPerson(id, name, address)
{
this->course = course;
this->year = year;
}
void Student::displayProfile()
{
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Address: " << address << endl;
cout << "Course: " << course << endl;
cout << "Year: " << year << endl << endl;
}
void Student::changeCourse(int newCourse)
{
course = newCourse;
}
void Student::changeYear(int newYear)
{
year = newYear;
}
//-----main()------------------------
int main()
{
SASPerson* john = new SASPerson(901289, "John Doe", "500 Massachusetts Ave.");
Student* james = new Student(971232, "James Lee", "32 Vassar St.", 6, 2);
john->displayProfile();
james->displayProfile();
return 0;
}
Output:
Name: John Doe
ID: 901289
Address: 500 Massachusetts Ave.
Name: James Lee
ID: 971232
Address: 32 Vassar St.
Course: 6
Year: 2
//calling parent class contructor in child constructor
Student::Student(int id, string name, string address, int course, int year) : SASPerson(id, name, address)
{
this->course = course;
this->year = year;
}
Student::Student(int id, string name, string address, int course, int year)
{
this->id = id;
this->name = name;
this->address = address;
this->course = course;
this->year = year;
}
virtual keyword use:
#include <iostream>
#include <string>
using namespace std;
//------------parent class----------
class SASPerson
{
protected:
int id;
string name;
string address;
public:
SASPerson(int id, string name, string address);
void displayProfile();
void changeAddress(string newAddress);
};
SASPerson::SASPerson(int id, string name, string address)
{
this->id = id;
this->name = name;
this->address = address;
}
void SASPerson::displayProfile()
{
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Address: " << address << endl << endl;
}
void SASPerson::changeAddress(string newAddress)
{
address = newAddress;
}
//----------child class------------
class Student : public SASPerson
{
int course;
int year; // 1 = freshman, 2 = sophomore, etc.
public:
Student(int id, string name, string address, int course, int year);
void displayProfile();
void changeCourse(int newCourse);
void changeYear (int newYear);
};
Student::Student(int id, string name, string address, int course, int year) : SASPerson(id, name, address)
{
this->course = course;
this->year = year;
}
void Student::displayProfile()
{
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Address: " << address << endl;
cout << "Course: " << course << endl;
cout << "Year: " << year << endl << endl;
}
void Student::changeCourse(int newCourse)
{
course = newCourse;
}
void Student::changeYear(int newYear)
{
year = newYear;
}
//-----main()------------------------
int main()
{
SASPerson* steve = new Student(911923, "Steve", "99 Cambridge St.", 18, 3);
steve->displayProfile();
return 0;
}
output:
Name: Steve
ID: 911923
Address: 99 Cambridge St.
Note:
though actual type is Student, the override function displayProfile() is executed from parent class SASPerson. that means override function call done as per declaration type SASPerson.
if we want override function call based actual type, we have to use virtual keyword for function declaration in parent class.
#include <iostream>
#include <string>
using namespace std;
//------------parent class----------
class SASPerson
{
protected:
int id;
string name;
string address;
public:
SASPerson(int id, string name, string address);
virtual void displayProfile();
void changeAddress(string newAddress);
};
SASPerson::SASPerson(int id, string name, string address)
{
this->id = id;
this->name = name;
this->address = address;
}
void SASPerson::displayProfile()
{
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Address: " << address << endl << endl;
}
void SASPerson::changeAddress(string newAddress)
{
address = newAddress;
}
//----------child class------------
class Student : public SASPerson
{
int course;
int year; // 1 = freshman, 2 = sophomore, etc.
public:
Student(int id, string name, string address, int course, int year);
void displayProfile();
void changeCourse(int newCourse);
void changeYear (int newYear);
};
Student::Student(int id, string name, string address, int course, int year) : SASPerson(id, name, address)
{
this->course = course;
this->year = year;
}
void Student::displayProfile()
{
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Address: " << address << endl;
cout << "Course: " << course << endl;
cout << "Year: " << year << endl << endl;
}
void Student::changeCourse(int newCourse)
{
course = newCourse;
}
void Student::changeYear(int newYear)
{
year = newYear;
}
//-----main()------------------------
int main()
{
SASPerson* steve = new Student(911923, "Steve", "99 Cambridge St.", 18, 3);
steve->displayProfile();
return 0;
}
output:
Name: Steve
ID: 911923
Address: 99 Cambridge St.
Course: 18
Year: 3
Note: overridden function displayProfile() is called based on actual type due to virtal keyword used.
pure virtual function:
virtual void displayProfile() = 0;
class having atleast one pure virtual function is called virtual class. we can NOT create objects for virtual class, can only be used to derive child class.
Child class has to define pure virtual functions coming from parent, otherwise child class also will become virtual class.
#include <iostream>
#include <string>
using namespace std;
//------------parent class----------
class SASPerson
{
protected:
int id;
string name;
string address;
public:
SASPerson(int id, string name, string address);
virtual void displayProfile() = 0;
void changeAddress(string newAddress);
};
SASPerson::SASPerson(int id, string name, string address)
{
this->id = id;
this->name = name;
this->address = address;
}
void SASPerson::changeAddress(string newAddress)
{
address = newAddress;
}
//----------child class------------
class Student : public SASPerson
{
int course;
int year; // 1 = freshman, 2 = sophomore, etc.
public:
Student(int id, string name, string address, int course, int year);
void displayProfile();
void changeCourse(int newCourse);
void changeYear (int newYear);
};
Student::Student(int id, string name, string address, int course, int year) : SASPerson(id, name, address)
{
this->course = course;
this->year = year;
}
void Student::displayProfile()
{
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Address: " << address << endl;
cout << "Course: " << course << endl;
cout << "Year: " << year << endl << endl;
}
void Student::changeCourse(int newCourse)
{
course = newCourse;
}
void Student::changeYear(int newYear)
{
year = newYear;
}
//-----main()------------------------
int main()
{
//SASPerson s(911923, "Steve", "99 Cambridge St."); // objects for class SASPerson not allowed bcz its virtal class
Student s(911923, "Steve", "99 Cambridge St.", 18, 3); // allowed, because Student class is not virtual class
return 0;
}
//single inheritance
#include <iostream>
using namespace std;
class A
{
public:
int i;
};
class B : public A
{
public:
int j;
};
int main()
{
B b;
b.j = 10;
b.i = 20;
cout <<"b.i="<<b.i<<","<<"b.j="<<b.j<<endl;
return 0;
}
//multiple inheritance
#include <iostream>
using namespace std;
class A
{
public:
int i;
};
class B
{
public:
int j;
};
class C : public A, public B
{
public:
int k;
};
int main()
{
C c;
c.i = 10;
c.j = 20;
c.k = 30;
cout <<"c.i="<<c.i<<","<<"c.j="<<c.j<<","<<"c.k="<<c.k<<endl;
return 0;
}