How to Access Private Variable in C++? Last Updated : 21 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, Private members of a class are accessible only within the class they are declared and by friend functions, they are not accessible outside the class not even by derived classes. In this article, we will learn how to access private variables in C++. Accessing Private Variables From Class in C++In C++, we can access private variables through public getter function. These public methods provide us with controlled access to private variables. The getter function returns the value of the private variable, and the setter function sets the value of the private variable. Syntax of Getterclass ClassName {private: type var_name; public: type get() { return var_name; } };Here, public is an access specifier that allows access outside the class.type is a data type of the value that the function will return.get is a name of the public getter function (can be any name).var_name is the name of private variable.C++ Program to Access a Private Variable The below example demonstrates the use of getter to access a private variable in C++. C++ // C++ program to demonstrate how to access private variable #include <iostream> using namespace std; // define a class named MyClass class MyClass { private: // private variable int myVar; public: MyClass(int val = 10) { myVar = val; } // Getter int getVar() { return myVar; } }; int main() { // creating object of MyClass MyClass obj(11); // Accessing value of private variable cout << "Private Variable Value : " << obj.getVar() << endl; return 0; } OutputPrivate Variable Value : 11 Time Complexity: O(1) Auxiliary Space: O(1) Note: Although in C++, it is possible to access private variables with public getter and setter functions, it is important to remember that these variables are declared private for some reason. Therefore, access to private variables should be avoided outside the class. Comment More infoAdvertise with us Next Article How to Access Private Variable in C++? D denzirop9v Follow Improve Article Tags : C++ Programs C++ cpp-class C++-Class and Object CPP Examples +1 More Practice Tags : CPP Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We 9 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio 10 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Like