The private data of a class can be accessed only through the member functions of that class. The main() function cannot contains statements that the access class members directly.
- Private members of the class can only accessed by the members with in that class.
- Public members of the class can be accessed outside the class also.
- To access class member outside class, it can be done by using dot operator and object of that class.
Syntax:
object-name.function-name(actual-arguments);
x.getdata(100,75.5);
Example:
#include<iostream>
using namespace std;
class employee // class
{
char name[10]; // data member
int id; // data member
public:
void getdata(); // prototype declaration
void putdata(); // prototype declaration
};
void employee::getdata() // member function
{
cout<<”Enter name and id of employee: ”;
cin>>name>>id;
}
void employee::putdata() // member function
{
cout<<”Display name and id of employee: ”;
cout<<name<<id;
}
int main()
{
employee x;
x.getdata();
x.putdata();
}
You must log in to post a comment.