Lab Report 3
Lab Report 3
CSC241
Object Oriented Programming
Lab # 03
Class BEE-3A
Lab 03
Class Scope and Accessing Class Members
1. Objectives
The objective of this lab is to teach the students, the scope of class data members and its member
functions.
2. Outcome
At the end of this lab student will be familiar with the accessing rules of class data members and
member functions
3. Introduction
In object-oriented programming, methods and variables have various scope. Scope means that
the method or variable may or may not be directly accessible to other objects or classes. Classes
that do not have instances may be accessible to the system.
3.6. Encapsulation
The process of providing a public interface to interact with the object while hiding other
information inside the object is called encapsulation.
4. Examples
The following program illustrates the usage of objects and classes in C++:
// The program uses public member functions to input two private numbers, add two private
numbers and display the result
#include<iostream>
using namespace std;
public:
void input(int iVar1, int iVar2) //Member function
{
cout<<"Functions to assign values to the member data"<<endl;
iNum1 = iVar1;
iNum2 = iVar2;
}
void sum (void) //Member function
{
cout<<"Functions to find the sum of two numbers"<<endl;
iNum3 = iNum1+iNum2;
}
void disp(void) //Member function
{
cout<<"The sum of the two numbers is "<<iNum3<<endl;
}
};
int main()
{
add A1;
int iX, iY;
cout<<"Input two numbers"<<endl;
cin>>iX;
cin>>iY;
A1.input(iX, iY);
A1.sum();
A1.disp();
system("pause");
return 0 ;
}
Output:
InLab Tasks
5.1. Code the example given above and check the errors if you try to access the private data
members in main() function.
Code:
#include <iostream>
class add
private:
int inum1,inum2,inum3;
inum1=var1;
inum2=var2;
} void sum(void)
inum3=inum1+inum2;
void disp(void)
cout<<inum3;
};
int main()
add A1;
int iX,iY;
cin>>iX;
cin>>iY;
A1.input(iX,iY);
A1.disp();
return 0;
ERROR CHECK:
5.2. Modify the above task by making the scope of public member functions as private. Create
access functions in public scope to access private member functions from main().
Code:
#include <iostream>
class add
private:
int inum1,inum2,inum3;
inum1=var1;
inum2=var2;
void sum(void)
inum3=inum1+inum2;
void disp(void)
cout<<inum3;
public:
input(x,y);
void show()
sum();
disp();
};
int main()
add A1;
int iX,iY;
cin>>iX;
cin>>iY;
A1.in(iX,iY);
A1.show();
return 0;
Output:
5.3. Code the example given above and include a private constructor in the class. Create objects
of this class. Test the code and write down how the constructor will be called or unable to be
called?.
Code:
#include <iostream>
using namespace std;
class add
{
private:
int iNum1, iNum2, iNum3;
add(int a=0, int b=0)
{
iNum1=a;
iNum2=b;
}
public: void input(int iVar1, int iVar2)
{
cout<<"Functions to assign values to the member data"<<endl;
iNum1=iVar1; iNum2=iVar2;
}
void sum(void)
{
cout<<"Functions to find the sum of two numbers"<<endl;
iNum3=iNum1+iNum2;
}
void disp(void)
{
cout<<"The sum of the two numbers is "<<iNum3<<endl;
} };
int main()
{
add A1;
int iX, iY;
cout<<"Input two numbers"<<endl;
cin>>iX;
cin>>iY;
A1.input(iX, iY);
A1.sum();
A1.disp();
return 0;
}
ERROR CHECK:
Code:
#include <iostream>
class subtraction
private:
int inum1,inum2,inum3;
inum1=var1;
inum2=var2;
void sum(void)
inum3=inum1-inum2;
void disp(void)
cout<<inum3;
public:
input(x,y);
void show()
sum();
disp();
}};
int main()
subtraction s1;
int iX,iY;
cin>>iX;
cin>>iY;
s1.in(iX,iY);
s1.show();
return 0; }
Output:
7. References
[i] http://www.tutorialspoint.com/cplusplus/cpp_class_access_modifiers.htm
[ii] http://www.learncpp.com/cpp-tutorial/83-public-vs-private-access-specifiers/
In this lab we learnt about the scope of class data members and its member
functions. And now we are well familiar with the accessing rules of class data
members and member functions.
Lab Assessment
Pre-Lab /1
In-Lab /5
/4 /10
Data Analysis
Data
Post-Lab /4 /4
Presentation
Writing Style /4