0% found this document useful (0 votes)
3 views34 pages

Unit 3

The document provides an overview of inheritance and pointers in programming, detailing types of inheritance such as single, multiple, hierarchical, multilevel, and hybrid inheritance. It explains member access control, constructors in inherited classes, and the concept of ambiguity in multiple inheritance, along with solutions to resolve it. Additionally, it covers pointers, pointer arithmetic, arrays of pointers, function pointers, and the 'this' pointer in C++.

Uploaded by

rutu.bidarkar
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)
3 views34 pages

Unit 3

The document provides an overview of inheritance and pointers in programming, detailing types of inheritance such as single, multiple, hierarchical, multilevel, and hybrid inheritance. It explains member access control, constructors in inherited classes, and the concept of ambiguity in multiple inheritance, along with solutions to resolve it. Additionally, it covers pointers, pointer arithmetic, arrays of pointers, function pointers, and the 'this' pointer in C++.

Uploaded by

rutu.bidarkar
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/ 34

Unit 3: Inheritance and Pointer

Introduction to Inheritance
Introduction to Inheritance
Introduction to Inheritance
Need of Inheritance
➢ Avoid Duplication of code
➢ Increases the execution time
➢ Reduces Chances of error and data redundancy
➢ Capability of inheriting features
➢ Reusability of code
Types of Inheritance
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multilevel inheritance
Hybrid inheritance
Single Inheritance
the inheritance in which a class A // base class
derived class is inherited from {
the only one base class
..........
};
class B : acess_specifier A //
derived class
{
...........
};
Multiple Inheritance
class A
a class can inherit more than one class
{
..........
};
class B
{
...........
};
class C : acess_specifier A,
access_specifier B
{
...........
};
Hierarchical Inheritance
class A // base class
{ .............. };
class B : access_specifier A // derived from
A
{ ........... } ;
class C : access_specifier A // derived A
{ ........... } ;
class D : access_specifier A // derived
from A
{ ........... } ;
Multilevel Inheritance
class A // base class
one class inherits another child class {
...........
};
class B : access_specifier A // derived class
{
...........
};
class C : access_specifier B // derived class
B
{
...........
};
Hybrid Inheritance
➢A combination of more than one type of
inheritance.
➢For example, A child and parent class relationship
that follows multiple and hierarchical inheritance
both can be called hybrid inheritance.
Syntax Hybrid Inheritance
class A {
.........
};
class B : public A{
..........
};
class C {
...........
};
class D : public B, public C
{
...........
};
MemberAccess Control
Public member is accessible to all the
functions of the program.
Private member is accessible within the
class only.
Protected member is accessible within its
own class as well as the class immediately
derived from it.
Base and Derived Class
Derive a new class (subclass) from an
existing class (base class or
superclass).

Inheritance creates a hierarchy of


related classes (types) which share
code and interface.
Visibility of Inherited Members
Base class Derived class visibility
visibility
Public Private Protected

Private Not Inherited Not Inherited Not Inherited

Protected Protected Private Protected

Public Public Private Protected


Constructors of Inherited Classes
The constructors of inherited classes are called in the same order in which they are
inherited. class C: public B, public A // Note the order
class A { public:
{ public: C()
A() { cout << "A's constructor called" << endl; } { cout << "C's constructor called" << endl; }
}; };

class B int main()


{ public: { Output:
B() { cout << "B's constructor called" << endl; } C c; B's constructor called
}; return 0; A's constructor called
} C's constructor called
Ambiguity
▪A situation in which something has more than
one possible meaning and may therefore cause
confusion.
▪Occurs during function overriding
▪Two base classes have a same function which is
not overridden in derived class
▪An ambiguity can arise when several paths
exist to a class from the same base class.
▪A child class could have duplicate sets of
members inherited from a single base class.
Solution of ambiguity in multiple
inheritance
The ambiguity can be resolved by using the scope resolution operator to specify the class
in which the member function lies
obj.a :: abc();
Virtual Base Class

Duplication is avoided regardless of the number of paths that exist to the child
class.
Pointers
• Pointers are symbolic representations of addresses. They enable programs to simulate call-
by-reference as well as to create and manipulate dynamic data structures. Iterating over
elements in arrays or other data structures is one of the main use of pointers.

• The address of the variable you’re working with is assigned to the pointer variable that
points to the same data type (such as an int or string).

datatype *var_name; int *ptr; // ptr can point to an address which holds int data
Pointers
Program
// C++ program to illustrate Pointers

#include <iostream.h>
using namespace std;
void geeks()
{
int var = 20;

// declare pointer variable


int* ptr;

// note that data type of ptr and var must be same


ptr = &var;

// assign the address of a variable to a pointer


cout << "Value at ptr = " << ptr << "\n";
cout << "Value at var = " << var << "\n";
cout << "Value at *ptr = " << *ptr << "\n";
}
// Driver program
int main()
{
geeks();
return 0;
}
Pointer Expressions and Pointer Arithmetic

A limited set of arithmetic operations can be performed on pointers


which are:
incremented ( ++ )
decremented ( — )
an integer may be added to a pointer ( + or += )
an integer may be subtracted from a pointer ( – or -= )
difference between two pointers (p1-p2)
Program
#include <iostream.h>
using namespace std;
void arith()
{
// Declare an array
int v[3] = { 10, 100, 200 };

// declare pointer variable


int* ptr;

// Assign the address of v[0] to ptr


ptr = v;

for (int i = 0; i < 3; i++) {


cout << "Value at ptr = " << ptr << "\n";
cout << "Value at *ptr = " << *ptr << "\n";

// Increment pointer ptr by 1


ptr++;
}
}

// Driver program
int main() { arith(); }
Arrays of Pointers
An array of pointers is an array that consists of variables of pointer type, which means that the variable is a
pointer addressing to some other element. Suppose we create an array of pointer holding 5 integer pointers;
then its declaration would look like:

• Declared an array of pointer named as p, and it allocates 3 integer pointers in memory.


• The element of an array of a pointer can also be initialized by assigning the address of some other element.
Arrays of Pointers

• In the above code, we are assigning the address of 'a' variable to the third element of an array 'ptr'.
• We can also retrieve the value of 'a' be dereferencing the pointer.
Array of Pointers

#include <iostream>
using namespace std;
int main()
{ int ptr1[5]; // integer array declaration
int *ptr2[5]; // integer array of pointer declaration
cout << "Enter five numbers :" <<endl;
for(int i=0;i<5;i++)
{ cin >> ptr1[i]; }
for(int i=0;i<5;i++)
{ ptr2[i]=&ptr1[i]; }
// printing the values of ptr1 array
cout << "The values are" << endl;
for(int i=0;i<5;i++)
{ cout << *ptr2[i] << endl; } }
Pointers to Functions

• The function pointer is a pointer used to point functions.


• It is basically used to store the address of a function.
• We can call the function by using the function pointer, or we can also pass the pointer to another function as
a parameter.
• They are mainly useful for event-driven applications, callbacks, and even for storing the functions in arrays.
Pointer to Object
• Pointers to objects aim to make a pointer that can access the object, not the variables. Pointer to object in
C++ refers to accessing an object.
• There are two approaches by which you can access an object. One is directly and the other is by using a
pointer to an object in C++.
• A pointer to an object in C++ is used to store the address of an object. For creating a pointer to an object in
C++, we use the following syntax:

• After storing the address in the pointer to the object, the member function can be called using the
pointer to the object with the help of an arrow operator.
‘This’ Pointer

• In C++ programming, this is a keyword that refers to the current instance of the
class. There can be 3 main usage of this keyword in C++.

• It can be used to pass current object as a parameter to another method.


• It can be used to refer current class instance variable.
• It can be used to declare indexers.
Pointer to Derived Class
https://www.youtube.com/watch?v=jfodcoyUhE8
Thank you

K.K. WAGH INSTITUTE OF ENGINEERING EDUCATION AND RESEARCH, NASHIK DEPARTMENT OF


January 21, 2021
ELECTRONICS AND TELECOMMUNICATION
61

You might also like