5.2 DATA HIDING AND ENCAPSULATION

The keywords “private” and “public” are access specifiers for the members of a class. They specify whether the members can be accessed freely or not. Let us first see the effects of these on data members.

5.2.1 Private and public data members

Let us study the effect of access specifiers with the help of Program 5.1.

Problem: Write a program to demonstrate data hiding.

Solution: We will develop class date such that it will have two private members and one public member. We will see if they can be used (accessed) in the main program. See Program 5.1.

Program 5.1 Data hiding

// hiding.cpp#include<iostream.h>class Date{ private:    int day, month;    public:        int year;} ;int main(){ Date date1 ;  cout << ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.