Getters and Setters
class MyClass {
private:
int myNum;
public:
void setMyNum(int num) { // Setter
myNum = num;
}
int getMyNum() { // Getter
return myNum;
}
};
MyClass myObj;
myObj.setMyNum(15); // Set the value of myNum to 15
cout << myObj.getMyNum() << endl; // Output 15
Comments