C++ operator overloading and private members

  • Context: C/C++ 
  • Thread starter Thread starter sir_manning
  • Start date Start date
  • Tags Tags
    C++ Members Operator
Click For Summary
SUMMARY

The discussion centers on C++ operator overloading, specifically the implementation of the addition operator (+) within a class named "number." The code demonstrates that the operator function can access private members of another instance of the same class due to the nature of member functions in C++. The user initially questioned a privacy violation, but it was clarified that member functions have access to all private members of any instance of their class, which is a fundamental aspect of C++ encapsulation.

PREREQUISITES
  • Understanding of C++ class structures and member functions
  • Knowledge of operator overloading in C++
  • Familiarity with access specifiers in C++ (public, private)
  • Basic understanding of C++ constructors
NEXT STEPS
  • Study C++ operator overloading in detail, focusing on different operators
  • Learn about C++ access specifiers and their implications on encapsulation
  • Explore the concept of friend functions in C++ for accessing private members
  • Practice writing C++ classes with various operators and access levels
USEFUL FOR

C++ developers, computer science students preparing for exams, and anyone interested in understanding operator overloading and class encapsulation in C++.

sir_manning
Messages
64
Reaction score
0
Good evening

I'm in the midst of reviewing for my exam, I've come across a piece of code I don't understand. It is as follows:

//////////////////////////
#include <iostream>
using namespace std;

class number{

private:
int value;

public:
number(int v){value = v;}

void operator + (const number & num){
value = value + num.value; //<-what is going on here?
}

void print(){count<<"\n"<<value<<"\n";}

};

int main(void){
number five(5);
number six(6);

five+six;
five.print();

return 0;
}

////////////////////////

This code compiles and gives an output of 11. However, it seems to me that there is a privacy violation on the line indicated. How is it that the operator function can access a private member ("value") of another number class ("num")?

Thanks.
 
Technology news on Phys.org
sir_manning said:
How is it that the operator function can access a private member ("value") of another number class ("num")?
That's not accurate: it is accessing a private member ("value") of a different instance ("num") of the same class ("number"). Since operator+ is a member of number, it has access to all of the private members of any instance of the number class.
 
Aha... Thanks
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
8K
Replies
53
Views
5K
Replies
5
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K