5.3 MEMBER FUNCTIONS

Just like data members class also has member functions. We need member functions to access private data members. Let us add two member functions3 (or methods) to the class Date which was declared earlier.

Problem: Write a program to demonstrate the use of public method in accessing private data members.

Solution: See Program 5.2.

Program 5.2 Introducing methods

// date1.cpp#include<iostream.h>//Class declaration begins hereclass Date{ private :    int day, month, year; // data members  public :    void setDate(int d1,int m1,int y1);// member function or method    void showDate(); //member function or method};//Class declaration ends here.//Method definitions are given after function mainint main(){ Date d1;  cout<< “<---date1.cpp--->”<<endl; ...

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.