Oop Lab 2
Oop Lab 2
Name
Maryam Khaliq
BEE-3C
Class
Asma Bibi
Instructor’s Name
Lab Assessment
• CODE:
#include <iostream>
using namespace std;
class Rectangle {
int length, width;
public:
void set_values (int,int);
int area () {return (length*width);}
};
void Rectangle::set_values (int l, int w) {
length = l;
width = w;
}
int main () {
Rectangle rect;
rect.set_values (3,4);
cout << "area =" << rect.area( );
return 0;
}
• OUTPUT:
2. This example demonstrates how constructors and destructors work.
• CODE:
#include<iostream>
using namespace std;
class counter
{
private:
int count;
public:
counter() { count=0; cout<<"I am a constructor"<<endl; }
~counter() { cout<<"I am a destructor"<<endl; }
};
int main( )
{
counter c;
return 0;
}
• OUTPUT:
➢ LAB Tasks
1. Write a class that displays a simple message “I am object no. __”, on the
screen whenever an object of that class is created.
• CODE:
#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;
}
• OUTPUT:
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.
• CODE:
#include <iostream>
using namespace std;
class Int {
private:
int data;
public:
Int() {
data = 0;
}
void display() {
cout << "Value: " << data << endl;
}
};
int main() {
Int myInt;
myInt.initialize(42);
myInt.display();
myInt.initialize(0);
myInt.display();
return 0;
}
• OUTPUT:
➢ Home Assignments:
• CODE:
#include <iostream>
using namespace std;
class Counter {
public:
Counter() {
count++;
}
~Counter() {
count--;
}
static int getCount() {return count;
}
private:
static int count;
};
int Counter::count = 0;
int main() {
Counter obj1;
Counter obj2;
Counter obj3;
Counter obj4;
std::cout << "Objects created: " << Counter::getCount() << std::endl;
Counter* objPtr = new Counter();
delete objPtr;
std::cout << "Objects destroyed: " << Counter::getCount() << std::endl;
return 0;
}
• OUTPUT:
2. 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.
• CODE:
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
int seconds;
public:
void setTime() {
cout << "Enter hours: ";
cin >> hours;
cout << "Enter minutes: ";
cin >> minutes;
cout << "Enter seconds: ";
cin >> seconds;
}
void display24HourFormat() {
cout << "Time in 24-hour format: ";
cout << hours << ":" << minutes << ":" << seconds << endl;
}
void display12HourFormat() {
cout << "Time in 12-hour format: ";
int hours12 = hours % 12;
string ampm = (hours < 12) ? "AM" : "PM";
cout << hours12 << ":" << minutes << ":" << seconds << " " << ampm
<< endl;
}
};
int main() {
Time t;
t.setTime();
t.display24HourFormat();
t.display12HourFormat();
return 0;
}
• OUTPUT:
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.
• CODE:
#include <iostream>
using namespace std;
class Marks {
private:
float mark1;
float mark2;
float mark3;
public:
void set_marks(float m1, float m2, float m3) {
mark1 = m1;
mark2 = m2;
mark3 = m3;
}
float sum() {
return mark1 + mark2 + mark3;
}
float avg() {
return sum() / 3.0;
}
};
int main() {
Marks studentMarks;
return 0;
}
• OUTPUT:
➢ CONCLUSION: