In: Computer Science
What will happen if you forget to implement the pure virtual function in the derived class?
How the following are achieved in C++? static polymorphism and dynamic polymorphism.
What is the advantage of defining the member functions outside a class rather than defining inside a class?
1-For the first part of the question let's first discuss what pure virtual function really is-
A pure virtual function in C++ is a virtual function for which we don’t have implementation, we only declare it and do not define it. This is done by assigning 0 in declaration. If any class has atleast one pure virtual class it is treated as abstract class ,that is we cannot define an object for this class.Therefore,if we forget to implement the pure virtual function in the derived class then derived class also becomes abstract class.
Screenshot of code showing error while declaring object of derived class:-
Code snippet:
#include<iostream>
using namespace std;
class Base
{
public:
virtual void PVfun() = 0; //pure virtual
function
};
class Derived : public Base { }; //derived class from base class
int main()
{
Derived d; //try to declare object of derived class
return 0;
}
2-Static polymorphism:This is also known as compile time polymorphism or early binding. In c++ it is implemented using:-
Screenshot of code showing function overloading:-
Code snippet:-
#include <bits/stdc++.h>
using namespace std;
class example
{
public:
// function with 2 int parameter
void sum(int x,int y)
{
cout << "sum= " << x +
y<< endl;
}
// function with same name but 3 int parameter
void sum(int x,int y,int z)
{
cout << "sum= " << x +
y+z<< endl;
}
};
int main() {
example obj1;
// Which function is called will depend on the
parameters passed
obj1.sum(7,8); // The first 'func' is called
obj1.sum(7,8,9); // The second 'func' is
called
return 0;
}
Screenshot of code showing Operator overloading
Code snippet:
#include<iostream>
using namespace std;
class set { //
private:
int a,b;
public:
set(int x, int y) {a = x; b = y;} //constructor
set operator + (set const &obj) //operator
overloading
{
set res(0,0);
res.a = a + obj.a;
res.b = b + obj.b;
return res;
}
void print() { cout << "set= ("<<a
<< " ," << b <<")"<< endl; }
};
int main()
{
set s1(10, 5), s2(2, 4);
set s3 = s1 + s2; // An example call to
"operator+"
s3.print();
}
Dynamic polymorphism: This is also known as run time polymorphism or late binding. In c++ it is implemented using:-
Screenshot of code showing dynamic polymorphism
Code snippet:
#include <bits/stdc++.h>
using namespace std;
class base
{
public:
virtual void print () // defining print() as virtual
function
{ cout<< "This is base class" <<endl;
}
};
class derived:public base
{
public:
void print () //redefinig print() in derived
class
{ cout<< "This is derived class" <<endl;
}
};
//main function
int main()
{
base *bptr;
derived d;
bptr = &d;
//virtual function, binded at runtime (Runtime
polymorphism)
bptr->print();
return 0;
}
3- The advantage of defining the member functions outside a class rather than defining inside a class is that it makes our code readable and well structured. Suppose we are woking on a big project which has a class having 30 member functions. In this case if we define all the functions inside the class , it will make the code confusing and will be very difficult for us to make any changes in the code or understanding the functionalities of that class therefore we define these functions outside the class. This is done by using scope resolution operator(::).