0% found this document useful (0 votes)
72 views

Object Oriented Programming Lab-07 (Static, This, Array of Objects)

This document discusses object oriented programming concepts like the hidden this pointer, member initializer lists, the use of the static keyword, and arrays of objects. It provides 6 code samples demonstrating these concepts and 3 tasks to define classes to model employees, books in a bookshop inventory, and arrays of integers.

Uploaded by

Ahmad Abduhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Object Oriented Programming Lab-07 (Static, This, Array of Objects)

This document discusses object oriented programming concepts like the hidden this pointer, member initializer lists, the use of the static keyword, and arrays of objects. It provides 6 code samples demonstrating these concepts and 3 tasks to define classes to model employees, books in a bookshop inventory, and arrays of integers.

Uploaded by

Ahmad Abduhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Oriented Programming

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

Lab Instructor: Ahmad Abduhu

Session: Spring 2020


School of Systems and Technology
UMT Lahore Pakistan

The hidden This Pointer

1
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 Data;
public:
Something(int Data)
{
this->Data = Data;
}
};
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(15); cCalc.Sub(3); cCalc.Mult(4); cCalc.Add(15);

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

Sample Code 03:

#include <iostream>

using namespace std;

class Calc
{
private:
int m_nValue;
public:
Calc() { m_nValue = 0; }
Calc& Add(int nValue)
{
m_nValue += nValue;
return *this;
}

2
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(15).Sub(3).Mult(4).Add(15);
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 IDGenerator; int ID;
public:
Something() { ID = IDGenerator++; }
int GetID() const { return ID; }

};
int Something::IDGenerator = 1;

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

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


cout << cSecond.GetID()<<endl;
cout << cThird.GetID()<<endl;
return 0;
}
Sample Code 06: Static Member Function
#include <iostream>
using namespace std;

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

int IDGenerator::NextID = 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