WINSEM2023-24 ICSE102L TH VL2023240503410 2024-04-30 Reference-Material-I
WINSEM2023-24 ICSE102L TH VL2023240503410 2024-04-30 Reference-Material-I
class.
When we want to use same function name in both the base and
another class.
class.
They are always defined in base class and overridden in derived class. It is not
mandatory for derived class to override (or re-define the virtual function), in
A class may have virtual destructor but it cannot have a virtual constructor.
Virtual Function
A virtual function is a member function that is defined within a base class and redefined by a
derived class.
To create virtual function, precede the function’s in the base class with the keyword virtual.
When a class containing virtual function is inherited, the derived class redefines the virtual
using base class pointer if we call some function which is in both classes, then base class
function is invoked. But if we want to invoke derived class function using base class pointer, it
can be achieved by defining the function as virtual in base class, this is how virtual functions
class class_name
{
public:
-------
--------
virtual returntype fun_name(args)
{
……
……
}
….
};
class Base
{
public: Derived function without virtual
void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
};
void main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show();
}
class A
{
protected:
int a;
public:
virtual void show()
main()
{
{
A *p;
}
B OBJ1;
};
p=&OBJ1;
class B : public A
p->show();
{
}
int b;
public:
B()
{
a=5;
b=10;
}
void show()
{
cout<<"value of a:"<<a;
cout<<"value of b:"<<b;
} };
Class A
{
int a;
public:
A()
{
a = 1;
} main()
virtual void show() {
{ A *p;
cout <<a; A z;
} p=&z;
}; p->show();
Class B: public A B b;
{ p = &b;
int b; p -> show();
public: }
B()
{
b = 2;
}
void show()
{
cout <<b;
}};
Pure Virtual Functions
A pure virtual function is a function which has no definition in the base class.
Its definition lies only in the derived class i.e. it is compulsory for the derived class
Since there is no definition in the base class, these functions can be equated to
zero.
A class containing pure virtual functions cannot be used to define any objects of its
own and hence such classes are called pure abstract classes or abstract classes .
All other classes without pure virtual functions are called concrete classes.
#include <iostream>
using namespace std;
class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};
main()
{
//Base obj; //Compile Time Error
Base *b;
Derived d;
b = &d;
b->show();
}
Abstract Class
A pure virtual function is a function which does not have definition of its own. The
classes which derive from this class need to provide definition for such function.
A class containing at least one such pure virtual function is called as abstract base class.
We can not create objects of abstract class because it contains functions which have no