0% found this document useful (0 votes)
4 views7 pages

Lab 6

The document covers Object Oriented Programming concepts for the Fall 2022 semester at UCP Lahore, focusing on static data members, the Singleton design pattern, and includes sample code for both. It provides lab tasks for dynamic object creation, tracking static attributes, and applying the Singleton pattern. The document contains code examples and outlines the requirements for each lab task.

Uploaded by

hirrah21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Lab 6

The document covers Object Oriented Programming concepts for the Fall 2022 semester at UCP Lahore, focusing on static data members, the Singleton design pattern, and includes sample code for both. It provides lab tasks for dynamic object creation, tracking static attributes, and applying the Singleton pattern. The document contains code examples and outlines the requirements for each lab task.

Uploaded by

hirrah21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Object Oriented Programming

Section 06

Semester: Fall 2022


Faculty of Information Technology
UCP Lahore, Pakistan
Table of Contents
Semester: Fall 2022.................................................................................................................... 1
static data members .................................................................................................................... 3
Sample problem ......................................................................................................................... 3
Sample Code .............................................................................................................................. 3
OUTPUT:.................................................................................................................................. 4

Singleton .................................................................................................................................... 5
Sample code ............................................................................................................................... 5
OUTOUT ................................................................................................................................... 6
Lab Task..................................................................................................................................... 7
Task 1: dynamic object .............................................................................................................. 7
Task 2: Static attribute: .............................................................................................................. 7
Task3: Singleton ........................................................................................................................ 7
static data members
- within class static data members
- when data sharing between objects is a requirement
- can't be initialized within class
- can only be initialized outside classs
- can be accessed with class reference

Sample problem
Create a dynamic array of student object and count number of students that create in main
function
// count number of students

Sample Code
#include<iostream>
#include<string>
using namespace std;
class Student{
string name;
int id;
static int count;
public:
Student();
Student(string,int);
void setName(string);
void setId(int);
string getName();
int getId();
void display();
int getcount();
};
Student::Student(){
cout << "default constructor" << endl;
count++;
}
Student::Student(string s,int i){
name = s;
id = i;
count++;

}
void Student::setName(string n){
name = n;

}
void Student::setId(int i){
id = i;
}
string Student::getName(){
return name;
}
int Student::getId(){
return id;

}
int Student::getcount(){
return count;
}
void Student::display(){
cout <<"name of the student: " <<name << endl;
cout <<"id of the strudent: "<< id << endl;
cout <<"total students: " <<count << endl;
}
int Student::count = 0;
int main(){
int d;
string a;
Student *s = new Student[3];
for (int i = 0; i < 3; i++){
cout << "enter name of student" << endl;
cin >> a;
cout << "enter id of student" << endl;
cin >> d;
s[i]=Student(a,d);
}

for (int i = 0; i < 3; i++){


s[i].display();
}
Student s2("ali", 111);
s2.display();
cout<<"total count of students are: "<<s2.getcount();

OUTPUT:
Singleton
- singleton pattern is a design pattern used to implement the mathematical concept of a singleton,
by restricting the instantiation of a class to one object.

-used when exactly one object is needed to coordinate actions across the system.

- The concept is sometimes generalized to systems that operate more efficiently when only one
object exists, or that restrict the instantiation to a certain number of objects.

Sample code
#include<iostream>
using namespace std;
class Biscuit{
private:
static int count;
static bool flag;
Biscuit() {
count++;
cout << count << " Biscuit created\n";
}
public:
static Biscuit * getBiscuit(){
if (flag == true){
flag = false;
return new Biscuit();
}
else{
cout << " you have already one first consume it\n";
return NULL;
}
}
void read(){

cout << "hello i can read you " << endl;


}
void eat(){ cout << " i m going to eat one" << endl; }

~Biscuit() {
cout << count << " Biscuit Consumed " << endl;
flag = true;
}
};
bool Biscuit::flag = true;
int Biscuit::count = 0;
int main(){
Biscuit *b1;
b1 = Biscuit::getBiscuit();
b1->eat();
Biscuit *b2;

b2 = Biscuit::getBiscuit();
b2->eat();
if (b2 != NULL)
b2->eat();
delete b1;//consume first biscuit
//try again
b2 = Biscuit::getBiscuit();
if (b2 != NULL)
b2->eat();

Biscuit *b3;

b3 = Biscuit::getBiscuit();
b3->eat();
if (b3 != NULL)
b3->eat();
delete b2;//consume first biscuit
//try again
b3 = Biscuit::getBiscuit();
if (b3 != NULL)
b3->eat();
Biscuit *b4;
b4 = Biscuit::getBiscuit();;
b4->eat();
system("pause");
return 0;
}

OUTOUT
Lab Task

Task 1: dynamic object


A class named Employee holds information of the employee comprising of Employee Code, Name,
Date of Birth and Date of Joining. Write a driver program to create at least three objects of
employee dynamically and enter some data into it (you may create more than 3 objects, however, at
least three of them must be dynamic). Display the names and count of those employees who are
older than 30 years.

Task 2: Static attribute:


Punjab government wants to track laptop objects that are distributed to students
Write a C++ program that create a class Laptop have a static attribute count, which counts
the number of laptops given to the students. whenever an object created it increase the
counter by 1 and whenever an array of any number created it counts that too.

Task3: Singleton
There is a system in an office which has some info associated with it. One system has single set of
information so there must be one instance of SystemInfo class. Apply Singleton design pattern to
this scenario so client could read same info of system from any global access point.

You might also like