0% found this document useful (0 votes)
17 views4 pages

C++Programs(18-12-2020)

Prgrmffgv

Uploaded by

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

C++Programs(18-12-2020)

Prgrmffgv

Uploaded by

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

C++ Programming

Non Virtual Function in C++


1. Non-virtual member functions are resolved statically.
2. Member functions are binding statically at compile-time based on the type of the pointer (or
reference) to the object.

Program 221
//221.Example of Non Virtual Function in c++
#include<iostream.h>
#include<conio.h>
class A
{
public:
void print()
{
cout <<"Print() of base class A called" << endl;
}
void show()
{
cout << "Show() of base class A called"<< endl;
}
};
class B : public A
{
public:
void print()
{
//A::print();
cout << "Print() of derived class B called " << endl;
}

void show()
{
cout << "Show() of derived class B called" << endl;
}
};

void main()
{
A *parent;
B child;
//Method 1(assignment of address of child to pointer of base class)
parent = new B();
clrscr();
// Non virtual function, binded at compile time
parent->print(); /*here print() of parent Class A will be called because pointer is belonging to Base class
A */
((B*)parent)->print();
//Method 2(assignment of address of child to pointer of base class)
parent=&child;

1|Page 18-12-2020
C++ Programming
// Non-virtual function, binded at compile time
parent->show(); /*here show() of parent Class A will be called because pointer is belonging to
Base class A.*/
getch();
}
/*Output
Print() of base class A called
Print() of derived class B called
Show() of base class A called
*/
=====================================================================
Virtual Function in C++
1. A virtual function is a member function which is declared within a base class and is re-
defined (Overridden) by a derived class.
2. 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
function.
3. It is declared using the virtual keyword.
4. When the function is made virtual, C++ determines which function is to be invoked at
runtime based on the type of the object pointed by the base class pointer.
5. They are mainly used to achieve Runtime polymorphism or Late Binding or Dynamic
Binding.

Polymorphism(Many Forms)
1. Early Binding or Compile Time (Function Overloading)
2. Late Binding or Dynamic Time (Virtual Function)
Program 222
//222.Example of Virtual Function in c++
#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void print()
{
cout << "Print() of base class A called" << endl;
}
virtual void show()
{
cout << "show() of base class A called" << endl;
}
};
class B : public A
{
public:
void print()
{
cout << "print() of derived class B is called" << endl;
}
};
void main()
2|Page 18-12-2020
C++ Programming
{
A *parent;
clrscr();
B child;
parent = new B();
// virtual function, binded at Run time
parent->print();//here print() of child Class B will be called because print() is virtual fun
in Base Class A

// virtual function, binded at compile time


parent->show(); //here show() of parent Class A will be called because pointer is belonging
to Base class A and show() is virtual function in Base Class A but it is not exists in class B.
getch();
}

Rules for Virtual Functions


1. Virtual functions cannot be static and also cannot be a friend function of another class.
2. Virtual functions should be accessed using pointer or reference of base class type to
achieve runtime polymorphism.
3. The prototype of virtual functions should be same in base as well as derived class.
4. It is not mandatory for derived class to override (or re-define the virtual function).
5. A class may have virtual destructor but it cannot have a virtual constructor.
6. They are accessed through object pointers.

Program 223
//223.Example of Virtual Function in c++
#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void display()
{
cout<<" \n Display of class A called ";
}
};
class B:public A
{
public:
/*void display()
{
cout<<"\n Display of B called";
}*/
};
class C:public A
{
public:
void display()
{
cout<<"\n Display of Class C called ";
}
3|Page 18-12-2020
C++ Programming
};
void main()
{
clrscr();
A *p=new B(); //pointer to object
p->display();
delete p;
//p=NULL;
p=new C;
p->display();
getch();
}
/*Output=
Display of Class A called
Display of Class C called*/

4|Page 18-12-2020

You might also like