0% found this document useful (0 votes)
31 views10 pages

Class Scope and Access in C++ Lab

lab 3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Class Scope and Access in C++ Lab

lab 3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMSATS UNIVERSITY OF ISLAMABAD, ISLAMABAD CAMPUS

Object Oriented Program

Lab # 03: Class Scope and Accessing Class Members

Name Maryam Khaliq

Registration Number FA22-BEE-115

BEE-3C
Class

Asma Bibi
Instructor’s Name
Lab Assessment
Post Lab Total

In-Lab Data Presentation Data Analysis Writing Style


➢ Objectives

The objective of this lab is to teach the students, the scope of class data members and its
member functions

• Class Scope

Class variables and class methods are associated with a class. An instance of the class (object) is not
required to use these variables or methods. Class methods cannot access instance variables or methods,
only class variables and methods.

• Instance Scope

Instance variables and instance methods are associated with a specific object. They can access class
variables and methods.

• Private Scope

Private variables and private methods are only accessible to the object they are contained in.

• Protected Scope

Protected variables and protected methods are accessible by the class they are in and inheriting classes
(sub classes) only.

• Public Scope

Public variables and public methods are accessible outside the object they are contained in. They are
accessible to any other object.

• Encapsulation

The process of providing a public interface to interact with the object while hiding information inside the
object is called encapsulation.

➢ 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.

▪ CODE:

#include<iostream>
using namespace std;
class add
{
private:
int iNum1, iNum2, iNum3;
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();
system("pause");
}
▪ OUTPUT:

➢ IN LAB TASKS:

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>
using namespace std;
class add {
private:
int iNum1, iNum2, iNum3;
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.iNum1;
A1.iNum2;
A1.iNum3;
A1.input(iX, iY);
A1.sum();
A1.disp();
return 0;
}
▪ OUTPUT:

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>
using namespace std;
class add {
private:
int iNum1, iNum2, iNum3;

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;
}
public :
void addition()
{cout<<"Enter two numbers:"<<endl;
cin>>iNum1>>iNum2;
input(iNum1,iNum2);
sum();
disp(); }
};
int main(){
add A1;
A1.addition();
return 0;
}
▪ OUTPUT:

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()
{
cout<<"This is a constructor"<<endl;
}
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;
A1.add();
return 0;
}

▪ OUTPUT:

➢ HOME TASK:

Create a class of subtraction having two private data members. Create class methods
to get data from users and for subtraction of data members. Use appropriate access
modifiers for class methods.

▪ CODE:

#include<iostream>
using namespace std;
class subtraction
{
private:
int num1,num2,num3;
public:
void input()
{
cout<<"Enter The first Number:";
cin>>num1;
cout<<"\nEnter The second Number:";
cin>>num2;}
void sub()
{
cout<<"\nFunctions to find the subtraction of two numbers"<<endl;
num3=num1-num2;
}
void disp()
{
cout<<"\nThe subtraction of the two numbers is "<<num3<<endl;
}
};
int main()
{
subtraction s1;
s1.input()
;s1.sub();
s1.disp();
return 0;
}

▪ OUTPUT:
➢ CONCLUSION:

I gained knowledge of a program's public and private scope in this lab. I observed that because
private data members are under private scope and cannot be accessed in main, if we attempt to
access them in main, an error would be displayed. I also learned how to define functions outside
of class. I also saw if we try to access private constructor in main it will also give an error
because there is an other method to call private constructor in main. I learned methods to access
members functions and data members. This lab helped me understand the concepts behind the
public and private scope of C++.

You might also like