Anda di halaman 1dari 36

Session 11

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 1 of 36

Session Objectives
Describe Single Inheritance Describe Base class and Derived class Access Base class members and use pointers in classes Describe types of inheritance Describe Constructors and Destructors under inheritance Describe how to call Member Functions of the Base Class and Derived Class Describe Container Classes
Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 2 of 36

Single Inheritance (1)


Single inheritance is the process of creating new classes from an existing base class.
Employee

Director

Manager

Secretary

Clerk

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 3 of 36

Single Inheritance (2)


Class: Employee Name, Age, Emp_id Salary, Department

The derived classes inherit the methods and variables of the base class. They can also have properties and methods of their own.

Class: Manager m_name, m_age, m_Emp_id, m_salary m_department perks, no_of_employees reporting

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 4 of 36

Advantages
Most important advantage: reusability of code. Result of reusability of code is the development of class libraries.


A class library consists of data and methods encapsulated in a class. Deriving a class from an existing one allows redefining a member function of the base class and also adding new members to the derived class. The base class remains unchanged in the process.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 5 of 36

Base Class and Derived Class


Employee Base class

Declaration of a singly derived class is similar to that of any ordinary class, except that the name of the base class also needs to be specified.
class Manager : public Employee

Derived from Manager Derived class

A base class can be classified into two types:


 

direct base indirect base

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 6 of 36

Direct and Indirect Base


A base class is called direct if it is mentioned in the base list.
class A { }; class B : public A { }; // where class A is a direct class.

An indirect class can be written as:


class A { }; class B : public A { }; class C : public B { }; // Here class A is a indirect class.
Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 7 of 36

Accessibility (1)
Knowing when a member function or data member of a base class can be used by objects of the derived class.


Class members can always be accessed by member functions within their own class, whether the members are private or public. Objects defined outside the class can access class members only if the members are public.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 8 of 36

Accessibility (2)
With inheritance:


Derived class members can access members of the base class if its members are public. Derived class members cannot access the private members of the base class.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 9 of 36

Protected Access Specifier


The protected section is like the private section in terms of scope and access.


Protected members can be accessed only by members of that class. Protected members cannot be accessed by objects or functions from outside the class, such as main(). Protected members is accessible in the derived class.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 10 of 36

Accessing Base Class Members


Members of the derived class can access public and protected members; they cannot access the private members of the base class. No access to some of the class members. Those members can be put in the private section. Allow controlled access by providing some protected members. Inheritance does not work in reverse.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 11 of 36

Access rules for Base class members (1)

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 12 of 36

Access rules for Base class members (2)


class Employee{ private: int privA; protected: int protA; public: int pubA; }; //base class

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 13 of 36

Access rules for Base class members (3)


//derived class class Manager : public Employee{ public: void fn() { int a; a = privA; //error:not accessible a = protA; //valid a = pubA; //valid } };

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 14 of 36

Access rules for Base class members (4)


void main() { Employee emp; emp.privA = 1; emp.protA = 1; emp.pubA = 1; Manager mgr; mgr.privA = 1; mgr.protA = 1; mgr.pubA = 1; }

//base class object //error:not accessible //error:not accessible //valid //derived class object //error:not accessible //error:not accessible //valid

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 15 of 36

Pointers in classes (1)


We can use a pointer, which has been declared to point to one class, to actually refer to another class. If a derived class has a public base class, then a pointer to the derived class can be assigned to a variable of type pointer to the base. An object of a derived class can be treated as an object of its base class when manipulated through pointers. However, the opposite is not true.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 16 of 36

Pointers in classes (2)


void main() { Manager mgr; Employee* emp = &mgr; //valid:every Manager is an Employee Employee eml; Manager* man = &eml; //error: not every Employee is a Manager }

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 17 of 36

Types of Inheritance (1)


A derived class can be declared with one of the specifiers - public, private and protected. The keyword public in the class declaration of the derived class specifies that objects of the derived class are able to access public member functions of the base class. With the keyword private in the derived class declaration, objects of the derived class in main() cannot even access public member functions of the base class.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 18 of 36

Types of Inheritance (2)


//base class class A{ private: int privA; protected: int protA; public: int pubA; }; class B : public A{ //publicly derived class public: void fn(){ int a; a = privA; //error:not accessible a = protA; //valid a = pubA; //valid } };
Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 19 of 36

Types of Inheritance (3)


class C : private A{ //privately derived class public: void fn() { int a; a = privA; //error:not accessible a = protA; //valid a = pubA; //valid } }; void main() { int m; B obj1; //object of publicly derived class

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 20 of 36

Types of Inheritance (4)


m = obj1.privA; //error:not accessible m = obj1.protA; //error:not accessible m = obj1.pubA; //valid: B is publicly derived from //class A C obj2; //object of privately derived class m = obj2.privA; //error:not accessible m = obj2.protA; //error:not accessible m = obj2.pubA; //error:not accessible: C is privately //derived from class A }

If no access specifier is given while creating the class, private is assumed.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 21 of 36

Types of Inheritance (5)

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 22 of 36

Multi-level inheritance (1)


Public, private or protected will affect the access the derived class functions have over the members of the base classes in a multilevel inheritance.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 23 of 36

Multi-level inheritance (2)


class A{ public : int a; }; class B : private A{ public : int b; void func_b() { int x,y; x=a; // valid y=b; // valid } };
Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 24 of 36

Multi-level inheritance (3)


class C : public B { public : void func_c() {int x,y; x=a; // not valid y=b; // valid } };

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 25 of 36

Constructors under inheritance (1)


class Base{ protected: int a; public: Base(){a = 0;} //default constructor Base(int c){ a = c;}//one-arg constructor }; class Derived{ public: Derived(): Base(){}//default constructor Derived(int c): Base(c){} //constructor with one-arg };
Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 26 of 36

Constructors under inheritance (2)


The constructor of the base class that should be called during a call of the constructor of a derived class is selected explicitly.
Derived obj1(20); Derived(int c): Base(c);

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 27 of 36

Destructors
Destructors will be called for the derived class first, then for the base class. A destructor for the derived class is to be defined only if its constructor allocates any memory through dynamic memory management.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 28 of 36

Calling Member Functions (1)


Member functions in a derived class can have the same name as those in the base class. When the function is invoked with the object of the base class, the function from the base class is called. If a member function from the derived class wants to call the base class function of the same name, it must use the scope resolution operator.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 29 of 36

Calling Member Functions (2)


A function in the base class can be invoked using the objects of the base class as well as the derived class. If a function exists in the derived class and not in the base class, it can be invoked only with the objects of the derived class.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 30 of 36

Calling Member Functions (3)


class Base{ protected: int ss; public: int func() {return ss;} }; class Derived: public Base{ public: int func() {return Base::func();} };
Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 31 of 36

Calling Member Functions (4)


void main() { //base class object Base b1; b1.func(); //calls base class func Derived a1;//derived class object a1.func(); //calls derived class func }

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 32 of 36

Container classes (1)


Inheritance can be termed as an "is a" relationship.


Example of the employee: The manager is an employee.

In contrast, a class X that has a member of another class Y is said to have a Y or X contains Y. This relationship is called a membership or a "has a" relationship.
class X{ public: Y abc; }; //X contains Y

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 33 of 36

Container classes (2)


When a class contains an object of another class as its member it is called a container class.


Example of a jet plane: Consider deriving a class for a jet plane from a class called engine. A jet plane is not an engine but has an engine.

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 34 of 36

Constructors in container classes (1)


class engine{ private: int num; public: engine(int s) {num = s;} };

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 35 of 36

Constructors in container classes (2)


class jet{ private: int jt; engine eobj; public: jet(int }; x, int y): eobj(y) {jt = x; }

Linux, C, C++ / Object Oriented Programming with C++ / Session 11 / 36 of 36

Anda mungkin juga menyukai