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

Object Oriented Programming

Lab Manual (Lab 04)

Topic:
The Hidden This Pointer,
Member Initializer List,
Const Qualifier,
The Use of keyword Static and Array of Objects

Course Instructor: Miss Sundas Sagheer


Lab Instructor: Ahmad Abduhu

Session: Fall 2019


School of Systems and Technology
UMT Lahore Pakistan

1
The hidden This Pointer

Sample Code 01:

If you have a constructor (or member function) that has a parameter of the same name as a member variable, you can
disambiguate them by using “this”:
class Something
{
private:
int nData;
public:
Something(int nData)
{
this->nData = nData;
}
};
Sample Code 02:

class Calc
{
private:
int m_nValue;

public:
Calc() { m_nValue = 0; }

void Add(int nValue) { m_nValue += nValue; }


void Sub(int nValue) { m_nValue -= nValue; }
void Mult(int nValue) { m_nValue *= nValue; }
int GetValue() { return m_nValue; }

};

int main()
{
Calc cCalc;
cCalc.Add(5);
cCalc.Sub(3);
cCalc.Mult(4);

cout<<cCalc.GetValue()<<endl;
return 0;

2
Sample Code 03:

class Calc

{
private:
int m_nValue;
public:
Calc() { m_nValue = 0; }
Calc& Add(int nValue) const
{
m_nValue += nValue; return *this;
}
Calc& Sub(int nValue) { m_nValue -= nValue; return *this; }
Calc& Mult(int nValue) { m_nValue *= nValue; return *this; }
int GetValue() { return m_nValue; }

};

int main()
{
Calc cCalc;
cCalc.Add(5).Sub(3).Mult(4);
cout<<cCalc.GetValue()<<endl;
return 0;
}

Sample Code 04: Member Initialization List


class Something
{
private:
const int m_nValue;
public:
Something(): m_nValue(5)

}
};
}

3
Sample Code 05: Static Member Variable

class Something
{
private:
static int s_nIDGenerator=1; int
m_nID;
public:
Something() { m_nID = s_nIDGenerator++; }
int GetID() const { return m_nID; }

};
int Something::s_nIDGenerator = 1;

int main()
{
Something cFirst;
Something cSecond;
Something cThird;

cout << cFirst.GetID()<<endl;


cout << cSecond.GetID()<<endl;
cout << cThird.GetID()<<endl;
return 0;
}

Static Member Function


class IDGenerator
{
private:
static int s_nNextID;
public:
static int GetNextID()
{
return s_nNextID++;
}
};

int IDGenerator::s_nNextID = 1;

int main()
{
for (int i=0; i < 5; i++)
cout << "The next ID is: " << IDGenerator::GetNextID() << endl;
return 0;
}

4
Lab Tasks

Task 1:

Define a class employee. Take name, designation, age, DoB, address and years of experience as data members and getdata
and showdata as methods. Now enter the details of 5 employee using array of objects.

Task 2:

Write a program with a class that contains an array of integers. Initialize the integer array in the constructor of the class. Then
create two functions to the class to find the largest and smallest integers in the array.
Create a destructor that sets all of the elements in the array to 0.

Task 3:
A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title,
price, publisher, stock position and book number (Book number is auto generated number it starts from 101 and goes on). Whenever
a customer wants a book, the sales person inputs the title and the system searches the list and displays weather it available or not. If it
is not, an appropriate message is displayed. If it is, then the system displays the book details and request for the number of copies
required. If the requested copies are available, the total cost of requested copies is displayed; otherwise the message “Required copies
not in the stock” is displayed. Design a system using a class called books with suitable member functions and constructors. Provide
these extra facilities in the program:

1. The price of the books should be updated as and when required. Use a private member function to implement this.

2. The stock value of each book should be automatically updated as soon as a transaction is completed.

3. The number of successful and unsuccessful transactions should be recorded for the purpose of statistical analysis. Use
static data members to keep count of transactions.

You might also like