![]() |
A Beginner's Guide to Creating a Virtual Function in C++ |
A Beginner's Guide to Creating a Virtual Function in C++
Virtual Function: A virtual function is a member function that is
declared within base class and is re-defined by derived class. Virtual
functions ensure that the correct function is called for an object, regardless
of the type of reference (or pointer) used for function call. Functions are
declared with a virtual keyword in base class. They are mainly used to achieve
Runtime polymorphism. The resolving of function call is done at Run-time. It is
used to tell the compiler to perform dynamic linkage or late binding on the
function.
Rules for Virtual Function
1.Virtual functions must be members of some class.2.Virtual functions cannot be static members.
3.They are accessed through object pointers.
4.They can be a friend of another class.
5.A virtual function must be defined in the base class, even though it is not used.
6.The prototype of virtual functions should be same in base as well as derived class.
How To Use without Virtual Keyword in C++?.
#include<iostream> using namespace std; class A { public: void area() { int h=20; int w=30; int a=h*w; cout << "A class
Area:"<<a; } }; class B: public A { public: void area() { int h=20; int w=30; int a=h*w; cout << "B class
Area:"<<a; } }; int main() { A* a; B b; a = &b; a->area(); } *****OUTPUT***** A class Area:600 |
How To Use with Virtual Keyword in C++?
#include<iostream> using namespace std; class A { public: virtual void area() { int h=20; int w=30; int a=h*w; cout << "A class
Area:"<<a; } }; class B: public A { public: void area() { int h=20; int w=30; int a=h*w; cout << "B class
Area:"<<a; } }; int main() { A* a; B b; a = &b; a->area(); } *****OUTPUT***** B class Area:600 |
Pure Virtual Function: A virtual
function is not used for performing any task. It only serves as a placeholder. When
the function has no definition, such function is known as the "do-nothing" function. The "do-nothing" function is known
as a pure virtual function.
A pure virtual function is a function declared in the base class that has no definition relative to the base class. A class containing the pure virtual function cannot be used to declare the objects of its own, such classes are known as abstract base classes.
The main objective of
the base class is to provide the traits to the derived classes and to create
the base pointer used for achieving the runtime polymorphism.
Pure Virtual Function can be declared in C++?
#include<iostream> using namespace std; class A { public: virtual void area()=0; { int h=20; int w=30; int a=h*w; cout << "A class
Area:"<<a; } }; class B: public A { public: void area() { int h=20; int w=30; int a=h*w; cout << "B class
Area:"<<a; } }; int main() { A* a; B b; a = &b; a->area(); } *****OUTPUT***** B class Area:600 |
Friend Function: A Friend function
is defined as a friend function then, the private and protected data of a class
can be accessed using the function. The complier knows a given function is a
friend function by the use of the keyword friend.
For accessing the data, the
declaration of a friend function should be made inside the body of the class
(can be anywhere inside class either in private or public section) starting
with keyword friend.
Syntax of friend function
class className { friend return_type functionName(argument); } return_type functionName(argument) { // Private
and protected data of className can be accessed from // this function because it is a friend
function of className. } |
How To Use friend function in C++?
#include<iostream> using namespace std; class Box { public: int h; int w; int area; Box() { h=10; w=10; } friend void area(Box); }; void area(Box f) { f.area=f.h*f.w; cout<<"Area of
Box:"<<f.area; } int
main() { Box b; area(b); } *****OUTPUT***** Area of Box:100 |
Friend Class: A friend class is a
class that can access the private and protected members of a class in which it
is declared as friend. This is needed when we want to allow a particular class
to access the private and protected members of a class.
How To Use Friend Class in C++?
#include <iostream> using namespace std; class A { int x=100; friend class B; }; class B { public: void display(A &a) { cout<<"Value of x is :
"<<a.x; } }; int
main() { A a; B b; b.display(a); return 0; } *****OUTPUT***** Value of x is :100 |
Static Variable: When a variable is
declared with static keyword ,it is called static variable. When a
variable is declared as static, space for it gets allocated for the lifetime of
the program.
Even if the function is called multiple times, space for The static variable is allocated only once and the value of variable in the previous call gets carried through the next function call.
For better understanding see the example below
How To Use without static keyword in C++?
#include <iostream> using namespace std; void A() { int i =
0; cout << i << " "; i++; } int main() { A(); A(); A(); A(); } *****OUTPUT***** 0 0 0 0 |
How To Use with static keyword in C++?
#include <iostream> using namespace std; void A() { static int i = 0; cout << i << " "; i++; } int main() { A(); A(); A(); A(); } *****OUTPUT***** 0 1 2 3 |
Static Function: When a function is
declared with static keyword is called static function. It can be
called by class name directly using scope resolution(::). There is no need of
object to call a static function.
How To Use Static Function in C++?
#include <iostream> using namespace std; class A { public: static void B() { cout<<"I am inside static
function"; } }; int main() { A::B(); } *****OUTPUT***** I am inside static function |
Exception: To understand the
exception firstly we should know about the error in prgram.Errors in program
can be categorized into two types.
1.Compile Time Errors
2.Run Time Errors
Compile Time Errors: Errors caught
during compiled time is called Compile time errors. Compile time errors include
library reference, syntax error or incorrect class import.
Run Time Errors: The error that
occours during the run time of program is called run time error.
They are also known as exceptions. Hence we can say that exception is a runtime
error that occours because of user's mistake.
Reasons of Exception: Mismatched
Input: Suppose that we are entering our name in place of age, causing exception
because age is of data type int and name will be string.
File does not exist : Suppose that we are
reading a text file easy.txt and that file does not exist in the
system, causing exception.
Exception related to array : Suppose that the array size is 5 and we are inserting more than 5
elements, causing exception.
Divide by zero exception : When a
number is divided by zero then the output will be undefined(infinity).
Exception Handling: The process of
handling the exception is called exception handling. There are three main
keyword are used to solve the problem of exception.
try block : It is the place where
actual code is written and exception occours. when the code will lead to any
error, that error/exception will get caught inside the catch block.
catch block : catch block is
intended to catch the error and handle the exception condition. We can have
multiple catch blocks to handle different types of exception and perform
different actions when the exceptions occur.
throw statement : It is used to show the user defined message about the
exception.
Syntax of try-catch
Try { // protected code } catch( ExceptionName e1 ) { // catch
block } catch( ExceptionName e2 ) { // catch block } catch(
ExceptionName en ) { // catch block } |
How To Use Exception Handling in C++?
#include<iostream> using namespace std; void Exception(int x,int y) { try { if(y!=0) { int z=x/y; cout<<"Division Value:"<<z;
} else throw "Don't put zero in
denominator"; } catch(const char *s) { cout<<s; } } int main() { int
a,b; cout<<"Enter a value:";
cin>>a; cout<<"Enter b value:";
cin>>b; Exception(a,b); } *****OUTPUT***** Enter a value:10 Enter b value:2 Division Value:5 Second Run Program Enter a value:10 Enter b value:0 Don't put zero in denominator |