Accessing private variables of a class

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Class Variables
AI Thread Summary
To access private variables of a class from another class in C++, one can use the concept of friend classes. By declaring class Two as a friend of class One, class Two gains access to the private members of class One, including the private array. This allows methods in class Two, like printNumbers(), to directly access and manipulate the private data of class One. The discussion also references the Wikipedia page on friend classes for further clarification. Understanding this concept is essential for effective class design and encapsulation in C++.
magnifik
Messages
350
Reaction score
0
how can i access the private variables of a class from another class?

for example...
Code:
class One{
  public:
     One();
  private:
     int array[6][7]    
};

class Two{
  public:
     Two();
  private:
     void printNumbers();
};

void Two::printNumbers(){
     // i want to access the array in class One here
}
 
Physics news on Phys.org
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
2
Views
2K
Replies
2
Views
1K
Replies
5
Views
3K
Replies
8
Views
1K
Replies
2
Views
1K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
12
Views
2K
Back
Top