Saturday, April 19, 2025

C++ OO Private vs Public Access

In C++, private and public are access modifiers that determine the level of access to the members (data and functions) of a class from outside of the class.

public members can be accessed from outside the class by any function or object that has access to an instance of that class. private members, on the other hand, can only be accessed by member functions of the same class.

In general, public members are used to provide an interface for external code to interact with the class, while private members are used for internal implementation details that should not be exposed to external code.

By using access modifiers, we can encapsulate data and functionality within a class, protecting the implementation details and promoting code reusability and maintainability. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string someString = "Constant Stuff";

        void printStr() {
            cout << someString << endl;
        }
};


int main() {

    Cls Object;
    Object.printStr();

    return 0;
}

In this code, the Cls class has a private data member someString and a private member function printStr(). The private members of a class can only be accessed by the member functions of that class and not by any other function or object outside the class.

In the main() function, an object of the Cls class is created. However, when the private member function printStr() is called on this object, the compiler generates an error because it is not allowed to access the private member functions outside the class.

Therefore, the program will not compile and will generate an error message: 


||=== Build: Debug in Main (compiler: GNU GCC Compiler) ===|
C:\Users\user\Desktop\Main\main.cpp|7|warning: non-static data member 
initializers only available with -std=c++11 or -std=gnu++11|
C:\Users\user\Desktop\Main\main.cpp||In function 'int main()':|
C:\Users\user\Desktop\Main\main.cpp|9|error: 'void Cls::printStr()' is private|
C:\Users\user\Desktop\Main\main.cpp|18|error: within this context|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

The code produces a warning and two errors.

The first error indicates that the printStr function is private, which means that it cannot be accessed outside the class. Since main() is trying to call the function on an object of the Cls class, this causes an error.

The second error is a consequence of the first one, and it indicates that the attempt to call the private printStr function is within an invalid context. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string someString = "Constant Stuff";

    public:
        int Num = 5;

        void printStr() {
            cout << someString << endl;
        }

        void printNum() {
            cout << Num << endl;
        }
};

int main() {

    Cls Object;
    Object.printStr();

    Object.printNum();

    return 0;
}

This code defines a class Cls that has two member functions, printStr() and printNum(), and two data members, someString and Num.

The data member someString is declared as private, which means it can only be accessed by member functions of the class itself. On the other hand, the data member Num is declared as public, which means it can be accessed from outside the class as well.

The member function printStr() simply prints out the value of the private data member someString using the cout statement.

The member function printNum() prints out the value of the public data member Num using the cout statement.

In the main() function, an object of the Cls class is created and its member functions printStr() and printNum() are called to print out the values of the private and public data members of the object, respectively.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...