Accessor functions : accessor functions « Class « C++ Tutorial






#include <iostream>
 class MyClass {
 public:                     // begin public section
     int getInt();           // accessor function
     void setint (int age);  // accessor function
     void display();         // general function
 private:                    // begin private section
     int itsAge;             // member variable
 };
 
 int MyClass::getInt()
 {
     return itsAge;
 }
 
 void MyClass::setint(int age)
 {
     itsAge = age;
 }
 
 void MyClass::display()
 {
     std::cout << "display.\n";
 }
 
 int main()
 {
     MyClass obj;
     obj.setint(5);
     obj.display();
     std::cout << obj.getInt() << " years old.\n";
     obj.display();
     return 0;
 }
display.
5 years old.
display.








9.6.accessor functions
9.6.1.Accessor functions
9.6.2.Access private data through accessor functions.