Question

In: Computer Science

What will happen if you forget to implement the pure virtual function in the derived class?...

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?

Solutions

Expert Solution

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:-

  • Function overloading:-  It is done by using functions with same name but different parameters. The definition of function is choosen on the basis of number or type of arguments passed.

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;
}

  • Operator overloading: It is done by using the operators with special meaning for different data types. For Example:- Using " + " to add (concatenate) two strings.

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:-

  • Virtual functions or Function Overriding : This is done by defining a function as virtual function in base class and then overriding or redefining it in derived class.

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(::).


Related Solutions

1 What is a pure virtual function? Why would you define a pure virtual function? Give...
1 What is a pure virtual function? Why would you define a pure virtual function? Give an example of a pure virtual function. 2 When a function has an object parameter that it doesn't modify, what is the best way to declare the parameter (in the function signature), and why? 3 Show an example using a function with a string object parameter. a) When a class has objects as data members, why should its constructors use initialization lists (member and...
In C++ write a program with a base class thet has a pure virtual function SALARY,...
In C++ write a program with a base class thet has a pure virtual function SALARY, and two derived classes. In the first derived class salary is increased by 20%, in the second derived class salary is increased by 30%
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
class Function {     public:     virtual double compute(double value) = 0;     virtual double differenciate(double value) = 0;...
class Function {     public:     virtual double compute(double value) = 0;     virtual double differenciate(double value) = 0;     virtual double integrate(double value) = 0; }; class Sine : public Function {     public:     double compute(double value); // compute sin(x) for a given x     double differenciate(double value); // compute derivative sin'(x) for a given x     double integrate(double value); // integrate sin(x) for a given x }; class Tangent : public Function {     public:     double compute(double value); // compute tan(x) for a given x     double...
What option in Virtual Box allows you to add additional virtual hard disk to a virtual...
What option in Virtual Box allows you to add additional virtual hard disk to a virtual machine? (Take screenshots of the steps)
lab requires you to use Oracle VIEW to implement a virtual database on DBSEC schema, for...
lab requires you to use Oracle VIEW to implement a virtual database on DBSEC schema, for example, on CUSTOMER table. Your task is to develop a single SQL script that will perform all the following tasks: Table created for this assignment is listed below: create table CUSTOMER_VPD( SALES_REP_ID NUMBER(4), CUSTOMER_ID NUMBER(8) NOT NULL, CUSTOMER_SSN VARCHAR(9), FIRST_NAME VARCHAR(20), LAST_NAME VARCHAR(20), ADDR_LINE VARCHAR(40), CITY VARCHAR(30), STATE VARCHAR(30), ZIP_CODE VARCHAR(9), PHONE VARCHAR(15), EMAIL VARCHAR(80), CC_NUMBER VARCHAR(20), CREDIT_LIMIT NUMBER, GENDER CHAR(1), STATUS CHAR(1), COMMENTS...
Using STL stack class, implement in C++ a function that checks for balanced braces { },...
Using STL stack class, implement in C++ a function that checks for balanced braces { }, in a given string / arithmetic expressions.
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
In this homework you will implement a Library class that uses your Book and Person class...
In this homework you will implement a Library class that uses your Book and Person class from homework 2, with slight modifications. The Library class will keep track of people with membership and the books that they have checked out. Book.java You will need to modify your Book.java from homework 2 in the following ways: field: dueDate (private)             A String containing the date book is due.  Dates are given in the format "DD MM YYYY", such as "01 02 2017"...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT