KEMBAR78
C++ Unit - 3 | PDF | Pointer (Computer Programming) | C++
0% found this document useful (0 votes)
13 views48 pages

C++ Unit - 3

This document covers key concepts in Object-Oriented Programming (OOP) including pointers, virtual functions, polymorphism, and file management in C++. It explains the use of pointers for dynamic binding, the role of virtual functions in achieving runtime polymorphism, and the importance of managing console I/O operations using C++ stream classes. Additionally, it discusses pure virtual functions and virtual destructors, emphasizing their significance in abstract classes and proper memory management.

Uploaded by

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

C++ Unit - 3

This document covers key concepts in Object-Oriented Programming (OOP) including pointers, virtual functions, polymorphism, and file management in C++. It explains the use of pointers for dynamic binding, the role of virtual functions in achieving runtime polymorphism, and the importance of managing console I/O operations using C++ stream classes. Additionally, it discusses pure virtual functions and virtual destructors, emphasizing their significance in abstract classes and proper memory management.

Uploaded by

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

OBJECTORIENTED

PROGRAMMING - I
UNIT : 3

Pointers Virtual Functions and


Polymorphism, Pure Virtual functions,
Managing Console I/O Operations, C++
Stream Classes, Managing Output with
Manipulators, Working with Files, File
Pointers and their manipulations, Templates
Introduction

• Polymorphism : It is one of the crucial features of OOP. It


simply means ‘one name, multiple forms’.
• The overloaded member functions are selected for invoking
by matching the arguments, both type and number.
• The information is known to the compiler at compile time.
• This is called early binding or static binding or static linking
and polymorphism.

3
Prepared by : Mr. Sandip A. Gohil
• // Virtual Functions and // Run-time Polymorphism
• #include <iostream.h>
▫ // base class
class base
{ public: int a; };
▫ // derived class
class derived:public base
{ public: int b; };
▫ // main
void main()
{ base b; derived d;
▫ // base class pointer
base *bptr;
// pointer pointing to base's object
bptr=&b;
bptr->a=10;
// pointer pointing to derived's object
bptr=&d; // still is able to access the members of the base class
bptr->a=100; }

4
Prepared by : Mr. Sandip A. Gohil
• // Using Virtual functions to achieve run-time Polymorphism
• #include <iostream.h>
// base class
class base
{
public:
virtual void func()
{ cout<<"Base's func()\n"; }
};
// derived class
class derived:public base
{
public:
void func()
{
cout<<"Derived's func()\n"; }
};

5
Prepared by : Mr. Sandip A. Gohil
• // main
void main()
{
int ch=0;
base b;
derived d;
// base class pointer
base *bptr;
while(ch!=3)
{
cout<<"1> Call Base's func\n";
cout<<"2> Call Derived's func\n";
cout<<"3> Quit\n"; cin>>ch;
switch(ch)
{
case 1: // point to base's object
bptr=&b; break;
case 2: // point tp derived's object
bptr=&d; break;
default: bptr=&b;
} // call whichever function // user has chosen to call
bptr->func(); } }
6
Prepared by : Mr. Sandip A. Gohil
Pointers
• To achieve the dynamic binding, it is required to use the
pointer to objects and virtual functions.
• Pointer is a derived data type that refers to another
data variable by storing the variable’s memory address
rather than the data.
• Like C, a pointer variable can also be used to refer
another pointer in c++.
• Pointers provide an alternative approach to access
other data objects.

7
Prepared by : Mr. Sandip A. Gohil
Pointer to Object
• Normal Objects cannot help in achieving runtime
polymorphism. It is possible to achieve it only by
setting or defining pointer to objects,
• Pointer to object is a variable containing an address of
an object.
• It is similar to a pointer to any other variable. We can
use normal address-of operator to get the address of
an object.
• We can define a pointer to an object and can assign it
the address of an object.

8
Prepared by : Mr. Sandip A. Gohil
Declaring Pointers
• The declaration of a pointer is based on the data type of the
variables it points to.
• Syntax : - datat-type *pointerVariable;
• A pointer is able to point to only one data type at a specific
time.
• Eg :- int *ptr; // declare
ptr = &a; // initialize

Class Demo{ ----}


Demo objDemo;
Demo *ptrObjDemo;
ptrObjDemo = &objDemo;
9
Prepared by : Mr. Sandip A. Gohil
this pointer
• The this pointer is a pointer that represent an object that
invokes a member function.
• This is a pointer that points to an object for which this
function was called.
• This pointer is automatically passed to a member function
when it is called.
• This pointer acts as an implicit argument to all the member
functions.
• Eg: objDemo.Display() function call, the value of this pointer
contains the address of objDemo.
• this pointer is poiner to objDemo.

10
Prepared by : Mr. Sandip A. Gohil
Virtual Functions
• Virtual functions are special.
• Using virtual functions we are able to point to any object of a
derived class using a base pointer and can manipulate that
object.
• The class has an additional storage requirement when at
least one virtual function is defined.
• A table is created additionally to store pointers to all virtual
functions available to all objects of class.
• This is known to a virtual table.
• Syntax Constraints on Virtual functions:
• 1) Function should precede virtual keyword in the base
class
11
Prepared by : Mr. Sandip A. Gohil
2)The function name in derived class must have same name as
of virtual function defined in base class and same prototype
3) The function in derived class need not to be preceded by
virtual keyword.
4) If function is not defined with same name then base class
function will be called.
5) The virtual function must be defined in the base class, it
may have an empty body though.
6) Polymorphism is achieved (executing the function of the
object which is pointed to) only using pointers to the base
class. It is not possible using objects.
7) Virtual Constructors are not possible.

12
Prepared by : Mr. Sandip A. Gohil
Virtual Destructors
• Virtual destructors are needed for proper deletion of objects
of derived class, when pointed to by a base class pointer.
• If do not define virtual destructors, only the base class
subobject is deleted, and the remaining portion of the
derived class object is not deleted.
• We know destructor name cannot be same in base and
derived class as it should same as class name.
• A class can have only one destructor and the derived class
destructor is the function called when delete is invoked with
a base class pointer.

13
Prepared by : Mr. Sandip A. Gohil
Pure Virtual Functions
• When a class does not have any object, there is no
need to have functions for it as there is no object to
utilize those functions.
• When we write “=0” in place of the function body after
function header, the function is said to be pure virtual
function.
• In this function need not have any body.
• So instead of defining the function with any empty
body, if we define it as pure virtual function, the
function forces the base class to be abstract and it has
to be derived.
• And abstract classes do not have object.
Prepared by : Mr. Sandip A. Gohil
14
Pointers expressions and Arithmetic
• C++ allows pointers to perform the following arithmetic
operations:
▫ A pointer can be incremented (++) or decremented
(--).
▫ Any integer can be added to or subtracted from a
pointer
▫ One pointer can be subtracted from another.

Eg: int a[10];


int *aptr;
aptr = &a[0];
We cn do aptr++ or aptr– to increment or decrement a
15
pointer and it moves to the next memory address.
Prepared by : Mr. Sandip A. Gohil
Pointers with Arrays and
Strings
• Accessing an array with pointers is simpler than accessing
the array index.
• Arrays refer to a block of memory space, but pointers do not
refer to any section of memory.
• The memory addresses of arrays cannot be changed, whereas
the content of the pointer variables, such as the memory
addresses that it refer to can be changed.
• Eg :- Pointer to array

int *nptr;
nptr = number[0];
Nptr points to the 1st element of an array.
16
Prepared by : Mr. Sandip A. Gohil
Array of pointers
• An array of pointers represent the collection of
addresses.
• An array of pointers point to an array of data
items.
• Each element of the pointer array points to an
item of the data array.
• Data items can be accessed either directly or
by dereferencing the elements of pointer array

17
Prepared by : Mr. Sandip A. Gohil
Pointers to Functions
• The pointer to function is known as callback function.
We can use these function pointers to refer to a
function.
• Using function pointers, we can allow a c++ program to
select a function dynamically at run time.
• We can also pass a function as an argument to another
function (as pointer).
• There are two types of function pointers, function that
points to static member functions and function pointers
that point to non-static member functions
• For non-static member function requires hidden
argument.
Prepared by : Mr. Sandip A. Gohil
18
Managing Console I/O Operations,
● Every program takes some data as input and generates processed data as an
output following the familiar input process output cycle.
● The use of the cin and cout is already known with the operator >> and << for
the input and output operations. In this article, we will discuss how to control the
way the output is printed.
● It also supports all of C’s set of I/O functions and therefore can be used in C++
programs, but their use should be restrained due to two reasons.

○ I/O methods in C++ support the concept of OOPs.


○ I/O methods in C cannot handle the user-define data type such as class and
object.
● It uses the concept of stream and stream classes to implement its I/O
operations with the console and disk files.

19
C++ Stream
● The I/O system in C++ is designed to work with a wide variety of devices
including terminals, disks, and tape drives.
● Although each device is very different, the I/O system supplies an interface to the
programmer that is independent of the actual device being accessed. This
interface is known as the stream.
○ A stream is a sequence of bytes.
○ The source stream that provides the data to the program is called the input
stream.
○ The destination stream that receives output from the program is called the
output stream.
○ The data in the input stream can come from the keyboard or any other input
device.
○ The data in the output stream can go to the screen or any other output device.
20
C++ Stream Classes
● The C++ I/O system contains a hierarchy of classes that are used to
define various streams to deal with both the console and disk files.
#include <iostream>
using namespace std;

int main()
{

cout << " This is my first" " Blog on the gfg";


return 0;
}

This is my first Blog on the gfg

21
Prepared by : Mr. Sandip A. Gohil
Tabular
Representation
of stream
classes for
Console
Operation

22
Prepared by : Mr. Sandip A. Gohil
C++ Function

23
Prepared by : Mr. Sandip A. Gohil
C++ Function

24
Prepared by : Mr. Sandip A. Gohil
C++ Function

25
Prepared by : Mr. Sandip A. Gohil
C++ Function

26
Prepared by : Mr. Sandip A. Gohil
C++ Function

27
Prepared by : Mr. Sandip A. Gohil
C++ Function

28
Prepared by : Mr. Sandip A. Gohil
C++ Function

29
Prepared by : Mr. Sandip A. Gohil
C++ Function

30
Prepared by : Mr. Sandip A. Gohil
C++ Function

31
Prepared by : Mr. Sandip A. Gohil
C++ Function

32
Prepared by : Mr. Sandip A. Gohil
C++ Function

33
Prepared by : Mr. Sandip A. Gohil
C++ Function

34
Prepared by : Mr. Sandip A. Gohil
C++ Function

35
Prepared by : Mr. Sandip A. Gohil
C++ Function

36
Prepared by : Mr. Sandip A. Gohil
C++ Function

37
Prepared by : Mr. Sandip A. Gohil
C++ Function

38
Prepared by : Mr. Sandip A. Gohil
C++ Function

39
Prepared by : Mr. Sandip A. Gohil
C++ Function

40
Prepared by : Mr. Sandip A. Gohil
C++ Function

41
Prepared by : Mr. Sandip A. Gohil
C++ Function

42
Prepared by : Mr. Sandip A. Gohil
C++ Function

43
Prepared by : Mr. Sandip A. Gohil
C++ Function

44
Prepared by : Mr. Sandip A. Gohil
C++ Function

45
Prepared by : Mr. Sandip A. Gohil
C++ Function

46
Prepared by : Mr. Sandip A. Gohil
C++ Function

47
Prepared by : Mr. Sandip A. Gohil
C++ Function

48
Prepared by : Mr. Sandip A. Gohil

You might also like