user14148022
user14148022

Reputation:

How to access a variable in a method of a different class in c++

How to access a variable in a method in a different class for example in this code.

#include <iostream>
using namespace std;
class Student
{
public:
    void Addition()
    {
        int c=17;
    }
};
int main()
{
    
}

How can i access variable c in Addition method in class Student without returning the value.

Thanks For Reading.

Have A Nice Day (:

Upvotes: 0

Views: 81

Answers (1)

Zach_Mose
Zach_Mose

Reputation: 357

You can use a setter and getter in your class, then use them to achieve what you want in the main

private:
  int c;
public:
  void setIntC(int intC) { c = intC; } 
  int getIntC() const { return c; } 

Upvotes: 3

Related Questions