Demonstrating the class member access operators . and ->
#include <iostream>
using std::cout;
using std::endl;
// Simple class Count
class Count {
public:
int x;
void print() { cout << x << endl; }
};
int main()
{
Count counter,
*counterPtr = &counter,
&counterRef = counter;
cout << "Assign 7 to x and print using the object's name: ";
counter.x = 7;
counter.print();
cout << "Assign 8 to x and print using a reference: ";
counterRef.x = 8;
counterRef.print();
cout << "Assign 10 to x and print using a pointer: ";
counterPtr->x = 10;
counterPtr->print();
return 0;
}
Related examples in the same category