OOP Lab Report # 02

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

OBJECT-ORIENTED

PROGRAMMING

SUBMITTED TO: MAM NAYAB GOGOSH


SUBMITTED BY: DAYYAN SALEEM
REG NO: FA20-EEE-033
Lab Report # 02

TASK 1
Write a class that displays a simple message “I am object no. ”, on the screen whenever an
object of that class is created.
#include <iostream>
using namespace std;
class object_counter
{ private:
static int count;
public:
object_counter()
{
count++;
cout << "I am object no." << count << endl;
}; };
int object_counter::count=0;
int main()
{
object_counter obj1, obj2, obj3, obj4;
return 0;
}
TASK 2
Create a class that imitates part of the functionality of the basic data type int, call the class Int.
The only data in this class is an integer variable. Include member functions to initialize an Int
to zero, to initialize it to an integer value and to display it. Write a program that exercises this
class by creating an Int variable and calling its member functions.

#include <iostream>
using namespace std;
class Int
{
int data;
public:
void initialize_zero(){ data=0; }
void initialize_value(int val){ data=val; }
void display() {cout << data << endl << endl; }
};
int main()
{
Int i1, i2;
cout << "Object 1 initialized as zero and displayed: " << endl;
i1.initialize_zero();
i1.display();
cout << "Object 2 initialized with a constant and displayed: " << endl;
i2.initialize_value(4);
i2.display();
return 0;
}
TASK 3
Write a program to calculate the number of objects created and destroyed for the counter
class.
#include <iostream>
using namespace std;
class counter
{
private:
static int count_created, count_destroyed;
public:
counter()
{
count_created++;
cout << "Number of objects created= " << count_created << endl;
};
~counter()
{
count_destroyed++;
cout << "Number of objects destroyed= " << count_destroyed << endl;
};
};
int counter::count_created=0, counter::count_destroyed=0;
int main()
{
counter obj1;
for(int i=2; i<=5; i++)
{
counter obj;
cout << "Iteration no. " << i << endl;
}
return 0;
}

TASK 4
Create a class named time, the data members are hours, minutes and seconds. Write a
function
to read the data members supplied by the user, write a function to display the data
members in
standard (24) hour and in (12) hour format.
#include <iostream>
using namespace std;
class time
{
private:
int hours;
int minute;
int seconds;
public:
void setvalue(int,int,int);
void showtime(int h,int m,int s)
{
int c;
if(h>=12)
{
c=h-12;
cout<<h<<":"<<m<<":"<<s;
cout<<"\n";
cout<<c<<":"<<m<<":"<<s;
}
else
{cout<<"24 hour Format"<<h<<":"<<m<<":"<<s;
cout<<"\n";
cout<<"12 hour format"<<c<<":"<<m<<":"<<s;
}
}
};
void time::setvalue(int h,int m,int s)
{
hours=h;
minute=m;
seconds=s;
showtime(h,m,s);
}
int main()
{
time t1;
int x,y,z;
cout<<"Enter hours = ";
cin>>x;
cout<<"Enter minute = ";
cin>>y;
cout<<"Enter seconds = ";
cin>>z;
t1.setvalue(x,y,z);
return 0;
}

TASK 5
Write a class marks with three data members to store three marks. Write three member
functions, set_marks() to input marks, sum() to calculate and return the sum and avg() to
calculate
and return average marks.
#include <iostream>
using namespace std;
class marks
{
private:
int m1;
int m2;
int m3;
public:
void setvalue(int,int,int);
int avg(int x,int y,int z)
{
int a=(x+y+z)/3;
return a;
}
int sum(int x,int y,int z)
{
int s=x+y+z;
return s;
}
};
void marks::setvalue(int x,int y,int z)
{
m1=x;
m2=y;
m3=z;
}
int main()
{
marks sheet1;
int x,y,z,a,s;
cout<<"Enter marks 1 = ";
cin>>x;
cout<<"Enter marks 2 = ";
cin>>y;
cout<<"Enter marks 3 = ";
cin>>z;
sheet1.setvalue(x,y,z);
a=sheet1.avg(x,y,z);
s=sheet1.sum(x,y,z);
cout<<"average is ="<<a;
cout<<"\nsum is ="<<s;
return 0;
}

Critical Analysis
In this lab we learnt about
 The importance of classes and construction of objects using classes.
 How to declare member functions and member variables of a class.
 Constructor and destructor.

You might also like