Define pure virtual functions. Write a C++ program to illustrate pure virtual functions.With an example, explain the syntax for passing arguments to base class constructors in multiple inheritance.
Question
Define pure virtual functions. Write a C++ program to illustrate pure virtual functions.With an example, explain the syntax for passing arguments to base class constructors in multiple inheritance.
Solution 1
- Pure Virtual Functions: In C++, a pure virtual function (or abstract function) in a base class is declared by assigning 0 in declaration. It makes it mandatory for the derived classes to define the function. A class containing pure virtual function is called abstract class.
Here is the syntax to declare pure virtual function:
class Base
{
int x;
public:
virtual void show() = 0; // Pure Virtual Function
};
In the above syntax, = 0 tells the compiler that function is pure virtual.
- C++ Program to illustrate pure virtual functions:
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0; // Pure Virtual Function
};
class Derived: public Base
{
public:
void show()
{
cout<<"Implementation of Virtual Function in Derived class";
}
};
int main()
{
Base *b;
Derived d;
b = &d;
b->show();
return 0;
}
In the above program, show() is a pure virtual function in the base class and it is defined in the derived class. The base class pointer b is used to call the function.
- Syntax for passing arguments to base class constructors in multiple inheritance: In multiple inheritance, a class is derived from more than one base class. The derived class inherits all the features of the base classes. The constructors of base classes are called in the same order as they are inherited.
Here is the syntax:
class Derived: public Base1, public Base2
{
public:
Derived(int a, int b): Base1(a), Base2(b)
{
}
};
In the above syntax, Derived is a class derived from Base1 and Base2. The constructor of Derived takes two parameters a and b and passes them to the constructors of Base1 and Base2 respectively.
Solution 2
- Pure Virtual Functions: In C++, a pure virtual function (or abstract function) in a base class is declared by assigning 0 in declaration. It makes it mandatory for the derived classes to define the function. A class containing pure virtual function is called abstract class.
Here is the syntax to declare pure virtual function:
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
In the above code, fun() is a pure virtual function.
- C++ Program to illustrate pure virtual functions:
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0; // Pure Virtual Function
};
class Derived: public Base
{
public:
void show()
{
cout<<"Implementation of Virtual Function in Derived class";
}
};
int main()
{
Base *b;
Derived d;
b = &d;
b->show();
return 0;
}
In the above program, show() is a pure virtual function in the base class and it is implemented in the derived class.
- Syntax for passing arguments to base class constructors in multiple inheritance:
In multiple inheritance, a class is derived from more than one base class. If these base classes have parameterized constructors, arguments to these constructors must be passed from derived class constructor.
Here is an example:
#include<iostream>
using namespace std;
class Base1
{
public:
Base1(int i)
{
cout<<"Base1's constructor called";
}
};
class Base2
{
public:
Base2(int j)
{
cout<<"Base2's constructor called";
}
};
class Derived: public Base1, public Base2
{
public:
Derived(int a, int b): Base1(a), Base2(b)
{
cout<<"Derived's constructor called";
}
};
int main()
{
Derived d(10, 20);
return 0;
}
In the above code, Derived class is derived from Base1 and Base2. Both base classes have parameterized constructors, so arguments to these constructors are passed from Derived class constructor.
Similar Questions
Which of the following is an example of a class with a pure virtual function in C++?
In C++, polymorphism requires :Inheritance onlyVirtual functions only References onlyInheritance, Virtual functions and reference
In C++, parametric polymorphism is implemented using:Virtual functionsFunction overloadingTemplate syntaxClass inheritance
Which of the following is true about pure virtual functions?They must be defined in the base class.They can have a definition in the base class.They make a class abstract and cannot be instantiated.They are defined using the keyword pure.
What is the issue with the following code snippet using virtual functions? #include <iostream> class Base { public: virtual void print() const { std::cout << "Base\n"; } }; class Derived : public Base { public: void print() { std::cout << "Derived\n"; } }; int main() { Base* ptr = new Derived(); ptr->print(); delete ptr; return 0; } (2 Points) a) No issue b) Missing const qualifier c) Incorrect virtual function declaration d) Missing virtual destructor
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.