Lab 02
Lab 02
Laboratory 02
Version: 1.0.0
Contents:
● Learning Objectives
● Required Resources
● General Instructions
● Background and Overview
o Class & Objects
o Comparison between Class and Object
o Encapsulation
o Abstraction
● Activities
o Pre-Lab Activity
▪ Introduction to Getter/Setter Member Functions
▪ Example code on Getter and Setter Member Functions
▪ Explanation of Public-private access specifiers
▪ Demonstration of Encapsulation and Abstraction using Getter/Setter functions
▪ Application of Access Specifiers in a sample program
o In-Lab Activity
▪ Constructor
▪ Default Constructor
▪ Example Code on Default Constructor
▪ Parameterized Constructor
▪ Example Code on Parameterized Constructor
▪ Copy Constructor
▪ Overloaded Constructor
▪ Example Code on Overloaded Constructor
▪ Destructor
▪ Example code on Destructor
▪ Task 1: Class Definition and Implementation
▪ Task 2: Constructor Implementation
▪ Task 3: Constructor Implementation
▪ Task 4: Constructor Implementation
o Post-Lab Activity
▪ Inline Functions
▪ Inline Functions with Classes
▪ Function Overloading
▪ Task 01: Inline functions
▪ Task 02: Function Overloading
● Submissions
● Evaluations Metric
● References and Additional Material
● Lab Time and Activity Simulation Log
Learning Objectives:
● Understanding Encapsulation and Abstraction
● Getter/Setter Member Functions
● Public private access specifiers
● Constructors
● Overloaded Constructor
● Default Constructor
● Destructor
● Inline Functions
● Function Overloading
Resources Required:
• Desktop Computer or Laptop
• Microsoft ® Visual Studio 2022
General Instructions:
• In this Lab, you are NOT allowed to discuss your solution with your colleagues, even not
allowed to ask how is s/he doing, this may result in negative marking. You can ONLY discuss
with your Teaching Assistants (TAs) or Lab Instructor.
• Your TAs will be available in the Lab for your help. Alternatively, you can send your queries
via email to one of the followings.
Teachers:
Course Instructor Prof. Dr. Syed Waqar ul Qounain [email protected]
Lab Instructor Azka Saddiqa [email protected]
Activities:
Pre-Lab Activities:
Overview of the Getter/Setter Member Functions:
Getter/Setter member functions are used to control access to class data members in C++. Getters are
used to retrieve data from the class, and setters are used to assign values to the class. They allow for the
encapsulation of data within a class, which makes it easier to manage class data and ensures that data
is used correctly.
Example:
For example, if a class has a data member called "age", it is best to create a getter and setter function
for that data member. The getter function would return the age of the class object, and the setter function
would allow us to set the age of the class object. This way, we can ensure that the data member is being
properly used and make sure it is not being passed invalid data.
Syntax of Getters and Setters in C++:
Getter:
The syntax of Getter method is as follows
Type ClassName::getPropertyName(){
return propertyName;
}
Setter:
The syntax of Setter method is as follows
void ClassName::setPropertyName(Type propertyName){
this->propertyName = propertyName;
}
Example Code:
Output:
Task 02: Creating A Program with Classes and Objects [Estimated 20 minutes / 15 marks]
Write a C++ program that uses classes and objects to calculate the area of a rectangle, circle, or square
based on the user's choice and the dimensions they provide. The concept of encapsulation, abstraction
should be represented clearly in your code (implementation of getters/setters). Your code should follow
all the coding conventions.
Expected Output:
In-Lab Activities:
Constructors:
Constructors in C++ are special class functions that are used to initialize objects. They are
automatically called when an object is created. There are three types of constructors in C++:
● Default Constructor:
The default constructor is a constructor that takes no parameters, or default values may be provided. It
is automatically called when an object is created.
Example Code:
● Parameterized Constructor:
The parameterized constructor is a constructor that takes parameters and initializes the object with the
given values.
Example Code:
● Copy Constructor:
The copy constructor is a constructor that creates a new object by copying the values of an existing
object. It is used to create a new object based on an existing object. The details are out of scope for
this laboratory manual.
Overloaded Constructors:
Overloaded constructors in C++ provide developers with the ability to create multiple constructors
with different parameters and different implementations. This makes it easier to create objects with
different initial states.
Example:
For example, consider a Car class with the following two constructors:
Car()
Car(int numberOfDoors)
The first constructor is the default constructor which initializes the car with the default values. The
second constructor takes a parameter and initializes the car with the provided number of doors. This
allows a developer to create a car with the default values or one with the number of doors specified as
a parameter.
Another use of overloaded constructors is to create objects with different numbers of parameters. This
allows for more flexibility when creating objects. For example, a Car class could have the following
three constructors:
Car()
Car(int numberOfDoors)
Car(int numberOfDoors, string color)
With these three constructors, developers can create cars with the default values, with a specified
number of doors, or with a specified number of doors and color.
Overall, overloaded constructors in C++ provide developers with the ability to create multiple
constructors with different parameters and different implementations. This makes it easier to create
objects with different initial states, and can be a powerful tool when creating classes.
Example Code:
Example Code:
Fig. 14 (Destructor)
Output:
• Data Members:
o A string named as company.
o A string named as fuelType.
o An integer named as yearOfManufacture.
o A string named as yearOfPurchase.
o A string named as color.
o An integer named as engineCapacity.
• Your class should provide the implementation of a default constructor that first prints a line
“Default Constructor of Vehicle is called” and then initializes all the data members with the
default values (Default value for integer and string variables are 0 and “”).
• Provide the implementation of appropriate properties method (Getter/Accessors and
Setters/Mutators).
• Provide the implementation of following data functions:
o displayDetails(): displays all the information of the particular vehicle on console.
o inputDetails(): takes the input of all the details of vehicle from the user.
o isOld(): displays that how Old the vehicle is. If the manufacturing and purchasing year
is the same, then display “vehicle is purchased in the same year as it is manufactures”,
otherwise displays the message “vehicle is years old”.
• Provide the implementation of appropriate destructor that displays the message “Destructor
Called”
Task 02: Class with Constructor Implementation [Estimated 20 minutes / 10 marks]
Write a class named Employee for which each object can hold information about a particular employee:
• Make sure to check the functionality of bookBus() and cancelBooking() after creating three
different objects.
Output:
Post-Lab Activities:
Inline Functions:
Implementing a program as a set of functions is good from a software engineering stand point, but
function calls involve execution-time overhead. C++ provides inline functions to help reduce function-
call overhead. Placing the qualifier inline before a function’s return type in the function definition
advises the compiler to generate a copy of the function’s body code in every place where the function
is called (when appropriate) to avoid a function call. This often makes the program larger. The compiler
can ignore the inline qualifier and generally does so for all but the smallest functions. Reusable inline
functions are typically placed in headers, so that their definitions can be included in each source file
that uses them.
Syntax to create an inline function:
inline return-type function-name(parameters)
{ // function code }
Advantages of using Inline functions:
• Function call overhead doesn’t occur.
• It also saves the overhead of push/pop variables on the stack when function is called.
• It also saves overhead of a return call from a function.
• When you inline a function, you may enable compiler to perform context specific optimization
on the body of function. Such optimizations are not possible for normal function calls. Other
optimizations can be obtained by considering the flows of calling context and the called context.
• Inline function may be useful (if it is small) for embedded systems because inline can yield less
code than the function call preamble and return.
Example of Inline Function:
It is also possible to define the inline function inside the class. In fact, all the functions defined inside
the class are implicitly inline. Thus, all the restrictions of inline functions are also applied here. If you
need to explicitly declare inline function in the class then just declare the function inside the class and
define it outside the class using inline keyword.
Example
Example of (1)
Output:
Submissions:
● For In-Lab Activity:
▪ Save the files on your PC.
▪ TA’s will evaluate the tasks.
● For Pre-Lab & Post-Lab Activity:
▪ Submit the .cpp file on Google Classroom and name it to your roll no.
Evaluations Metric:
● All the lab tasks will be evaluated by TA’s
● Division of Pre-Lab marks: [25 marks]
▪ Task 01: C++ Strings Problem solving [10 marks]
▪ Task 02: Class and Object Basics [15 marks]
● Division of In-Lab marks: [55 marks]
▪ Task 01: Class Definition and Implementation [10 marks]
▪ Task 02: Constructor Implementation [10 marks]
▪ Task 03: Constructor Implementation [15 marks]
▪ Task 04: Constructor Implementation [30 marks]
● Division of Post-Lab marks: [20 marks]
▪ Task 01: [10 marks]
▪ Task 02: [10 marks]
● Constructors
https://www.programiz.com/cpp-programming/constructors