Is assignment operator inherited?

2.8

In C++, like other functions, assignement operator function is inherited in derived class.

For example, in the following program, base class assignment operator function can be accessed using the derived class object.

#include<iostream>

using namespace std;

class A {
 public:
   A & operator= (A &a) { 
    cout<<" base class assignment operator called "; 
    return *this;
   }
};

class B: public A { };

int main()
{
  B a, b;
  a.A::operator=(b); //calling base class assignment operator function 
                    // using derived class
  getchar();
  return 0;
}

Output: base class assignment operator called

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.8 Average Difficulty : 2.8/5.0
Based on 15 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.