LAB NO 4 OOPS
LAB NO 4 OOPS
5
18/10/2024
Lab outcomes:
After completing this lab, students will be able to;
Understand concept of Encapsulation
Basic Implementation of Getters and Setters.
Theory:
Encapsulation in C++
1. What is Encapsulation?
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
Use Classes: Define a class that bundles the related data and
methods together.
Access Specifiers: Use C++ access specifiers (public, private,
protected) to control the visibility of the class members (data members
and methods).
Getters and Setters: Implement methods (also known as getter and
setter methods) to provide controlled access to private members.
Access Scope/Visibility
Specifier Usage
public Members are Used for methods and attributes that
accessible from should be accessed directly by other
outside the class. parts of the program.
private Members are Ensures that data cannot be accessed
accessible only within directly from outside the class,
the class itself. maintaining data integrity.
protected Members are Typically used in inheritance scenarios
accessible within the where derived classes need controlled
class and its derived access to base class members.
classes.
Getters:
o These are methods that return the value of a private/protected
attribute.
o They provide read-only access, meaning that users of the class
can view the value without modifying it directly.
o Syntax: DataType getAttributeName();
Setters:
o These are methods that set or update the value of a
private/protected attribute.
o They provide write or update access with validation logic if
needed to ensure the value being set is appropriate.
o Syntax: void setAttributeName(DataType value);
5. Why Use Getters and Setters?
Data Encapsulation: By using getters and setters, the internal
representation of data is hidden from external access. This ensures
that the data cannot be modified directly, maintaining its integrity.
Data Validation: Setters allow us to add validation logic before
modifying the value of an attribute. This prevents invalid data from
being set.
Control Access: Getters and setters allow fine-grained control over
how and when the data can be accessed or modified.
6. Code Example: Getters and Setters
#include <iostream>
using namespace std;
class Car {
private:
int year;
public:
return 0;
}
Tasks:
Lab Task 1: write a program that has a class consisting of two
private members and two public member, that can explain
encapsulation using getter and setter function in OOP.
Code:
Output:
Observations:
In this lab we have done some tasks in which we used private and public
access specifiers and placed some variables in it and call those values in int
main and for private values to call we used getters and setters and with the
help of them we call all the values.
Rubrics
Report not Content that The The Appropri Conclusion
submitted has been requirement observati ate drawn
plagiarized s are ons, as measure correctly
or that has described,
well as ments or with exact
Laborato been as well as
submitted the the numeric results and
ry
incompletel experiment complete al a complete
Reports
y al process. technique analysis report in
, are are every way
document carried
ed. out.
Category Ungrade Very Poor Poor Fair Good Excellent
d
Marks 0 1 2 3 4 5
Demonst Absent The The student The The student The student
ration student understands student has created successfully
is unable the followed a functional developed a
to presented instructio or working functional
adequat laboratory ns to build schematic, model,
ely instructions the core model, logic, circuit,
follow and is schematic block block
the familiar with , block diagram, or diagram, or
instructi the lab diagram, code and code and
ons environment code, or has completed
provided but Cannot model. successfull the lab
. The setup With y run the objective in
student simulation some program or real-time or
can according to assistance circuit on a in a
name design but , the software simulation
the knows how student platform. environment
hardwar to perform can set up With , achieving
e or simulation and run minimum the expected
simulatio the assistance, results. They
n simulation they can can set up,
platform, . On the set up and operate, and
but protoboar run the run the
Cannot d/trainer/s simulation. simulation
setup or imulation on their own.
perform software
the
simulatio
n
Category Ungrad Very Poor Fair Good Excellent
ed Poor
Marks 0 1 2 3 4 5