0% found this document useful (0 votes)
42 views

Oop Lab 2

lab 2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Oop Lab 2

lab 2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

COMSATS UNIVERSITY OF ISLAMABAD, ISLAMABAD CAMPUS

Object Oriented Program


Lab # 01: Lab 02 – Classes and Data Abstraction

Name
Maryam Khaliq

Registration Number FA22-BEE-115

BEE-3C
Class

Asma Bibi
Instructor’s Name

Lab Assessment

Post Lab Total

In-Lab Data Presentation Data Analysis Writing Style


➢ Examples:
1. The following example shows the declaration of class Rectangle. It also
demonstrates how member functions can be defined both inside and
outside the class and how objects of class rectangle can be used to access
its member functions.

• 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 initialize(int value) {


data = value;
}

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:

1. Write a program to calculate the number of objects created and


destroyed for the counter class.

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

float m1, m2, m3;


cout << "Enter three marks: ";
cin >> m1 >> m2 >> m3;

studentMarks.set_marks(m1, m2, m3);

cout << "Sum of marks: " << studentMarks.sum() << endl;


cout << "Average marks: " << studentMarks.avg() << endl;

return 0;
}

• OUTPUT:
➢ CONCLUSION:

In this lab, I explored the fundamental concepts of object-oriented programming,


focusing on classes, objects, constructors, and destructors. I learned that classes are
blueprint templates for creating objects. Objects, on the other hand, are instances of
classes. They represent real-world entities and are created based on class
definitions. Constructors are special member functions within a class that are
responsible for initializing object data members when an object is created.
Destructors are created automatically with constructors and destroy what's made
with constructors and freeing memory. I learned a lot of new C++ concepts while
writing different programs for lab and home assignments. In conclusion this lab
was very helpful in understanding the key concepts of object oriented
programming.

You might also like