OOP Lab Report 02
OOP Lab Report 02
OOP Lab Report 02
1. Objectives:
Objective of this lab is to understand the importance of classes and construction of objects
using classes.
2. Introduction:
The fundamental idea behind object-oriented languages is to combine into a single unit both data
and the functions that operate on that data. Such a unit is called an object. A class serves as a
plan, or blueprint. It specifies what data and what functions will be included in objects of that
class. An object is often called an “instance” of a class Syntax:
Classes are generally declared using the keyword class, with the following format:
• A doctor sees the person as patient. Thus he is interested in name, height, weight, age, blood
group, previous or existing diseases etc of a person
• An employer sees a person as an employee. Therefore employer is interested in name, age,
health, degree of study, work experience etc of a person.
3.4 Constructors:
It is a special function that is automatically executed when an object of that class is created. It has no
return type and has the same name as that of the class. It is normally defined in classes to initialize data
members.
Syntax:
class_name( )
// Constructor body
Syntax:
~ class_name ()
// Destructor body
3. In-Lab Tasks:
3.1. Task#01
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>
class Alpha
public:
Alpha()
Count++;
};
int Alpha::Count=0;
int main()
Alpha C1,C2,C3,C4,C5;
Output:
Write a program to calculate the number of objects created and destroyed for the counter
class.
Code:
#include<iostream>
using namespace std;
class Alpha
{
public:
static int Count;
static int Destruc;
Alpha()
{
Count++;
}
~Alpha()
{
Destruc++;
if(Destruc==Count)
{
cout<<"\n\n\t Total Objects
Created:"<<Count<<endl;
cout<<"\n\n\t Total Objects
Destroyed:"<<Destruc<<endl;
}
}
int Alpha::Count=0;
int Alpha::Destruc=0;
int main()
{
Alpha C1,C2,C3,C4,C5,C6,C7;
}
Output:
Code:
#include<iostream>
using namespace std;
class INT
{
private:
int Num;
public:
void Single_Zero()
{
Num=0;
}
void Single_Intger(int A)
{
Num=A;
}
void Display()
{
cout<<"\n\n\t Value Stored In A:
"<<Num;
}
void Sum(INT O4,INT O5)
{
Num=O4.Num+O5.Num;
}
};
int main()
{
INT O1,O2,O3;
O2.Single_Intger(15);
O3.Single_Intger(8);
1. Post-Lab Tasks:
1.1. Task#01
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
{
public:
int Hours;
int Mins;
int Secs;
Time()
{
cout<<"\n\n\t Please Enter The Hours: ";
cin>>Hours;
cout<<"\n\t Please Enter The Minutes: ";
cin>>Mins;
};
int main()
{
Time Alpha;
char Meridiem;
int Format;
int H;
H=Alpha.Hours;
class Student
{
public:
int Stu_No_1;
int Stu_No_2;
int Stu_No_3;
int Sum;
int Summer();
int Average(int);
void Set_Mark();
};
void Student::Set_Mark()
{
cout<<"\n\n\t Please Enter The Marks of Student No
1 : "; cin>>Stu_No_1;
cout<<"\n\t Please Enter The Marks of Student No 2
: "; cin>>Stu_No_2;
cout<<"\n\t Please Enter The Marks of Student No 3
: "; cin>>Stu_No_3;
}
int Student::Summer()
{
Sum=Stu_No_1+Stu_No_2+Stu_No_3;
return(Sum);
}
int main()
{
Student Alpha;
int Sum;
int Avg;
// Setting Marks Of Student
Alpha.Set_Mark();
// Creating Sum Of All Student Numbers
Sum=Alpha.Summer();
// Creating Average Of The Students Marks
Avg=Alpha.Average(Sum);
// Displaying Results
cout<<"\n\n\t Sum Of All The Marks Of Student =
"<<Sum;
cout<<"\n\t Average Of All The Student Marks =
"<<Avg;
}
Output:
Advantages of Constructors:
Automatic initialization of objects at the time of their declaration.
Multiple ways to initialize objects according to the number of arguments passes
while declaration.
The objects of child class can be initialized by the constructors of base class.
Advantages of Destructor:
It gives the final chance to clean up the resources that are not in use to release
the memory occupied by unused objects like deleting dynamic objects, close of the
system handles, used files.