Escolar Documentos
Profissional Documentos
Cultura Documentos
Overriding &
Virtual
Function in
C++
Method Overriding & Virtual Function in C++
Dr. Munesh
Singh
Method
Overriding &
Virtual
Function in
C++
Dr. Munesh
Singh
Method
Overriding & class A
Virtual
Function in {
C++
Dr. Munesh
public:
Singh void f1() {}
void f2() {}
};
class B:public A
{
void f1() {} //method overriding
void f2(int x) {} //method hiding
};
void main() {
B obj;
obj.f1(); //B
obj.f2(); // error
obj.f2(4) //B };
Base Class Pointer
Method
Overriding &
Virtual
Function in
C++
Dr. Munesh
Singh Base class pointer can point to the object of any of its
descendant class
But its converse is not true
But in C++ we see the pointer in different role
When pointer of base class is created, then it can keep the
address of the child class
But child class pointer can not point to the parent class
pointer
Method Overiding
Method
Overriding &
Virtual
class A{
Function in public:
C++
Dr. Munesh
virtual void f1() {} };
Singh class B:public A {
public: void f1() {} //method overriding
};
void main() {
A *p, obj1; // During the compile time pointer type define
the address of class
B obj2//B
p=&obj1;
p=&obj2;
p− >f1(); // compiler can not know the address of which
class
obj.f2(); // error
obj.f2(4) //B };
Virtual Function
Method
Overriding & If we define the pointer of class A type and later we assign
Virtual
Function in the address of class B object into the pointer, then class A
C++
Dr. Munesh
function will be called
Singh
This is a problem of compile time binding
It occurs only in method overriding case
During the compile time, compiler bind the address of
class A based on pointer type, not the assigned address
To resolve such issue the virtual function concept is
introduced
Virtual function do the runtime binding, and resolve the
method overriding issues
If we write virtual before the method of parent class, then
late binding allow the compiler to refer the address of the
object and call appropriate function of a class
Virtual Function
Method
Overriding &
Virtual
main()
Function in
class A{ {
vtable
C++ *vptr A *p,obj;
Dr. Munesh public: f2 static array
Singh
p=&obj;
void f1(){} obj p->f2(); EB
virtual void f2(){} virtual function address p p->f3(); LB
virtual void f3(){} p->f4(); LB
virtual void f4(){} *vptr
p->f4(5);EB
}; vtable
class B: public A{ f2 static array main(){
public: A *p;
void f1(){}; function address B obj
void f2(){}; p=&obj;
void f4(int x){}; p->f1(); EB
}: p->f2(); LB
p->f3(); LB
p->f4(); LB
p->f4(5);EB
Abstract Class in C++
Method
Overriding &
Virtual
Function in
C++
Pure Virtual Function
Dr. Munesh
Singh A do nothing function is pure virtual function
class Person {
public:
virtual void fun()=0;
};
class student: public Person {
public:
void fun() {}
};
Abstract Class
Method
Overriding &
Virtual
Function in
C++
Dr. Munesh
Pure Virtual Function
Singh
A class containing pure virtual function is an abstract class
we can not instantiate abstract class
Method
Overriding &
Virtual
Function in
C++
Dr. Munesh
Singh
The keyword template is used to define function template
and class template
It is a way to make your function or class generalize as far
as data type is concern.
Use of temples
Function Template
Class Template
Template Example
Method
Overriding &
Virtual
int big(int a,int b)
Function in {
C++
if (a>b)
Dr. Munesh return a; template <class x> x big(x a,x b)
Singh
else {
return b; if (a>b)
} return a;
double big(int a,int b) else
{ return b;
if(a>b) }
return a; int main()
else {
return b; cout<<big(2,5);
} cout<<big(5.5,7.5);
int main() }
{
template <class x,class y> x big(x a,y b)
cout<<big(2,5);
cout<<big(5.5,7.5);
}
Class Template
Method
Overriding &
Virtual
Function in
C++
Dr. Munesh
Singh
Method
Overriding & template <class x> class ArrayList
Virtual {
private:
Function in list1
struct controlblock
C++ capacity
{
int capacity; s
Dr. Munesh
int *arr_ptr; arr_ptr
Singh
};
controlblock *s;
public:
ArrayList(int capacity)
{
s=new controlblock;
s->capacity=capacity;
s->arr_ptr=new int[s->capacity];
}
void addelement(int index,int data)
{
if(index>=0 && index<=s-> capacity-1)
s->arr_ptr[index]=data;
else
count<"array index is not valied"; int main()
} {
void viewlist() ArrayList <int> list1(4);
{ list1.addelement(0,32);
int i; list1.viewlist();
for(i=0;i<s->capacity;i++) }
cout<<""<<s->arr_ptr[i];
}
};
File Handling in C++
Method
Overriding & Data Persistence(life of data)
Virtual
Function in
C++
Dr. Munesh
Singh
Streams
Streams
Method
Overriding &
Virtual
Function in Data Persistence(life of data)
C++
Dr. Munesh
Singh
Output Streams Example
Method
Overriding & Data Persistence(life of data)
Virtual
Function in
C++ #include<fstream..h>
Dr. Munesh
Singh
void main()
{
ofstream fout;
fout.open("myfile.data);
fout<<"hello";
fout.close();
}
Input Streams Example
Method
Overriding & Data Persistence(life of data)
Virtual
Function in
C++
#include<fstream..h>
Dr. Munesh void main()
Singh
{
char ch;
ifstream fin;
fin.open("myfile.dat");
fin>>ch; //ch=fin.get();
while(!fin.eof())
{
cout<<ch;
fin>>ch;
}
fin.close();
}
File Opening Mode
Method
Overriding &
Virtual
Function in
C++
Dr. Munesh
Singh
File Opening Mode
Method
Overriding &
Virtual
Function in
C++
Dr. Munesh
Singh
File Opening Mode
Method
Overriding &
Virtual
Function in
C++
Dr. Munesh
Singh