Special welcome gift. Get 50% off your first courses with code “AS50”. Find out more!

HomeInterviewC++ Q/AHow to access private members of a class in C++?
courses 10

How to access private members of a class in C++?

Private members of the class are not accessible by object or function outside the class. Only functions inside the class can access them or friend functions. However, pointers can be used to access private data members outside the class.

The sample code is as follows:

#include <iostream> 

using namespace std; 

class sample_test{ 

private: 

    int n; 

public: 

    sample_test() { n = 45; } 

    int display() { 

return n; 

     } 

};

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

Categories

ads sidebar 1

You May Also Like

Oracle has several modes for shutting down the database: In normal mode, the database is shut down by default. It...
Materialized views are items that contain condensed sets of data from base tables that have been summarized, clustered, or aggregated. They...
Every database in Oracle has a tablespace called SYSTEM, which is generated automatically when the database is created. It also...