Lab 6
Lab 6
Section 06
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);
}
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(){
~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
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.