Write short notes on:

  1. Cascading of IO operators
  2.  Pure Virtual Function

This answer is restricted. Please login to view the answer of this question.

Login Now

a) Cascading of IO operators

When an object calls an operator function by passing an argument and the returned value of the operator function calls the next operator function in the same expression, it is called as cascading of operators. Below are the examples for illustration of Cascading Of Operations:

#include <iostream>
using namespace std;

class Height{
    private:
        int feet, inches;

    public:
        Height(){
            feet = 0;
            inches = 0;
        }

        void setData(int x, int y){
            feet = x;
            inches = y;
        }

        void showData(){
            cout << feet << "'" << inches;
        }
        
        Height operator+(Height H){
            Height temp;
            
            temp.feet = feet + H.feet;
            temp.inches = inches + H.inches;

            return temp;
        }

        void normalize(){
            if (inches == 12 || inches > 12){
                feet = feet + inches / 12;
            }

            inches = inches % 12;
        }
};

int main(){
    Height h1, h2, h3, h4;

    h1.setData(5, 9);
    h2.setData(5, 2);
    h3.setData(6, 2);

    // Add all the heights using
    // cascading of operators
    h4 = h1 + h2 + h3;

    h4.normalize();
    h4.showData();

    return 0;
}

In this code, Cascading of the operator is taking place here:

h4 = h1 + h2 + h3;

Here at first h1 object called (+) operator and passes h2 as an argument in the operator function call and the returned value of this operator function calls again (+) operator and passes h3 as an argument in the same expression, at last, the returned value of this second operator function is assigned in h4.

b) Pure Virtual Function

The virtual function that is only declared but not defined in the base class is called the pure virtual functions.

A function is made pure virtual by preceding its declaration with the keyword virtual and by post fixing, it with = 0.

The pure virtual function in c++ simply tells the compiler that the function is pure. The class that contains the pure virtual function in c++ exists only to act as a parent or base of the derived classes. This function is implemented in the derived classes.

Let’s take an example for better understanding.

We can change the virtual function area() in the base class to the following −

class Shape {
   protected:
      int width, height;

   public:
      Shape(int a = 0, int b = 0) {
         width = a;
         height = b;
      }
      
      // pure virtual function
      virtual int area() = 0;
};

The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .