by Dinesh Thakur Category: Classes in C++

Member of a class can be accessed only through the object of a class. The members are accessed as follows:

 

p.member;

pl.member;

p2.member;

 

The member may be a member function or member data. The dot operator is also called the class member access operator. This is used for associating the object name with the member function.

Following are some examples to show the use of public and private accesses.

 

Example

 

An example to show that member data can be declared in the class whereas the values

can be provided outside the class.

#include <iostream.h>

#include<conio.h>

class base

{

public: int sl, s2;

};

void main()

{

clrscr();

base p;

p.s1 = 5;

p.s2 = 6;

cout<<p.s1<<" "<<p.s2;

}

 

In the above example, sl and s2 have been declared to be public.



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article