Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Inheritance: Presented By: Er. Simarpreet Kaur Subject: Programming in C++

You are on page 1of 30

INHERITANCE

Presented by: Er. Simarpreet Kaur


Subject: Programming in C++
Inheritance
• Inheritance is the capability of one class to
acquire properties and characteristics from
another class. The class whose properties are
inherited by other class is called
the Parent or Base or Super class. And, the
class which inherits properties of other class is
called Child or Derived or Sub class.
• Inheritance makes the code reusable. When
we inherit an existing class, all its methods
and fields become available in the new class,
Purpose of Inheritance
• Code Reusability
• Method Overriding (Hence, Runtime
Polymorphism.)
• Use of Virtual Keyword
Syntax of Inheritance
• Syntax:
class Subclass_name : access_mode
Superclass_name
• While defining a subclass like this, the super
class must be already defined or atleast
declared before the subclass declaration.
• Access Mode is used to specify, the mode in
which the properties of superclass will be
inherited into subclass, public, privtate or
protected.
Derived Class
• A class can be derived from more than one
classes, which means it can inherit data and
functions from multiple base classes. To define
a derived class, we use a class derivation list to
specify the base class(es). A class derivation
list names one or more base classes.
• Syntax
class derived-class: access-specifier base-class
• Where access-specifier is one of public,
protected, or private, and base-class is the
Inheritance Visibility Mode
• Public inheritance
• Private inheritance
• Protected inheritance
Public inheritance
• This is the most used inheritance mode. In this
the protected member of super class becomes
protected members of sub class and public
becomes public.
• Syntax
class Subclass : public Superclass
Private Inheritance
• In private mode, the protected and public
members of super class become private
members of derived class.
• Syntax
class Subclass: Superclass // By default its
private inheritance
Protected Inheritance
• In protected mode, the public and protected
members of Super class becomes protected
members of Sub class.
• Syntax
class subclass : protected Superclass
Ambiguity in Inheritance
• In multiple inheritance, there may be
possibility that a class may inherit member
functions with same name from two or more
base classes and the derived class may not
have functions with same name as those of its
base classes. If the object of the derived class
need to access one of the same named
member function of the base classes then it
result in ambiguity as it is not clear to the
compiler which base’s class member function
Example
• A subclass can be derived from more than one superclass.
This is multiple inheritance.
• class A
{
public:
void show(){cout<<"A";}
};
class B
{
public:
void show() { cout<<"B"; }
};

• /* class C is multiple derived from its superclass A and B */


class C:public A, public B
{
};
int main()
{
Cont..
• Ambiguous call:
 For the above sample
program, c.show() is ambiguous to get
resolved among candidate functions
B::show( ) and A::show( )
• Solution to ambiguous calls in multiple
inheritance:
 Use scope resolution operator to explicitly
specify which base class's member function is
to be invoked.
Virtual Base Class
• When two or more objects are derived from a
common base class, we can prevent multiple
copies of the base class being present in an
object derived from those objects by declaring
the base class as virtual when it is being
inherited. Such a base class is known as virtual
base class. This can be achieved by preceding
the base class’ name with the word virtual.
• Virtual base classes are created before non-
virtual base classes, which ensures all bases
Example
class PoweredDevice
{
public:
PoweredDevice(int nPower)
{
cout << "PoweredDevice: " << nPower << endl;
}
};

class Scanner: public PoweredDevice


{
public:
Scanner(int nScanner, int nPower)
: PoweredDevice(nPower)
{
Cont..
class Printer: public PoweredDevice
{
public:
Printer(int nPrinter, int nPower)
: PoweredDevice(nPower)
{
cout << "Printer: " << nPrinter << endl;
}
};

class Copier: public Scanner, public Printer


{
public:
Copier(int nScanner, int nPrinter, int nPower)
: Scanner(nScanner, nPower), Printer(nPrinter, nPo
Cont..
• If we have to create a Copier class object,
by default you would end up with two
copies of the PoweredDevice class -- one
from Printer, and one from Scanner. This
has the following structure:
Then…how to use ‘virtual class’
• At the time, when only one copy of
PoweredDevice to be shared by both Scanner
and Printer. To share a base class, simply insert
the “virtual” keyword in the inheritance list of
the derived class. This creates what is called
a virtual base class, which means there is only
one base object that is shared. Here is the an
example (without constructors for simplicity)
showing how to use to virtual keyword to
create a shared base class:
Example
class PoweredDevice
{
};

class Scanner: virtual public PoweredDevice


{
};

class Printer: virtual public PoweredDevice


{
};

class Copier: public Scanner, public Printer


Function Overriding
• If we inherit a class into the derived class and
provide a definition for one of the base class's
function again inside the derived class, then
that function is said to be overridden, and this
mechanism is called Function Overriding
Requirements for overriding
• Inheritance should be there. Function
overriding cannot be done within a class. For
this we require a derived class and a base
class.
• Function that is redefined must have exactly
the same declaration in both base and derived
class, that means same name, same return
type and same parameter list.
Example
class Base
{
public: void show( )
{
cout << "Base class";
}
};
class Derived:public Base
{
public: void show( )
{
cout << "Derived Class";
}
}
Order of execution of Constructors
• During inheritance, base class may also
contain constructor and destructor.
In this case if you create an instance for the
derived class then base class constructor will
also be invoked and when derived instance is
destroyed then base destructor will also be
invoked and the order of execution of
constructors will be in the same order as their
derivation and order of execution of
destructors will be in reverse order of their
Example
using System;
namespace ProgramCall
{
class Base1
{
public Base1()
{
Console.WriteLine("Base Class Constructor");
}
~Base1()
{
Console.WriteLine("Base Class Destructor");
}
}
class Derived1 : Base1
{
public Derived1()
{
Console.WriteLine("Derived Class Constructor");
}
Cont..
class ConstructorInheritance
{
static void create()
{
Derived1 obj = new Derived1();
}

static void Main()


{
create();
GC.Collect();
Console.Read();
}
Output of Program
• Base Class Constructor
Derived Class Constructor
Derived Class Destructor
Base Class Destructor
Nesting of classes
A nested class is a class which is declared in
another enclosing class. A nested class is a
member and as such has the same access
rights as any other member. The members of
an enclosing class have no special access to
members of a nested class; the usual access
rules shall be obeyed.
Example 1: compilation without
error
#include<iostream>
using namespace std;
/* start of Enclosing class declaration */
class Enclosing {
int x;
/* start of Nested class declaration */
class Nested {
int y;
void NestedFun(Enclosing *e) {
cout<<e->x; // works fine: nested class can access
// private members of Enclosing class
}
}; // declaration Nested class ends here
}; // declaration Enclosing class ends here
int main()
Example 2: compilation with
error
#include<iostream>
using namespace std;
/* start of Enclosing class declaration */
class Enclosing {
int x;
/* start of Nested class declaration */
class Nested {
int y;
void NestedFun(Enclosing *e) {
cout<<e->x; // works fine: nested class can access
// private members of Enclosing class
}
}; // declaration Nested class ends here
}; // declaration Enclosing class ends here
int main()
Member access specifier
• Nested class declarations obey member
access specifiers, a private member class
cannot be named outside the scope of the
enclosing class, although objects of that class
may be manipulated:
example
class enclose {
struct nested { // private member
void g( ) { }
};
public:
static nested f( ) { return nested{ }; }
};
int main( )
{
//enclose::nested n1 = e.f( ); // error: 'nested' is
private

You might also like