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

Oop 9

The lab report summarizes two programs that use constructors in C++. The first program defines a time class with default and parameterized constructors, and includes functions to add time objects and display the time. The second program defines a vehicle class with a constructor that initializes attributes, and includes a display function that calculates charges with or without a 10% discount. Both programs demonstrate the use of constructors and passing objects between functions.

Uploaded by

Dewanand Giri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Oop 9

The lab report summarizes two programs that use constructors in C++. The first program defines a time class with default and parameterized constructors, and includes functions to add time objects and display the time. The second program defines a vehicle class with a constructor that initializes attributes, and includes a display function that calculates charges with or without a 10% discount. Both programs demonstrate the use of constructors and passing objects between functions.

Uploaded by

Dewanand Giri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Report of: OOP Date: 2020/09/21

Lab Report on: Constructors Report no. :9

Submitted To: Submitted By:

Name of teacher: Jeevan Dhungel Name of Student: Dewanand Giri

Subject: OOPL Lab Roll no. : 8

Section: A

Semester: 2nd
1. Write a program that has a class to represent time. The class
should have constructors initialize data members hour, minute and
second to 0 and to initialize them to values passes as arguments. The
class should have member function to add time objects and return the
result as time object. There should be another function to display the
result in 24 hour format.

Ans:

#include<iostream>
using namespace std;
class time
{
private:
int hh,m,s;
public:
time add(time);
void display();
time()
{
hh=0;
m=0;
s=0;
}
time(int a,int b,int c)
{
hh=a;
m=b;
s=c;
}
};
time time::add(time y)
{
time x;
x.s=s+y.s;
x.m=x.s/60;
x.s=x.s%60;
x.m=x.m+m+y.m;
x.hh=x.m/60;
x.m=x.m%60;
x.hh=x.hh+hh+y.hh;
return x;
}
void time::display()
{
cout<<hh<<" hour "<<m<<" minutes and "<<s<<" seconds"<<endl;
}
int main()
{
time t1,t2(10,30,05),t3(8,50,36),t4;
cout<<"\nFirst time is\n";
t1.display();
cout<<"\nSecond time is\n";
t2.display();
cout<<"\nThird time is\n";
t3.display();
t4=t2.add(t3);
cout<<"\nTime after addition is\n";
t4.display();
return 0;
}

2. Assume that one constructor initializes data member say


num_vehicle, hour and rate. There should be 10% discount if
num_vehicle exceeds 10. Display the total charge. Use two objects
and show bit-by-bit copy of one object to another (make your own
copy constructor)

Ans:

#include<iostream>
using namespace std;
class vechicle
{
private:
int num_vechicle,h;
float r,c;
public:
vechicle(int x,int y,float z)
{
num_vechicle=x;
h=y;
r=z;
}
void display()
{
if(num_vechicle>10)
{
c=h*r;
c=c-((float(10)/100)*c);
cout<<"Charge is "<<c<<endl;
}
else
{
c=h*r;
cout<<"Charge is "<<c<<endl;
}
}
vechicle(vechicle &v2)
{
num_vechicle=v2.num_vechicle;
h=v2.h;
r=v2.r;
}
};
int main()
{
vechicle v1(10,12,700);
v1.display();
vechicle v3=v1;
v3.display();
return 0;
}
3. Write a class that can store Department ID and Department Name
with constructors to initialize its members. Write destructor member
in the same class and display the message”. Object n goes out of
scope”. Your program should be made such that it should show the
order of constructor and destructor invocation.

Ans:

#include<iostream>
#include<cstring>
using namespace std;
class department
{
private:
int id;
string na;
public:
department(int i,string n)
{
id=i;
na=n;
cout<<"object "<<id<<" is created with name "<<na<<endl;
}
~department()
{
cout<<"object "<<id<<" goes out of scope"<<endl;
}
};
int main()
{
department d1(1,"Kaushaltar department ");
department d2(2,"lokanthali department ");
return 0;
}
Output:

You might also like