Accessing the class variables in main function in C++

In this code we are going to take a look on accessing the class variables in main function

Source code :

#include <iostream>

using namespace std;

class myClass{

public:

    int a,b;
    
    int add(){
        
    cout<<"Value of A is "<<a<<" B is "<<b<<"";
    cout<<"\nThe Sum is "<<a+b;
    
    return 0;
    }

};

int main(){
    

    myClass obj;
    obj.a=10;
    obj.b=20;
    obj.add();

return  0;
}


Output :

Value of A is 10 B is 20
The Sum is 30

Output :


                

Comments :