Class Inheritance

Class Inheritance - Lecture 7: Class Inheritance CS256 Fall...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 7: Class Inheritance CS256 – Fall 2008
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Inheritance Purpose: Code reuse Concepts If a new class is to be implemented while a class representing a more general concept is already available, the new class can inherit from the existing class. Base class: the general class Derived class: the more specialized class Example: a Manager class can inherit from the Employee class
Background image of page 2
Syntax Base class No change, as it’s already defined Derived class class DerivedClassName : public BaseClassName { //features //i.e. new members that belong to base class only };
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example Base Class class Employee class as previously defined Derived Class class Manager : public Employee { public: Manager (string name, double salary, string dept_name); string get_department(); private: string dept; };
Background image of page 4
Calling the Base Constructor Syntax DerivedClassName::DerivedClassName(expr) : BaseClassName(expr) { statements } Example Manager::Manager(string name, double salary, string dept_name) : Employee (name, salary) { dept = dept_name; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Using the Derived Class int main() { Employee e (“David”, 45000); Manager m (“John”, 85000, “BIO”); cout << “Empolyee “ << e.get_name() << “Earns salary “ << e.get_salary() << “a year \n”; cout << “Manager “ << m.get_name() << “who works in “ << m.get_department() << “Earns salary” << m.get_salary() << “a year\n”; return 0; } // an object of a derived class may call a member function of // its own or call a member function of its base class
Background image of page 6
Inheriting Base-Class Member Function For a function f() defined in the base class B, the derived class can inherit f (i.e. B::f()) by not defining a function with the same name f in the derived class Example: Manager m (“John”, 85000, “BIO”); m.set_salary(90000); //note: set_salary is defined in Employee’s class m.Employee::set_salary(90000); //same, but more specific
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Replacing Base-Class Member Function The derived class may replace B::f() by supplying a new implementation D::f() that is unrelated to B::f() class Manager :: public Employee { public: double get_salary() {return BONUS; } //assume BONUS is a constant };
Background image of page 8
Scope Resolution Operator If a derived class redefines a base class’ function, may use scope resolution operator :: to distinguish which one to call Example Manager m ( …); cout << m.Employee::get_salary(); //without bonus cout << m.get_salary(); //Bonus only cout << m.Manager::get_salary(); //same as with Bonus only, but more specific
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The derived class can extend B::f() by supplying a new implementation D::f() that calls B::f(). Example
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 43

Class Inheritance - Lecture 7: Class Inheritance CS256 Fall...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online