0% found this document useful (0 votes)
39 views4 pages

Lab 03 Class Scope and Accessing Class Members: 1. Objectives

The document discusses class scope and accessing class members in C++. It defines private, protected, and public scopes and shows examples of creating a class with private member data accessed via public member functions. The lab tasks involve coding examples to demonstrate class scopes and access modifiers.

Uploaded by

bilaltanveer41
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)
39 views4 pages

Lab 03 Class Scope and Accessing Class Members: 1. Objectives

The document discusses class scope and accessing class members in C++. It defines private, protected, and public scopes and shows examples of creating a class with private member data accessed via public member functions. The lab tasks involve coding examples to demonstrate class scopes and access modifiers.

Uploaded by

bilaltanveer41
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
You are on page 1/ 4

Lab 03 Class Scope and Accessing Class

Members
February 27, 2024, 08:30 am to 11:30 am
Instructor: Dr. M. Umar Khan
Instructions:
1. Lab Reports must be submitted in both hard and soft forms before the deadline.
2. The cover page of the assignment must clearly state the Name and Registration of the
student along with the title of the Lab.
3. For online submission use MS Team group of section BCE 2B.
4. All lab reports are assessed as per rubric defined in the course description file (CDF).

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.

One of the techniques in object-oriented programming is encapsulation. It concerns the hiding


of data in a class and making them available only through its methods. In this way the chance of
making accidental mistakes in changing values is minimized. C++ allows you to control access to
classes, methods, and fields via so-called access modifiers. The access to classes, constructors,
methods and fields are regulated using access modifiers
i.e. a class can control what information or data can be accessible by other classes. To take
advantage of encapsulation, you should minimize access whenever possible.

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

Page 1 of 4
3.2. Instance Scope
Instance variables and instance methods are associated with a specific object. They can
access class variables and methods.

3.3. Private Scope


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

3.4. Protected Scope


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

3.5. Public Scope


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

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;

class add //Specifies the class

private:

int iNum1, iNum2, iNum3; //Member data

public:

void input(int iVar1, int iVar2) //Member function

cout<<"Functions to assign values to the member data"<<endl;


iNum1=iVar1;

Page 2 of 4
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;


}

};

/////////main function of the program///////////


void 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");

5. Lab Tasks
5.1. Code the example given above and check the errors if you try to access the private
data members in main() function.
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().

Page 3 of 4
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?.
6. Home Tasks
6.1. 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.
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/

Page 4 of 4

You might also like