You are on page 1of 38

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-1


Bachelor of Engineering (Computer Science & Engineering)
Subject Name and Code: Object-oriented Programming using
C++
CSP-157
Prepared by Ms. Jasleen Kaur
Pointers and Virtual Functions DISCOVER . LEARN . EMPOWER
CHAPTER -7
Course Outcome
CO Title Level
Number

CO1 provide the environment that allows students to Remember


understand object-oriented programming Concepts.   Will be covered in this
CO2 demonstrate basic experimental skills for Understand lecture
differentiating between object-oriented and  
procedural programming paradigms and the
advantages of object-oriented programs.

CO3 Ability to demonstrate their coding skill on complex Understand


programming concepts and use it for generating
solutions for engineering and mathematical problems.
CO4 Students will develop skills to design the application of Understand
classes, objects, constructors, destructors, inheritance,  
operator overloading and polymorphism, pointers,
virtual functions, templates, exception handling, file
operations and handling
2
• Declaring & initializing pointers,
• pointer to objects,
• this pointer,
CONTENTS • pointer to derived classes,
• static and dynamic binding.

This chapter discusses pointers-one of the most


powerful, yet challenging to use, C++ capabilities.
Our goals here are to help you determine when it
is appropriate to use pointers and show how to
use them correctly and responsibly.

3
WHAT IS POINTER
• Pointers are symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create
and manipulate dynamic data structures. It’s general declaration in C/C++ has the format:
• Syntax:
• datatype *var_name;
• int *ptr; //ptr can point to an address which holds int data
• The reason we associate data type to a pointer is that it knows how many bytes the data is stored in. When we increment a
pointer, we increase the pointer by the size of data type to which it points

4
SAMPLE PROGRAM IN C++

Figure 7.1 Program made on Dev-C++ 5


REFERNCES AND POINTERS
There are 3 ways to pass C++ arguments to a function:
• call-by-value
• call-by-reference with pointer argument
• call-by-reference with reference argument

6
PROGRAM

Figure 7.2 Program made on Dev-C++ 7


PROGRAM

Figure 7.3 Program made on Dev-C++ 8


PROGRAM

Figure 7.4 Program made on Dev-C++ 9


PROGRAM

Figure 7.5 Program made on Dev-C++ 10


PROGRAM

Figure 7.6 Program made on Dev-C++ 11


PROGRAM’s OUTPUT

Figure 7.7 Program made on Dev-C++ 12


THIS POINTER
Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an
implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking
object.
For example, consider an object obj calling one of its member function say method() as obj.method(). Then, this pointer will
hold the address of object obj inside the member function method(). The this pointer acts as an implicit argument to all the
member functions.
Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this
pointer
C++ lets object destroy themselves by calling the following code :
delete this;

Figure 6.3 C++ EXCEPTIONS [1] 13


THIS POINTER

Figure 7.8 Made on Dev-C++ 14


EXAMPLE

Figure 7.9 Program made on Dev-C++ 15


EXAMPLE

Figure 7.10 Program made on Dev-C++ 16


EXAMPLE

Figure 7.11 Program made on Dev-C++ 17


EXPLANATION
This program is used to compare the percentage of three students and display the highest among them.
The concept of this pointer is used in this program. A class student is created with data members name, roll, age and percent
and member functons getdata(), max() and display(). Data for each student is entered by calling getdata() function.
Then, max() function is called by object s3 and s2 and s1 are passed as parameter in the function.
The value of percent is compared and the object with highest percent is returned. If the object calling the method has the
highest percentage then, it is returned by using this pointer as,
return *this;

18
WHEN THIS POINTER IS USED?
• When local variable’s name is same as member’s name
• Method Chaining
• Returning object

19
WHEN THIS POINTER IS USED?
• When local variable’s name is same as member’s name

Figure 7.12 Program made on Dev-C++ 20


WHEN THIS POINTER IS USED?
• When local variable’s name is same as member’s name

Figure 7.13 Program made on Dev-C++ 21


WHEN THIS POINTER IS USED?
• To return reference to the calling object
/* Reference to the calling object can be returned */
Test& Test::func ()
{
// Some processing
return *this;
}

Figure 7.14 Program made on Dev-C++ 22


WHEN THIS POINTER IS USED?
• To return reference to the calling object
/* Reference to the calling object can be returned */
Test& Test::func ()
{
// Some processing
return *this;
}

Figure 7.15 Program made on Dev-C++ 23


METHOD CHAINING
After returning the object from a function, a very useful application would be to chain the methods for ease and a cleaner code.
For example,
positionObj->setX(15)->setY(15)->setZ(15);
Here, the methods setX, setY, setZ are chained to the object, positionObj. This is possible because each method return *this
pointer.
This is equivalent to
positionObj->setX(15);
positionObj->setY(15);
positionObj->setZ(15);

24
POINTER TO OBJECT

Figure 7.16 Program made on Dev C++ 25


POINTER TO OBJECT

Figure 7.17 Program made on Dev C++ 26


POINTER TO DERIVED CLASS

Figure 7.18 Program made on Dev C++ 27


POINTER TO DERIVED CLASS

Figure 7.19 Program made on Dev C++ 28


STATIC AND DYNAMIC BINDING IN C+
+
Binding refers to the process of converting identifiers (such as variable and performance names) into addresses.
Binding is done for each variable and functions.
For functions, it means that matching the call with the right function definition by the compiler. It takes place either at compile
time or at runtime.

Figure 7.20 Types of binding[2]

29
VIRTUAL FUNCTIONS
By default early binding happens in C++. Late binding (discussed below) is achieved with the help of virtual keyword)
Virtual means existing in appearance but not in reality. When virtual functions are used, a program that appears to be calling a
function of one class may in reality be calling a function of a different class.
• Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used
for function call.
• They are mainly used to achieve Runtime polymorphism
• Functions are declared with a virtual keyword in base class.
• The resolving of function call is done at Run-time.

30
DEFAULT- EARLY BINDING

Figure 7.21 Program made on Dev-C++


31
DEFAULT- EARLY BINDING

Figure 7.22 Program made on Dev-C++


32
LATE BINDING

Figure 7.23 Program made on Dev-C++


33
LATE BINDING

Figure 7.24 Program made on Dev-C++


34
Assessment Pattern
Section-A
1. (a) Define virtual functions
(b) What is structured binding?
(c) Differentiate between static and dynamic binding.
Section-B
2. Write a C++ program to demonstrate the concept of virtual functions.
3. WAP in C++ to demonstrate pointer to object.
4. Write a C++ program to use this pointer.

35
APPLICATIONS OF THIS POINTER

• When local variable’s name is same as member’s name


• Method Chaining
• Returning object

36
REFERENCES

• Reference Website
[1] https://www.geeksforgeeks.org/pointers-c-examples/
[2] https://techdifferences.com/difference-between-static-and-dynamic-binding.html
[3] https://www.geeksforgeeks.org/copy-constructor-argument-const/
[4] https://www.softwaretestinghelp.com/runtime-polymorphism-in-cpp/

37
THANK YOU

For queries
Email: CST157_2019@gmail.com

You might also like