UNIT 4 CS - 15
UNIT 4 CS - 15
CS-15
C++ and Object Oriented
Programming
Unit – 4 Presented By : Dhruvita Savaliya
Pointer, Virtual
Functions and
Polymorphism, RTTI
Console I/O
Operations
2
Topics :
• Pointer to Object
• Pointer to derived class
• this Pointer
• Rules for virtual function
• Virtual function and pure virtual function
• Run Time Type Identification (RTTI)
• C++ Streams
• C++ Stream Classes
• Unformatted and formatted I/O operations
• Use of Manipulators.
3
• Introduction to polymorphism :
• The word “polymorphism” means having many
forms. In simple words, we can define
polymorphism as the ability of a message to be
displayed in more than one form.
4
Function Overriding occurs when a derived class has a definition for one of the
member functions of the base class. That base function is said to be overridden.
There are 2 type of overriding :
1. Compile time
2. Run time
1. Compile Time overriding : 2. Run Time overriding :
class Parent { class Parent {
public: public:
void Print() virtual void Print()
{ {
cout << "Base Function" << endl; cout << "Base Function" << endl;
} }
}; };
Example :
class Student{
public :
int rollno , houseno;
void show()
{
cout << "\nRoll no of the Student is Using Pointer = " <<rollno ;
cout << "\nHouse of the Student is = " << houseno;
}
};
void main ()
Output:
{ Roll no of the Student is Using Pointer = 26
Student s_obj; House of the Student is = 24
Student *ptr_s_obj;
ptr_s_obj=&s_obj;
ptr_s_obj ->rollno = 26 ; //*(ptr_s_obj).rollno=26;
ptr_s_obj -> houseno = 24 ; //*(ptr_s_obj).houseno=26;
ptr_s_obj->show(); //*(ptr_s_ibj).show();
getch();
}
8
This Pointer :
• this is a keyword that refers to the current
instance of the class.
• Main usage of this keyword in C++:
1. When local variable’s name is same as
member’s name.
2. To return reference to the calling object.
13
Virtual Function :
• A virtual function (also known as virtual methods) is a
member function that is declared within a base class and is re-
defined (overridden) by a derived class.
• When you refer to a derived class object using a pointer or a
reference to the base class, you can call a virtual function for
that object and execute the derived class’s version of the
method.
Example :
class A
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
void main()
{
clrscr();
A* a,a_obj; //pointer of base class
B b; //object of derived class Output:
a = &b; Derived Class is invoked
a->display(); //Late Binding occurs Base Class is invoked
a_obj.display();
getch();
}
17
int main()
{
B* b = new D; // Base class pointer
D* d = dynamic_cast<D*>(b); // Derived
class pointer
d->fun();
25
Example of typeid operatr :
#include <typeinfo>
#include <iostream>
#include<conio.h>
using namespace std;
class Base {
public:
virtual void vvfunc( ) { }
};
class Derived : public Base { };
void main( ) {
Derived* pd = new Derived;
Base* pb = pd;
cout << typeid(pb).name( ) << endl; //prints "class Base *"
cout << typeid(*pb).name( ) << endl; //prints "class Derived"
cout << typeid(pd).name( ) << endl; //prints "class Derived *"
cout << typeid(*pd).name( ) << endl; //prints "class Derived"
getch( );
26
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 stream.
• A stream is a sequence of bytes.
• It acts either as source from which the input data can be obtained
or as a destination to which the output data can be sent.
• The source stream that provides data to the program is called the
input stream and the destination stream that receives output from
the program is called the output stream. In other words a
program
27
• 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. Theses are
called stream classes.
• These classes are declared in the header file iostream.h. This file
should be included in
• all the programs that communicate with the console unit.
• The ios is the base class for istream (input stream) and ostream
(output stream) The class ios is declared as virtual base class so
that only one copy of its members are inherited by the iostream.
31
Functions Description
getline(char* arr, int size) Reads a line of characters, entered by the user at
the console which ends with a newline character
or until the size of .
string city1;
getch();
return 0;
}
35
Function Description
width(int) Used to set the width in number of character spaces
for the immediate output data.
setf(format flags) Used to set various flags for formatting output like
showbase, showpos, oct, hex, etc.
Use of Manipulators :
• Manipulators are special functions that can be included in the I/O
statements to alter the format parameters of a stream. Following
table shows some important manipulators functions that are
frequently used. To access manipulators, the file iomanip.h should
be included in the program.
• Advantages and Purpose of Manipulators
1. It is mainly used to make up the program structure.
2. Manipulators functions are special stream function that changes
3. Certain format and characteristics of the input and output.
4. To carry out the operations of the manipulators <iomanip.h>
must be included.
5. Manipulators functions are specially designed to be used in
conjunction with insertion (<<) and extraction (>>) operator on
stream objects.
6. Manipulators are used to changing the format of parameters on
streams and to insert or extract certain special characters.
40
#include<iomanip>
#include<iostream>
using namespace std;
int main()
{
cout<< setw(10) << 1 << endl;
cout<< setw(10) << 10 << endl;
cout<< setw(10) << setfill('*')<< 100 << endl;
cout<< setprecision(4) << 22/7.0 << endl;
cout<< setw(5) << setiosflags(ios::left)<<"Hello"<< endl;
cout<< setw(20) << resetiosflags(ios::right)<<"Hello"<< endl;
}