Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Object Oriented Programming Lab (CS1022L)

Lab Manual (Lab # 02)

Session: Spring ’24


Instructor: Jaweria Aslam

Department of Computer Science


School of Systems and Technology UMT Lahore Pakistan
Objective: (Mapping CLO 01 + CLO 02)
 Access specifier
 How to create getters in c++
 How to create setters in c++
 Why we use Public and Private keywords in class
 Scope of variables
 Data Members and Member Functions
 Introduction to Constructors
Access Specifier:
One of the main features of object-oriented programming languages such as C++ is
data hiding.
Data hiding refers to restricting access to data members of a class. This is to
prevent other functions and classes from tampering with the class data.
However, it is also important to make some member functions and member data
accessible so that the hidden data can be manipulated indirectly.
The access modifiers of C++ allow us to determine which class members are
accessible to other classes and functions, and which are not.
For example,

Here, the variables patientNumber and diagnosis of the Patient class are hidden
using the private keyword, while the member functions are made accessible using
the public keyword.
Types of C++ Access Modifiers
 public
 private
 protected

Getter in C++:
Getter is a member function that helps to get the value of a private data member. It
allows access to private members to get read by external code but not allowing
them to modify it directly.
Setter in C++:
Setter is a member function that helps to set the value of a private data member. It
allows controlled access to private members and can provide a constraint on the
input or validation before a value is set.

Example:
Output:
Introduction to constructor:
C++ Constructors
A constructor is a special type of member function that is called automatically
when an object is created.
In C++, a constructor has the same name as that of the class, and it does not have a
return type. For example,

Here, the function Wall() is a constructor of the class Wall. Notice that the
constructor has the same name as the class, does not have a return type, and
is public
Types of constructors:
 C++ Default Constructor
 C++ Parameterized Constructor
 C++ Copy Constructor

C++ Default Constructor:


A constructor with no parameters is known as a default constructor. For example,
parameterized constructor:
In C++, a constructor with parameters is known as a parameterized constructor.
This is the preferred method to initialize member data. For example,
LAB TASKS

You might also like