Fa20 Eee 012 Oop Lab 2
Fa20 Eee 012 Oop Lab 2
Fa20 Eee 012 Oop Lab 2
Registration
FA20-EEE-012
Number
Class EEE-3
Instructor’s Name
MAM NAYAB GOGOSH
5.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>
#include <stdio.h>
class object
{
private:
int obj;
public:
object(int x) {obj=0; cout<<"I am object no"<<x<<endl;}
};
int main()
{
object a(1), b(2), c(3);
return 0;
}
OUTPUT
:
5.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>
#include <stdio.h>
class Int
{
private:
int var;
public:
Int()
{
var=0;
cout<<"The number is:"<<var;
}
void abc()
{
cin>>var;
}
void function()
{
cout<<var;
}
};
int main()
{
Int edf ;
edf.abc();
edf.function();
return 0;
}
OUTPUT:
6. Home Assignments:
6.1 Write a program to calculate the number of objects created and
destroyed for the
counter class.
#include <iostream>
#include <stdio.h>
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 also in (12) hour format.
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace std;
class Time{
private:
int Hours,hours, minutes, seconds;
public:
void get(){
cout<<"\n KHIZER HAYAT SATTI (105)";
cout<<"\nEnter hours: ";
cin>>hours;
cout<<"\nEnter minutes: ";
cin>>minutes;
cout<<"\nEnter seconds: ";
cin>>seconds;
}
void display(){
if(hours>12){
cout<<"\nTime in 12 hour format:\n";
Hours=hours-12;
cout<<"\n"<<Hours<<":"<<minutes<<":"<<seconds;
}
}
};
int main(){
Time t;
t.get();
t.display();
return 0;
}
Output
6.3 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>
#include<stdio.h>
using namespace std;
class Marks
{
private:
int x;
int y;
int z;
public:
void setmarks()
{
cout << "KHIZER HAYAT SATTI PROGRAM "<<endl;
cout << "Enter your first marks: ";
cin >> x;
cout << "Enter your second marks: ";
cin >> y;
cout << "Enter your third marks: ";
cin >> z;
}
int sum()
{
return x + y + z;
}
double avg()
{
return sum() / 3;
}
};
int main()
{
Marks m;
m.setmarks();
cout << "Sum of marks: "<<m.sum()<<endl;
cout << "Average of marks : " << m.avg() <<endl;
return 0;
}
OUTPUT:
CRITICAL ANAYLSIS:
This lab leads to the introduction of classes , objects .