0% found this document useful (0 votes)
15 views13 pages

CPP Unit 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

CPP Unit 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CM3104

Unit 5 - Inheritance And Pointers

5.1 Introduction : Definition of inheritance, defining derived classes, concept of


base class and sub class, types of inheritance, making private member
inheritable, single inheritance, multilevel inheritance, multiple inheritance,
hierarchical inheritance, hybrid inheritance,
5.2 More on inheritance: virtual base class, virtual functions, abstract classes,
constructors in derived classes, member classes using nesting of classes.
5.3 Pointers : Introduction, definition, syntax to declare pointer, pointers to
objects, this pointer, pointers to derived classes, example on pointers
****************************************************************
Inheritance- Introduction
✓ It is the process by which object of one class derived (acquired) the
properties of object of another class. The mechanism of deriving a new
class from an old class is called as inheritance.
✓ In inheritance, the old class is referred as the base Or Parent or super
class and the new class is called as the derived class or child class or
subclass.
✓ We can eliminate redundant code and extend the use of existing classes.
✓ Destructor never participate in inheritance
✓ Creating a new class from an existing class or base class is called
Inheritance.
✓ Inheritance helps in reducing the overall code size of the program, which
is an important concept in object-oriented programming.
✓ The concept of inheritance provides the idea of reusability. Instead of
rewriting the code you can create the class from the existing class and
extending the functionality of existing class.
✓ This mean that we can add additional features to an existing class with
out modifying it.
✓ This is possible by designing a new class that will have the combined
features of both the classes.

1|Page 186_Shraddha Keshao Bonde_N2


CM3104

Base class
✓ A base class can be defined as a normal C++ class, which may consist of
some data and functions.
✓ The existing class whose properties are inherited by other/new class is
called the Parent or Base or Superclass.

Derived class
✓ The class which inherits properties of other class is called Child or
Derived or Sub class.
✓ In other words, a new class which acquires properties from base class is
called derived class.
✓ A derived class can be defined by class in addition to its own detail.
✓ Derived class is also known as a child class or sub class.

2|Page 186_Shraddha Keshao Bonde_N2


CM3104

Advantage of Inheritance
✓ You can reuse the methods and data of the existing class. The existing
class code will be already tested and debugged.
✓ You can extend the existing class by adding new data and new methods
✓ You can modify the existing class by overloading its methods with your
own implementations
✓ As the existing code is reused, it leads to less development and
maintenance costs.
✓ Inheritance helps to reduce code redundancy and supports code
extensibility.

Visibility Modes
✓ The visibility mode is optional. It may be either public , private,
protected.
✓ The default visibility mode is private.
✓ Visibility mode specifies whether the features of the base
✓ class are privately
derived, publicly
derived or
protected.
✓ Examples:
class B : public A {
Members of B }
class B : private A{
Members of B }
// private derivation
class B : A
{ Members of B }

Private Visibility Modes


✓ When a base class is privately inherited by a derived class, “public
members” and “protected members” of the base class become „private
members‟ of the derived class.
✓ Therefore, the public and protected members of the base class can only be
accessed by the member functions of derived class but, cannot be
accessed by the objects of the derived class.
3|Page 186_Shraddha Keshao Bonde_N2
CM3104

✓ Syntax:
class derived: private base{
//Members of derived class;
};

Public Visibility Modes


✓ When a base class is publicly inherited by a derived class then “protected
members” of base class becomes
✓ “protected members” and ‟public members” of the base class become
“public members‟ of the derived class.
✓ Therefore the public members of the base class can be accessed by both
the member functions of derived class as well as the objects of the
derived class.
✓ Syntax:
class derived: public base{
//Members of derived class;
};

Protected Visibility Modes


✓ When a base class is protectedly inherited by a derived class, “public and
protected members‟ of the base class become “protected members‟ of the
derived class.
✓ Therefore the public and protected members of the base class can be
accessed by the member functions of derived class as well as the member
functions of immediate derived class of it but they cannot be accessed by
the objects of derived class
✓ Syntax:
class derived: protected base{
//Members of derived class;
};

Types of Inheritance

1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance

4|Page 186_Shraddha Keshao Bonde_N2


CM3104

5. Hybrid inheritance

Single Inheritance
✓ A derived class with only one base class is called single
inheritance.
✓ Implement an “is-a” relationship
✓ In single level inheritance there is only one base class and
one derived class.
✓ In figure, class A is called Base class and class B is called
as Derived class.
✓ Syntax Class base {
Member of base class
};
Class derive: public/private/protected base{
Member of derive class
};

Multilevel Inheritance
✓ When a class is derived from another derived class i.e.,
derived class act as a base class, such type of inheritance
is known as multilevel inheritance.
✓ In multilevel inheritance, the class is derived from another
derived class.
✓ Example - Class A {
------- };
Class B : public A {
------- };
Class C: public B {
------- };

Multiple Inheritance
✓ When two or more base classes are used from
derivation of a class, it is called as multiple
inheritance.
✓ In multiple inheritance, one derived class is derived
from more than one base class.
✓ Example - Class A {
------- };

5|Page 186_Shraddha Keshao Bonde_N2


CM3104

Class B {
------- };
Class C: public B , public A {
------- };

Hierarchical Inheritance
✓ A class which contain only one base class and multiple derive class is
called Hierarchical Inheritance.
✓ In other word, when a single base
class is used for derivation of two or
more classes it is known as
hierarchical inheritance.
✓ In hierarchical inheritance, more than
one derived class is derived from a
single base class.
✓ Syntax - Class base1 {Member of
base class1};
Class derive1: public/private/protected base1{
Member of derive class1
};
Class derive2: public/private/protected base1
{
Member of derive class2
};

Hybrid Inheritance
✓ It is the combination of more than one type of Inheritance is called
Hybrid Inheritance.
✓ Hybrid Inheritance is combination of
Hierarchical and Multilevel Inheritance.
✓ Syntax - Class base{
Member of base class
};
Class derive1:public/private/protected base{
Member of derive1 class
};
Class derive2{
Member of derive2 class
};

6|Page 186_Shraddha Keshao Bonde_N2


CM3104

Class hyb:public/private/protected derive1,


public/private/protected derive2{
Member of hyb class
};

Virtual Base Class


✓ In C++ Virtual base classes are used in virtual inheritance in a way of
preventing multiple “instances” of a given class appearing in an
inheritance hierarchy when using
multiple inheritances.
✓ Consider a hybrid inheritance as
shown in the above diagram. It
can also directly inherit as shown
by line.
✓ The child class has two direct base
classes, Parent1 and Parent2
which themselves have a common
base class as Grandparent.
✓ The child inherits the members of
Grandparent via two separate
paths.
✓ All the public and protected members of Grandparent are inherited into
Child twice, first via Parent1 and again via Parent2.
✓ This leads to duplicate sets of the inherited members of
✓ Grandparent inside Child class.
✓ EXAMPLE OF VIRTUAL BASE CLASS
class grandparent { };
class parent1:virtual public grandparent {
};
class parent2:virtual pubic grandparent {
};
class child: public parent1,public parent 2
{ };
✓ The duplication of inherited members can be avoided by making the
common base class as virtual base class while declaring the direct or
intermediate base classes as shown in diagram.

7|Page 186_Shraddha Keshao Bonde_N2


CM3104

Constructor in derived class


✓ If base class constructor does not have any arguments, the derived class
need not have a
constructor function.
✓ If base class contain
one or more argument
then it is mandatory for
the derived class to
have a constructor and
pass the argument to the
base class constructor.
✓ In inheritance we create object of only derived class, so it make sense of
derived class to pass argument to the base class constructor.
✓ Base class constructors are always called in the derived class
constructors.
✓ Whenever you create derived class object, first the base class default
constructor is executed and then the derived class's constructor finishes
execution.
✓ To call base class's parameterized constructor inside derived class's
parameterized constructor, we must mention it explicitly while declaring
derived class's parameterized constructor.

Pointer – Introduction
✓ Every variable is a memory location and every memory location has its
address defined which can be accessed using ampersand (&) operator
which denotes an address in memory.
✓ A pointer is a variable whose value is the address of another variable.
✓ Like any variable or constant, you must declare a pointer before you can
work with it.
✓ The general form of a pointer variable declaration is:
✓ Syntax- datatype *var-name;
✓ Example: int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

8|Page 186_Shraddha Keshao Bonde_N2


CM3104

Syntax to declare pointer


✓ There are few important operations, which we will do with the pointers.
1. we define a pointer variables Ex- int a=10; int *p;
2. assign the address of a variable to a pointer Ex- p=&a;
3. finally access the value at the address available in the pointer variable
by using unary operator * that returns the value of the variable Ex-
cout<<*p;
✓ Pointer variable consist of two parts
o the pointer operator (*)
o The Address operator (&)
✓ Syntax to create/declare pointer variable:
datatype *ptr_name; Ex- int *ip;
✓ How to assign address to pointer:
ptr_name=&variable_name

C++ Pointer Operators


✓ Pointer operators used in C++ are :
✓ ‘value at’ operator - * is the dereference operator and can be read as
“value pointed by”.
✓ ‘address’ operator - & is the reference
operator and can be read as “address
of”.
✓ Example-
#include<iostream.h>
void main(){
int a=10; int *ptr;
ptr=&a;
cout<<"\n Value of a:"<<a; //a=10
cout<<"\n Value at ptr:"<<*ptr;
//*ptr=10
cout<<"\n Address of a:"<<&a;
//&a=1024
cout<<"\n Address of a:"<<ptr;
//ptr=1024
cout<<"\n Address of ptr:"<<&ptr; //&ptr=1000
}

Pointer to Objects
✓ Pointer is a variable which stores the address of another variable.

9|Page 186_Shraddha Keshao Bonde_N2


CM3104

✓ We can also call the class members using the pointer.


✓ For that we have to create a pointer of the class data-type.
✓ Object pointers are useful in creating objects at run time.
✓ We can use the object pointer to access the public members of an object.
✓ Example:
class person{
---- public:
void getdata( ){ ------------------------ }
void putdata( ){ ----------------------- }
};
✓ In the given example, we can call the member function getdata( ) and
putdata( ) using the object name as well as the using the object pointer.
person p1; // create person object
person *p; //create person to pointer object
p=&p1; // initialize pointer with address of objectp->getdata( ); // using
object pointer
p->putdata( ); // using object pointer
p1.getdata( ); // using object name
p1.putdata( ); // using object name
✓ We can also use the following method:
(*p).getdata( );
(*p).putdata( );

This Pointer
✓ "A this pointer is automatically passed to a function when it is called.”
✓ C++ uses unique keyword called this to represent an object that
invokes/calls a member function.
✓ The this pointer is a pointer accessible only within the nonstatic member
functions of a class, struct, or union type.
✓ Static member functions don't have a this pointer.
✓ Eg: if we call max function using object A then A.max() will set the
pointer this to the address of the object A.
✓ Applications:
• this pointer is used to represent an object that invokes a member
function.
• Access data member with this pointer like, this->x=50;
• this pointer is to return the object it points to like return *this;

10 | P a g e 186_Shraddha Keshao Bonde_N2


CM3104

Pointer to derived classes


✓ Using the pointer of the base class, we can access only those members
which are inherited from base class and not of the members of derived
class.
✓ To access the members of derived class we have to use the pointer to the
derived type.
✓ We can use pointer not only to the base class object but also to the object
of derived class.
✓ A single pointer variable can be maze to point to objects belonging to
different class
✓ Suppose B is base class and D is derived class form B, then pointer
declared as a pointer to B can also be pointer to D.
✓ Example - B *p1; //pointer object
lB b1 //base class object
lD d1 //derived class object
lp1=&b1; We can make p1 top point to the object d1 as: p1=&d1

Virtual Function
✓ When we use the same function name in both the base and derived
classes, the function in base class is declared as virtual using the keyword
virtual preceding its normal declaration.
✓ A virtual member function in a base class expects to be overridden in a
derived class

Rules for virtual function


1. It must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. It can be a friend of another class.
5. A virtual function in a base class must be defined, even though it may not be
used.
6. The prototypes of the base class version of a virtual function and all the
derived class versions must be identical.
7. We cannot have virtual constructors, but we can have virtual

11 | P a g e 186_Shraddha Keshao Bonde_N2


CM3104

destructors.
8. While a base pointer can point to any type of the derived object, the reverse is
not true.
9. When a base pointer points to a derived class, incrementing or
decrementing it will not make it to point to the next object of the derived class.
10. If a virtual function is defined in the base class, it need not be
necessarily redefined in the derived class.

Pure Virtual Function


✓ Pure virtual function is virtual function declared in a base class which has
no definition in base class.
✓ It ends with ZERO in the declaration: virtual void show( )=0;
✓ Derived class override the pure virtual function. That is in the virtual
function, the function is declared as virtual inside the base class and
redefine it in the derived classes
✓ The function inside the base class is used for performing any task.
✓ It only serves as a placeholder.
✓ It is also called as a “do-nothing” function.
✓ Example: class Base{
public:
virtual void show()=0 //it is pure virtual function};

Abstract Class
✓ Abstract class is a class which has at least one pure virtual function.
✓ A class from which we never want to create objects is called an abstract
class.
✓ Such class exist only as
a parent for derived
classes.
✓ If we try to create
object of such base
class then compiler
would generate error.
✓ The main objective is to provide some traits to the derived classes and to
create base pointer to achieve run time polymorphism.
✓ Abstract classes are mainly used for Upcasting, which means that its
12 | P a g e 186_Shraddha Keshao Bonde_N2
CM3104

derived classes can use its interface.


********************************************************

13 | P a g e 186_Shraddha Keshao Bonde_N2

You might also like