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