1. Accessing Class Members that are Defined Inside the Class 2. Accessing Class Members that are Defined Outside the Class 3. Scope Resolution Operator
Member
Data and Member Functions
•
The data members and member function defined within a class belong to that
class's scope.
•
The non‒member functions belong to global
namespace
•
Within the class's scope, the data members of that class are immediately
accessible by the member functions of that class.
•
Outside the class the, only the public members of that class are accessible by
handle. Following C++ program illustrates this concept.

C++ Program
#include <iostream>
using namespace std;
class Test
{
//Public member functions of Class Test
private:
int a,b,c;
public:
int Addition(int a,
int b)
{
c= a+b;
return c;
}
void Display( )
{ cout <<
"The sum is:" << c << "\n";}
};
int main()
{
//Accessing them outside the class using object of that class
Test obj;
obj.Addition(10,20);
obj.Display();
return 0;
}
•
The class members are accessed using the handle called object with the help of dot operator. The ‒ > is used to access
the pointer variable.
•
There are two ways to access the class members.

Accessing class members:
1.
Membersinside class
2.
Members outside class
•
The function declared within the class
is usually called as member function
in C++ and it is called method in
object oriented programming.
•
The data declared along with some data
type is called as the data member of
that class.
•
The data
members denote the property of
the class and the member functions
denote the operations on that data.
•
Normally in C++ the data within the
class is declared as private and
member functions are declared as public.
This avoids any manipulation of data by the functions outside the class. As
these functions are declared as public
they can be accessible from outside the class.
•
Some times you may require the data to be private and member functions
to be public. Thus access mode helps the C++ programmer to
hide the data whenever required.
•
Following C++ program illustrates the concept of class, access specifiers,
function and data members.
/*******************************************
Program to demonstrate the class, access specifiers and data and
member functions
*******************************************/

#include <iostream>
using namespace std;
class Test
{
private:
int a,b,c;
public:
int Addition(int a,
int b)
{
c = a+b;
return c;
}
void Display( )
{ cout <<
"The sum is:" << c << "\n";}
};
int main()
{
Test obj;
obj.Addition(10,20);
obj.Display();
return 0;
}
Output
The sum is: 30
Example :1
Write a
C++ program that inputs two numbers and outputs the largest number using class.
Solution:
/***********************************************
Program to check the largest number
************************************************/
#include<iostream>
using namespace std;
class Test
{
int a,b;
public:
void Get_num()
{
cout <<
"Please two numbers: "<<endl;
cin >>a>>b;
}
void Check_largest()
{
if(a>b)
cout<<a<<"
is largest number"<<endl;
else
cout<<b<<"
is largest number"<<endl;
}
};
int main()
{
Test obj;
obj.Get_num();
obj.Check_largest();
return 0;
}
Example :2
Write a C++ program to check whether an
integer is a prime or a composite number.
Solution :
#include<iostream>
using namespace std;
class Test
{
int num;
public:
void Get_num()
{
cout <<
"Please enter a positive integer" << endl;
cin >> num;
}
void Check_prime()
{
int flag = 1;
for(int n = 2; n <= num ‒ 1; n++)
{
if(num % n = = 0)
{
flag=0;
}
}
if(flag= =1)
cout<<num<<"
is a prime number"<<endl;
else
cout<<num<<"
is a composite number"<<endl;
}
};
int main()
{
Test obj;
obj.Get_num();
obj.Check_prime();
retrun 0;
}
Output(Run 1)
Please enter a
positive integer
5
5 is a Prime number
Output(Run 2)
Please enter a
positive integer
10
10 is a composite
number
Example :
3
Consider
a Bank Account class with Acc No and balance as data members. Write a C++
program to implement the member functions get_Account_Details() and
display_Account_Details(). Also write suitable main function.
Solution:
/******************************************
Program to implement Bank Account class
*******************************************/
#include <iostream>
using namespace std;
class BankAccount
{
int AccNo;
double balance;
public:
void getAccDetails()
{
cout<<"\n
Enter the Account Number:
cin>> AccNo;
cout<<"\n
Enter the Balance Amount:
cin>>balance;
}
void DisplayAccDetails()
{
cout<<"\nAccount
Number: "<<AccNo;
cout<<"\nBalance
Amount: "<<balance;
cout<<endl;
}
};
int main()
{
BankAccount obj;
obj.getAccDetails();
cout<<"\n
The account details are ..."<<endl;
obj.DisplayAccDetails();
return 0;
}
Output
Enter the Account
Number: 101
Enter the Balance Amount:
10000
The account details
are...
Account Number: 101
Balance Amount: 10000
•
The members functions that needs to be
defined outside the class are
defined using the scope resolution
operator. Following program illustrates it.
/***************************************************************************
Program that uses a class where the member functions are defined
outside a class
*********************************************************************/
#include <iostream>
using namespace std;
class Test
{
private:
int a,b,c;
public:
int
Addition(int,int);
void Display();
};
int Test::Addition(int a, int b)
// Definition of function
{
// Outside the
class
c= a+b;
retum c;
}
void Test::Display()
{ cout << "The sum is:" <c<
"\n";}
int main()
{
Test obj;
obj.Addition(10,20);
obj.Display();
return 0;
}
Output
The sum is:30
•
The :: operator is used to define the
scope of the variable or function.
•
One can declare the function inside the
class and can write its definition outside the class. But while doing so the
programmer has to specify the belonging class name, scope operator and name of
the function.
•
Let us discuss a C++ program which is
making use of scope resolution operator.
C++
Program
#include<iostream>
using namespace std;
int a=5;
int main()
{
int a=15;
{
int a=20;
cout<<"INNER BLOCK\n";
cout<<"Value of a= "<<a<<endl;
cout<<"The scope of a= "<<::a<<endl;
{
int a=25;
cout<<"INNER‒INNER BLOCK\n";
cout<<"Value of a = "<<a<<endl;
cout<<"The scope of a= "<<::a<<endl;
}
}
cout<<"OUTER BLOCK\n";
cout<<"Value of a = "<<a<<endl;
cout<<"The scope of a= "<<::a<<endl;
retum 0;
}
Output
INNER BLOCK
Value of a= 20
The scope of a= 5
INNER‒INNER BLOCK
Value of a= 25
The scope of a= 5
OUTER BLOCK
Value of a = 15
The scope of a= 5
Program Explanation
•
In above program we have redeclared a
variable a whose value is modified on entering each new block. Note that a
block can be defined using { and }.
•
The scope resolution operator is used to
denote the scope of the variable. The scope of the variable is always a global scope. Hence each time we get
output as 5 for :: a but the value of variable a gets changed each time in the
block and that corresponding value can be displayed as an output.
Review Question
1. Explain in detail
about class, objects, methods and messages.
Object Oriented Programming: Chapter 2: Classes and Objects : Tag: Oops, Computer Programming : C++ Programming - Member Data and Member Functions
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation