C++: Assessing class members from main

This entry is part 52 of 61 in the series C++

We have always say that a public member of a class can by assessed by any functions.

A good way to look at it is in accessing it from main function.

In main, only public members can be accessed.  It would generate an error if you try to access a private or a protected member.

#include <iostream>

using namespace std;

class Music {
    int x;
    int y;
    int z;

int main() {
    Music music;
    music.x = 6;
    cout << music.x;
    // Main will not be able to access y & z.
    // music.y = 8;   //error
    // music.z = 10; //error


Series Navigation<< C++: Applying the 3 classes to the base class
C++: Protected members and functions >>