Lab 8
Lab 8
Task 1: deep copy, setter, getter, default constructor, parameterized constructor, copy
constructor and constant function
Write a class ‘Student’ that represents the real-life model of a student. It shall be able to store the
following attributes
•char * Name
•char* Program
• int Semester
• char* Section
• float GPA
Use default constructor to create an object of this class with some standard attribute values, for
example,
• Name = NIL
• Program = “BSSE”
• Semester = 3
• Section = 01
• GPA = 3.00
Create getter setter for each attribute create default, parameterized and copy constructor
Create display function to display all the information i.e.
Name
Program
Semester
Section
GPA
also make display function constant
When a new student arrives, your program shall create a new student object. Set the values for
name program semester section and GPA the program shall be able to destroy their objects using the
destructor.
Solution:
#include <iostream>
class Student {
private:
// attributes...........................
char * Name;
char* Program;
int Semester;
char* Section;
float GPA;
public:
// default constructor............................
Student(){
Name = NULL;
Program ="BSSE";
Semester = 3;
Section = "01";
GPA = 3.00;
}
// parameterized constructor
///deepcopy name....................
int len=0;
for(int i=0;n[i]!='\0';i++){
len++;
Name[j]=n[j];
Name[len]='\0';
//deepcopy(Program)...................
len=0;
for(int i=0;p[i]!='\0';i++){
len++;
Program[j]=p[j];
Program[len]='\0';
//deepcopy(Section)..................
len=0;
for(int i=0;sec[i]!='\0';i++){
len++;
Section[j]=sec[j];
}
Section[len]='\0';
Semester=s;
GPA=g;
Student(Student &obj){
int len=0;
for(int i=0;obj.Name[i]!='\0';i++){
len++;
Name[j]=obj.Name[j];
Name[len]='\0';
len=0;
for(int i=0;obj.Program[i]!='\0';i++){
len++;
Program[j]=obj.Program[j];
}
Program[len]='\0';
len=0;
for(int i=0;obj.Section[i]!='\0';i++){
len++;
Section[j]=obj.Section[j];
Section[len]='\0';
Semester=obj.Semester;
GPA=obj.GPA;
//setName deepcopy.........................
void setName(char* n)
int len=0;
for(int i=0;n[i]!='\0';i++){
len++;
}
Name[j]=n[j];
Name[len]='\0';
//getname deepcopy...................................
char* getName()
int len=0;
for(int i=0;Name[i]!='\0';i++){
len++;
Name[j]=Name[j];
Name[len]='\0';
return Name;
int len=0;
for(int i=0;p[i]!='\0';i++){
len++;
Program[j]=p[j];
}
Program[len]='\0';
char* getProgram()
int len=0;
for(int i=0;Program[i]!='\0';i++){
len++;
Program[j]=Program[j];
Program[len]='\0';
return Program;
int len=0;
for(int i=0;sec[i]!='\0';i++){
len++;
Section[j]=sec[j];
Section[len]='\0';
}
char* getSection() {
int len=0;
for(int i=0;Section[i]!='\0';i++){
len++;
Section[j]=Section[j];
Section[len]='\0';
Semester=s;
int getSemester()
return Semester;
GPA=g;
float getGPA()
return GPA;
{
cout<<".................................."<<endl;
cout<<".................................."<<endl;
cout<<".................................."<<endl;
cout<<".................................."<<endl;
cout<<".................................."<<endl;
cout<<".................................."<<endl;
~Student(){
delete[] Name;
Name= NULL;
delete[] Section;
Section= NULL;
delete[] Program;
Program= NULL;
};
int main()
Student s1;
s1.setName("ali");
s1.setProgram("BSCS");
s1.setSemester(5);
s1.setGPA(3.78);
s1.setSection("o9");
s1.display();
Student s2("Amna","BSSE",07,"o1",3.89);
s2.display();
return 0;
You have a class circle having instance variable radius (in float), default constructor, a
constructor having one parameter and behaviors (Area and circumference) with return type as
float.
Circle c1(5);
Circle c2 = c1;
c2.setRad(8);
cout<<"After Setting C2"<<endl;
cout<<"c1.area() = "<<c1.area()<<endl;
cout<<"c1.circumference()= "<<c1.circumference()<<endl;
cout<<"c2.area() = "<<c2.area()<<endl;
cout<<"c2.circumference()= "<<c2.circumference()<<endl;
SOLUTION:
#include <iostream>
class circle{
private:
float radius;
const float pie;
public:
circle():pie(0){
cout<<"default constructor"<<endl;
}
circle(float r):pie(22.0/7.0)
{
radius=r;
}
circle(const circle &obj):pie(22.0/7.0)
{
radius=obj.radius;
}
float area(){
return pie*radius*radius;
}
float circumfrence(){
return 2*pie*radius;
}
void setRad(float r){
radius=r;
}
float getRad(){
return radius;
}
};
int main()
{
circle c1(5);
circle c2 = c1;
c2.setRad(8);
cout<<"After Setting C2"<<endl;
cout<<"c1.area() = "<<c1.area()<<endl;
cout<<"c1.circumference()= "<<c1.circumfrence()<<endl;
cout<<"c2.area() = "<<c2.area()<<endl;
cout<<"c2.circumference()= "<<c2.circumfrence()<<endl;
return 0;
}
In main function create a object of employee w.r.t parameterized constructor and create
another object w.r.t setter function
Employee Ali(21);
Ali.display()
Employe Amna;
Amna.setid(11);
Amna.disply()
Employe *ptr=new Employee;
Ptr->setid(90);
Ptr->display();
Solution:
#include <iostream>
using namespace std;
class Employee
{
int id;
static int count;
public:
Employee(){
id=0;
count++;
}
Employee(int d){
id=d;
count++;
}
void setid(int d)
{
id=d;
}
int getid(){
return id;
}
void display()
{
cout << "The id of this employee is " << id << " and this is employee number " << count
<< endl;
}
int main()
{
Employee Ali, rohan, Amna;
Ali.setid(12);
rohan.setid(11);
Amna.setid(02);
rohan.display();
Employee abc(21);
abc.display();
Employee *ptr=new Employee;
ptr->setid(90);
ptr->display();
return 0;
}
TASK 4: Static function
Charity Fund
Student wants to contribute in charity fund. charity Fund is a shared fund between
students
Write a c++ program that create a class name student it has private data member id,
name and static charity fund. create its constructor default and parameterize, create a
getter for static charity fund attribute, Now creates a static void function for calculate
the contributions of the students
SOLUTION:
#include <iostream>
using namespace std;
class Student {
private:
int id;//exclusive
static int fund;//shared (allocated once)
public :
Student() { }
Student (int id ) {this->id=id;}
static int getfund() { return fund;}
static void contribute(int amt) { fund=fund+amt;}
};
int Student::fund=0;
int main () {
cout <<Student::getfund();
Student::contribute(10000);//univesity contributes 10000
Student goher(1),fraz(2),baig(3),ayesha(4);
goher.contribute(100);
fraz.contribute(100);
baig.contribute(100);
ayesha.contribute(0);
cout<<"Goher wants to check Pfund ="<<goher.getfund()<<endl;
cout<<"fraz wants to check Pfund ="<<fraz.getfund()<<endl;
baig.contribute(-200);
cout<<"ayesha wants to check Pfund ="<<ayesha.getfund()<<endl;
cout<<"university wants to check Pfund ="<<Student::getfund()<<endl;
return 0;
}
TASK 5: 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.
SOLUTION:
#include <iostream>
using namespace std;
class System{
private:
static System *obj;
System() {}
public:
static System *getInstance()
{
if(obj == nullptr)
{ obj = new System();
cout<<"system admin created"<<endl;
}
else
cout<<"system admin already created you cant create new one : you only use read
functions "<<endl;
return obj;
}
void prn(){
cout<<"System information : everybody can excess this !"<< endl;
}
~System() {
obj=NULL;
}
};
System *System::obj=NULL;
int main(){
System *sys = System::getInstance();
sys->prn();
System *sys1 = System::getInstance();
sys1->prn();
System *sys2 = System::getInstance();
sys2->prn();
delete sys;
System *sys3 = System::getInstance();
sys3->prn();
Solution:
#include<iostream>
class fraction{
private:
int num;
int dem;
public:
//add();
//sub();
void display();
void reduceform();
};
}
*/
//___________________constructor
dem=d;
if (dem==0){
dem=1;
//_________________desctructor
fraction::~fraction(){
//____________________reduceform
void fraction::reduceform(){
num = num / i;
dem = dem / i;
if(num==dem){
num=1;
dem=1;
cout << "rduced form=" << num << "/" << dem << endl;;
//.....................assignment
num=obj.num;
dem=obj.dem;
}
//_________________________________ADD
fraction res;
res.num=num*obj.dem + obj.num*dem;
res.dem=dem*obj.dem;
return res;
//____________________________________SUB
fraction res;
res.num=num*obj.dem - obj.num*dem;
res.dem=dem*obj.dem;
return res;
//__________________________________MULtiply
fraction res;
res.num=num*obj.num;
res.dem=dem*obj.dem;
return res;
//________________________________divide
fraction res;
res.num=num*obj.dem;
res.dem=dem*obj.num;
return res;
}
//__________________________________Equal
if (num==obj.num&& dem==obj.dem)
return true;
return false;
//______________________________PreFix increment
fraction fraction::operator++(){
num++;
dem++;
return fraction(num,dem);
//_____________________________PostFix addition
fraction d(num,dem);
num++;
dem++;
return d;
//________________________________postfix decrement
fraction d(num,dem);
num--;
dem--;
return d;
}
//___________________________prefix decrement
fraction fraction::operator--(){
num--;
dem--;
return fraction(num,dem);
fraction fraction::operator-(){
fraction obj=*this;
obj.num=obj.num*-1;
return obj;
fraction fraction::operator+(){
return *this;
//___________________________________________extraction
return out;
//_________________________________ insertion
in >> f.num;
return in;
//________________________display
void fraction::display(){
cout<<num<<"/"<<dem<<endl;
int main(){
fraction f1(2,3);
fraction f2(5,4);
++f1;
f1.display();
--f2;
f2.display();
f1++;
f1.display();
f2--;
f2.display();
if (f1==f2)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
fraction g(70,120);
g.reduceform();
fraction f10(9,7);
fraction f11;
f11=f10;
f11.display();
fraction left;
fraction right;
//cout<<endl;
return 0;}