0% found this document useful (0 votes)
10 views23 pages

Lab 8

The document outlines a series of programming tasks focused on Object Oriented Programming concepts in C++, including the creation of classes with various functionalities such as constructors, destructors, getters, setters, operator overloading, and static members. It provides detailed instructions for implementing classes like 'Student', 'Circle', 'Employee', and 'Fraction', along with example code for each task. The tasks emphasize deep copy, constant attributes, and the Singleton design pattern, showcasing practical applications of OOP principles.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

Lab 8

The document outlines a series of programming tasks focused on Object Oriented Programming concepts in C++, including the creation of classes with various functionalities such as constructors, destructors, getters, setters, operator overloading, and static members. It provides detailed instructions for implementing classes like 'Student', 'Circle', 'Employee', and 'Fraction', along with example code for each task. The tasks emphasize deep copy, constant attributes, and the Singleton design pattern, showcasing practical applications of OOP principles.

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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

University Of Central Punjab

Object Oriented Programming


revision
fall 2021

Lab Topic: REVISION topics


Constructor (default, parameterized and copy)
Destructor
Getter
Setter
Deep copy
Constant attribute, constant function
Static attribute, static function
Singleton
Operator overloading

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>

using namespace std;

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

Student(char *n,char* p,int s,char* sec,float g){

///deepcopy name....................

int len=0;

for(int i=0;n[i]!='\0';i++){

len++;

Name=new char(len + 1);

for(int j=0 ; j<len ; j++){

Name[j]=n[j];

Name[len]='\0';

//deepcopy(Program)...................

len=0;

for(int i=0;p[i]!='\0';i++){

len++;

Program=new char(len + 1);

for(int j=0 ; j<len ; j++){

Program[j]=p[j];

Program[len]='\0';

//deepcopy(Section)..................

len=0;

for(int i=0;sec[i]!='\0';i++){

len++;

Section=new char(len + 1);

for(int j=0 ; j<len ; j++){

Section[j]=sec[j];
}

Section[len]='\0';

//shallow copy Semester..........

Semester=s;

// shallow copy gpa..............

GPA=g;

/// copy constructor....................

Student(Student &obj){

//deep copy Name.........................

int len=0;

for(int i=0;obj.Name[i]!='\0';i++){

len++;

Name=new char(len + 1);

for(int j=0 ; j<len ; j++){

Name[j]=obj.Name[j];

Name[len]='\0';

// deep copy Program

len=0;

for(int i=0;obj.Program[i]!='\0';i++){

len++;

Program=new char(len + 1);

for(int j=0 ; j<len ; j++){

Program[j]=obj.Program[j];
}

Program[len]='\0';

//deep copy Section..................................

len=0;

for(int i=0;obj.Section[i]!='\0';i++){

len++;

Section=new char(len + 1);

for(int j=0 ; j<len ; j++){

Section[j]=obj.Section[j];

Section[len]='\0';

//shallow copy Semester.......................

Semester=obj.Semester;

//shallow copy GPA..............................

GPA=obj.GPA;

//setName deepcopy.........................

void setName(char* n)

int len=0;

for(int i=0;n[i]!='\0';i++){

len++;
}

Name=new char(len + 1);

for(int j=0 ; j<len ; j++){

Name[j]=n[j];

Name[len]='\0';

//getname deepcopy...................................

char* getName()

int len=0;

for(int i=0;Name[i]!='\0';i++){

len++;

Name=new char(len + 1);

for(int j=0 ; j<len ; j++){

Name[j]=Name[j];

Name[len]='\0';

return Name;

//set Program deepcopy..................

void setProgram(char* p){

int len=0;

for(int i=0;p[i]!='\0';i++){

len++;

Program=new char(len + 1);

for(int j=0 ; j<len ; j++){

Program[j]=p[j];
}

Program[len]='\0';

//get Program deepcopy..........................

char* getProgram()

int len=0;

for(int i=0;Program[i]!='\0';i++){

len++;

Program=new char(len + 1);

for(int j=0 ; j<len ; j++){

Program[j]=Program[j];

Program[len]='\0';

return Program;

void setSection(char* sec){

int len=0;

for(int i=0;sec[i]!='\0';i++){

len++;

Section=new char(len + 1);

for(int j=0 ; j<len ; j++){

Section[j]=sec[j];

Section[len]='\0';

}
char* getSection() {

int len=0;

for(int i=0;Section[i]!='\0';i++){

len++;

Section=new char(len + 1);

for(int j=0 ; j<len ; j++){

Section[j]=Section[j];

Section[len]='\0';

void setSemester(int s){

Semester=s;

int getSemester()

return Semester;

void setGPA(float g){

GPA=g;

float getGPA()

return GPA;

void display() const

{
cout<<".................................."<<endl;

cout<<".................................."<<endl;

cout<<".................................."<<endl;

cout<<"Student name is :"<<Name<<endl;

cout<<"Student Program is: "<<Program<<endl;

cout<<"Student Section is: "<<Section<<endl;

cout<<"Student Semester is: "<<Semester<<endl;

cout<<"Student GPA is : "<<GPA<<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;

Task 2: constant attribute and Copy Constructor

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.

You should implement a copy constructor to perform declaration time initialization.

Driver Program should be like this:

Circle c1(5);
Circle c2 = c1;

cout<<"Before 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;
cout<<"--------------------------------------------"<<endl;

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>

using namespace std;

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;

cout<<"Before 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;
cout<<"--------------------------------------------"<<endl;

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;
}

Task 3: Static data member, pointer of object


Write a program in c++ that create an Employee class which has 2 attributes
 integer id
 static integer count
create its default and parameterized constructor
Create its getter & setter create a function of display that will display the employee id and
total count of employees i.e.
 Setter
 Getter
 Display(display employee id and total number of employee count)

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;
}

static void getCount(){

cout<<"The value of count is "<<count<<endl;


}
};

// Count is the static data member of class Employee


int Employee::count; // Default value is 0

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();

Task 6: Operator overloading


Create a class called Fraction for performing arithmetic with fractions. Write a driver
program to test your class. Use integer variables to represent the private data of the class, the
numerator and the denominator. Provide a constructor function that enables an object of this
class to be initialized when it is declared.
The constructor should contain default values in case no initializers are provided and should
store the fraction in reduced form (use a utility function to reduce) (i.e., the fraction 2/4 would
be stored in the object as 1 in the numerator and 2 in the denominator).
You have to implement functionalities to:
1. Add fractions using the operator +
2. Subtract fractions using the operator –
3. Multiply fractions using the operator *
4. Divide fractions using the operator /
5. Compare fraction using operators ==
6. increment fractions using post-increment and pre-increment operators ++
7. Decrement fractions using post-decrement and pre-decrement operators - -
8. Take negative of fractions using operator unary –. Also overload the fraction unary +

Solution:

#include<iostream>

using namespace std;

class fraction{

private:

int num;

int dem;

public:

fraction(int n=0,int d=1);


~fraction();

//add();

//sub();

void display();

void reduceform();

fraction operator+(const fraction&); // add

fraction operator-(const fraction&); // subtracr

fraction operator*(const fraction&); //multiply

fraction operator/(const fraction&); // divide

//fraction operator float(int) ;


fraction operator+(); //uniry

fraction operator-(); //negitive uniry

fraction operator++(); // increment

fraction operator--(); // decriment

fraction operator++(int); //increment

fraction operator--(int); //decriment

fraction operator=(const fraction &); // assignment

bool operator==(const fraction &obj); // bool equalieny

friend ostream& operator<<(ostream& out, fraction& f); //extraction

friend istream& operator>>(istream& in, fraction& f); //insertion

};

/*fraction fraction::operator float(int)

return float( num ) / float( dem);

}
*/
//___________________constructor

fraction::fraction(int n,int d){


num=n;

dem=d;

if (dem==0){

dem=1;

//_________________desctructor

fraction::~fraction(){

//____________________reduceform

void fraction::reduceform(){

for (int i = 1; i < dem; i++){

if (num%i == 0 && dem%i == 0){

num = num / i;

dem = dem / i;

if(num==dem){

num=1;

dem=1;

cout << "rduced form=" << num << "/" << dem << endl;;

//.....................assignment

fraction fraction ::operator=(const fraction &obj){

num=obj.num;

dem=obj.dem;
}

//_________________________________ADD

fraction fraction ::operator+(const fraction& obj){

fraction res;

res.num=num*obj.dem + obj.num*dem;

res.dem=dem*obj.dem;

return res;

//____________________________________SUB

fraction fraction ::operator-(const fraction& obj){

fraction res;

res.num=num*obj.dem - obj.num*dem;

res.dem=dem*obj.dem;

return res;

//__________________________________MULtiply

fraction fraction ::operator*(const fraction& obj){

fraction res;

res.num=num*obj.num;

res.dem=dem*obj.dem;

return res;

//________________________________divide

fraction fraction ::operator/(const fraction& obj){

fraction res;

res.num=num*obj.dem;

res.dem=dem*obj.num;

return res;
}

//__________________________________Equal

bool fraction ::operator==(const fraction &obj){

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 fraction::operator++( int){

fraction d(num,dem);

num++;

dem++;

return d;

//________________________________postfix decrement

fraction fraction::operator--( int){

fraction d(num,dem);

num--;

dem--;

return d;

}
//___________________________prefix decrement

fraction fraction::operator--(){

num--;

dem--;

return fraction(num,dem);

//___________________________________negative uniry operator

fraction fraction::operator-(){

fraction obj=*this;

obj.num=obj.num*-1;

return obj;

//___________________________________overload the fraction unary+

fraction fraction::operator+(){

return *this;

//___________________________________________extraction

ostream& operator<<(ostream& out, fraction& f)

out << f.num << "/" << f.dem;

return out;

//_________________________________ insertion

istream& operator>>(istream& in, fraction& f)

cout << "Please enter the numerator part: ";

in >> f.num;

cout << "Please enter the denominator part: ";


in >> f.dem;

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;

cin >> left;

cin >> right;

fraction result = left + right;

//cout<<endl;

cout << result << endl;

return 0;}

You might also like