OOP Lab Manual - W04
OOP Lab Manual - W04
Instructions
Submission: Use proper naming convention for your submission file. Name the submission file
as LabNO_ROLLNUM (e.g. Lab01_00000). Submit the file on Google Classroom within the
deadline. Failure to submit according to the above format would result in deduction of 10%
marks. Submissions on the email will not be accepted.
Plagiarism: Plagiarism cases will be dealt with strictly. If found plagiarized, both the involved
parties will be awarded zero marks in the assignment, all of the remaining assignments, or even
an F grade in the course. Copying from the internet is the easiest way to get caught!
Deadline: The deadlines to submit the assignment are hard. Late submission with marks
deduction will be accepted according to the course policy shared by the instructor. Correct and
timely submission of the assignment is the responsibility of every student; hence no relaxation
will be given to anyone.
Comments: Comment your code properly. Bonus marks (maximum 10%) will be awarded to
well comment code. Write your nameObjectives
and roll number (as a block comment) at the beginning of
the solution to each problem.
this lab, you will learn:
Tip
o :About
For timely completion
Structure ADT. of the assignment, start as early as possible. Furthermore, work
o How- as
smartly to some
defineofa the problems
Structure, can beand
initialize solved
referusing smarter members
to individual logic. of a Structure.
Note: Follow the given instructions to the letter, failing to do so will result in a zero.
Objectives
In this lab, you will learn:
Constant Data Members, Constant Member Functions, and Constant Objects
Static Data Members and its initialization and accessing
“This” Pointer
Concepts
1. Constant:
1.1. Constant Data Members
Data members of a class may be declared as const. Such a data member must be initialized by the
constructor using an initialization list. Once initialized, a const data member may never be
modified, not even in the constructor or destructor. Data members that are both static
and const have their own rules for initialization. Furthermore, a const data member cannot be
initialized at the time of declaration or within the member function definition. To initialize
a const data member of a class, follow given syntax:
The following example illustrates the initialization and declaration of constant data members:
In this program, Number is a class and x is a constant integer data member, we are initializing it
with 12.
Output:
Output:
Output:
Output:
2.2. Lifetime
Even though static member variables are declared in a class, they are actually defined outside
the class declaration. The lifetime of a class’s static member variable is the lifetime of the
program. This means that a class’s static member variables come into existence before any
instances of the class are created.
3. “this” Pointer:
Every object has access to its own address through an important pointer called “this” pointer.
The “this” pointer is an implicit parameter to all member functions. Therefore, inside a member
function, this may be used to refer to the invoking object. To summarize, the “this” pointer
Air University Islamabad
FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L– Object Oriented Programming Lab Manual
holds the address of current object, in simple words you can say that this pointer points to the
current object of the class.
Let’s take an example to understand this concept. Here, you can see that we have two data
members num and ch. In member function setMyValues() we have two local variables having
same name as data members name. In such case if you want to assign the local variable value to
the data members then you won’t be able to do until unless you use this pointer, because the
compiler won’t know that you are referring to object’s data members unless you use
“this” pointer. This is one of the example where you must use this pointer.
Output:
Lab Tasks
1. Create a Car class, where attributes associated with each car are name, registration number,
manufacture name and year. All attributes should not be accessed directly. One can view the
details of all cars. Note car attributes cannot be changed by any means after initialization. (Hint
use constant objects)
2. Your team is creating a game ludo. You are working on player class. It contains the following
information.
o Current position of player (between 1 to 100)
o Player alive pawns (Gooti)
o Does the player have its turn now or not.
Player data can be initialized via constructor and via one setter function. Player data (attributes)
can be fetched using getter() function but with the conformation that via getting data player
data cant be changed or updated. (user constant functions)
3. In car class (Question 1) maintaining car count means your program could be able to tell the
count of car objects created. (User static counter variable)
4. You are creating a Patient Record App. Following information is required to store.
o name. A string that holds the patient’s name.
o idNumber. An int variable that holds the patient’s ID number.
o department. A string that holds the name of the department where the patient admitted.
o Disease. A string that holds the patient’s disease title.
o CovidStatus: Either the patient has been covid Positive or not.
Note:
• Code all problems in program loop.