• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Classes » Accessing Public and private members of C++ classes
Next →
← Prev

Accessing Public and private members of C++ classes

By Dinesh Thakur

The members of a class can be directly accessed inside the class using their names. However, accessing a member outside the class depends on its access specifier. The access specifier not only determines the part of the program where the member is accessible, but also how it is accessible in the program.

Accessing Public Members: The public members of a class can be accessed outside the class directly using the object name and dot operator ‘.’ .The dot operator associates a member with the specific object of the class.

The syntax for accessing a public data member outside the class is

obj_name.member_name;

The syntax for calling a public member function is

object_name.function_name(parameter_list);

To understand the concept of accessing public members of a class, consider this example.

Example : A code segment to demonstrate the concept of accessing public members of a class

#include<iostream>
using namespace std;
class number {
 int x;
 public:
    int y;
    int z;
    void fn(int a);
} ;
  int main () {
     number p;
     p.y = 7;
     p.z = 2;
     p.x = 3;
     p.fn (10) ;
     return 0;
}

In this example, a class number having three data members x, y and z is defined. The data member x is private by default, whereas, y and z are declared as public. Hence, y and z can be accessed directly outside the class using the object name and the dot operator. However, x being a private data member cannot be accessed directly outside the class.

Accessing Private Members: The private members of a class are not accessible outside the class not even with the object name. However, they can be accessed indirectly through the public member functions of that class.

To understand the concept of accessing private members of a class, consider this example.

Example: A program to demonstrate the concept of accessing private members of a class

class book {
  //body of class as in Example1
} ;
int main () {
  book book1;
  book1.price = 350;
  book1.title="Exploring IT";
  book1.getdata ("Exploring IT", 350);
  return 0;
}

In this example, the object book1 of class book is used to access the public member function getdata (), which provides an indirect access to private data members title and price.

The basics of classes and objects can be summarized in a single program as shown in this example.

Example: A program to demonstrate the concept of classes and objects in C++

#include<iostream>
using namespace std;
class book {
  // definition of a class
  char title [30];
  float price;
  public:
    void getdata(char[] ,float);
    void putdata ();
} ;
  void book :: getdata (char a [],float b) {
    // definition of member function
   strcpy(title, a);
   price = b;
}
void book :: putdata(). // Definition of member function {
   cout<<"Title: "<<title<<", ";
   cout<<"Price:Rs"<<price;
}
int main () {
  book book1, book2, book3; // creating objects
  book1.getdata("Exploring IT" ,350);
  // reading data into book 1
  book2.getdata ("JAVA Tutorial", 300) ;
  //reading data into book 2
  book3.getdata("Computer Application",400);
  // reading data into book 3
  cout<<"\nTitle and Price of Book l\n";
  book1.putdata () ; // displaying data of book 1
  cout<<"\nTitle and Price of Book 2\n";
  book2.putdata (); // displaying data of book 2
  cout<<"\nTitle and Price of Book 3\n";
  book3.putdata();
  return 0;
}

The output of the program is 

Title and Price of Book 1

Title: Exploring IT, Price: Rs 350

Title and Price of Book 2

Title: JAVA Tutorial, Price: Rs 300

Title and Price of Book 3

Title: Computer Application, Price: Rs 400

You’ll also like:

  1. How to Accessing members of a class in C++
  2. Describe Private, Protected and Public – the differences and give examples
  3. Write A C++ Program To Declare Private Member Function And Access It Using Public Member Function.
  4. Arrays as Class Members in C++
  5. Write A C++ Program To Illustrate The Concept Of Public Keyword.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW