You are on page 1of 4

DEPARTMENT OF COMPUTER &

SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI

Subject Name

Object Oriented Programming

Lab Number # 7

SUBMITTED TO:
LE Sundas Ashraf

SUBMITTED BY:
Student Name
Ummaima Nadeem
Reg # 393460
DE- 43 Dept electrical engineering

Submission Date: 8 Nov , 2022


Objectives:
The main objective of this lab is to get an idea about static and constant variables.
Related Topic/Chapter in theory class:
Classes

Hardware/Software required:
Hardware: PC
Software Tool: Visual Studio

Tasks 1

Solution:

#include <iostream>
using namespace std;

class tollBooth
{
private:
unsigned int cars;
double money;
public:
tollBooth()
{
cars = 0;
money = 0;
}
tollBooth(unsigned int cars, double money) : cars(cars), money(money) {}

void payingCar()
{
cars++;
money = money + 0.50;
}
void nopayCar()
{
cars++;
}
void const display()
{
cout << "Total cars passed through tollbooth: " << cars << endl;
cout << "Total cash collected: " << money << endl;
}
};

int main() {
tollBooth o1;
int c = 0;
int m = 0;
while (m == 0)
{
cout << "Press 1 to count a paying car\n";
cout << "Press 2 to count a non-paying car\n";
cout << "Press 3 to display cash collected and total cars that passed\n";
cin >> c;
if (c == 3)
{
break;
}
switch (c)
{
case 1:
o1.payingCar();
break;
case 2:
o1.nopayCar();
break;
default:
cout << "Invalid Input!" << endl;
break;
}
cout << endl;
}
m = 1;
cout << endl;
o1.display();

return 0;
}

Output:

Task 2
Solution:
#include<iostream>
using namespace std;

class C
{
private:
static int objectCount;
int serialNumber;
public:
C() : serialNumber(0) {}
C(int serialNumber) : serialNumber(serialNumber)
{
objectCount++;
cout << "I am object number " << objectCount << endl;
cout << "My serial number is " << serialNumber << endl;
cout << endl;
}
static void display()
{
cout << "Objects Created: " << objectCount << endl;
}
};

int C::objectCount;

int main()
{
C o1(5674), o2(8765), o3(1456), o4(9876);
C::display();

return 0;
}

Output:

Conclusion:
In this lab, we learned the concept of constant and static variables in classes.

You might also like