OOP Lab Report 3
OOP Lab Report 3
OOP Lab Report 3
EXPERIMENT NO 3
Objectives:
i. To get to know about Default Constructors.
ii. Understanding Passing parameters to constructors (Parametrized constructors).
iii. To understand Constructor overloading.
iv. Know the use of Copy constructor.
v. Understand the usage and importance of Destructor.
LAB ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Data presentation
Experimental results
Conclusion
Define a class Customer that holds private fields for a customer's ID, first name,
last name, address, and credit limit.
• Define functions that set the fields of customer. For example setCustomerID().
• Define functions that get and show the fields of customer. For example getCustomerID().
• Define a function that takes input from user and set all fields of customer data.
• Define a function that displays the entire customer’s data using this new object you created.
CODE
#include <iostream>
#include <string>
using namespace std;
class Customer
{
private:
int customer_id, credit_limit;
string first_name, last_name, address;
public:
Customer ()
{
customer_id = 123;
first_name = "abc";
last_name = "mno";
address = "xyz";
credit_limit = 100001;
}
~Customer()
{
}
Customer (int id, string fnm, string lnm, string add, int crd_lmt)
{
customer_id = id;
first_name = fnm;
last_name = lnm;
address = add;
credit_limit = crd_lmt;
}
void get_customer();
void show_customer();
};
int main()
{
Customer c2;
c2.get_customer();
Customer c3;
c3.get_customer();
Customer c4 = c3;
Customer c5;
c1.show_customer();
c2.show_customer();
c3.show_customer();
c4.show_customer();
c5.show_customer();
return 0;
}
OUTPUT
Lab Task #2:
Write a class Travel that has the attributes of kilometer’s and hours. A
constructor with no parameters initializes both data members to 0. A member
function set() inputs the values and function show() displays the values. It has a
member function add() that takes an object of type Travel, adds the kilometer’s
and hours of calling object and the parameter and return an object with added
values.
CODE
#include<iostream>
class Travel
public:
Travel()
kilometers=0;
hours=0;
void set()
cin>>kilometers;
cin>>hours;
void show()
{
cout<<"Kilometers:- "<<kilometers<<"\t Hours:- "<<hours<<endl;
Travel t3;
return t3;
};
int main()
t1.set();
t1.show();
cout<<endl;
t2.set();
t2.show();
cout<<endl;
t3 = t1.add(t2);
t3.show();
return 0;
OUTPUT
Lab Task #3:
• All the data members should be private. User should be able to set the value of all its
parameters either at the time of instance creation or later be able to individually change the
values of all these identifiers whenever user want to.
• Calling member function DisplayAll_Info() should display all the information pertaining to a
country instance. User should also be able to individually call functions such asDisplayName(),
DisplayCapital etc.
To test: Ask the user to enter 3 countries and all the related info. Display the info once Entered
CODE
#include <iostream>
#include <string>
class Country
private:
string country_name, country_location, country_capital, country_area,
country_population;
public:
Country (string name, string location, string capital, string area, string
population)
country_name = name;
country_location = location;
country_capital = capital;
country_area = area;
country_population = population;
void get_info();
void display_allinfo();
void display_location();
void display_capital();
void display_area();
void display_population();
};
cin>>country_location;
cin>>country_capital;
cin>>country_population;
cout<<endl;
cout<<endl;
{
cout<<"Country's Area:- "<<country_area;
int main()
Country c3 ("India", "20.5937° N, 78.9629° E", "New Delhi", "3.287 million km²",
"1.38 billion");
int x=0;
label:
cin>>x;
if (x==0)
{
int y=0;
cout<<"Press '0' for (a)\nPress '1' for (b)\nPress '2' for (c)\nPress '3' for (d)\
nPress '4' for (e)\n";
cin>>y;
if (y==0)
int z=0;
cout<<"Press '0' for (a)\nPress '1' for (b)\nPress '2' for (c)\nPress '3' for
(d)\n";
cin>>z;
if (z==0)
c1.display_allinfo();
if (z==1)
c2.display_allinfo();
if (z==2)
c3.display_allinfo();
if (z==3)
{
c4.display_allinfo();
if (y==1)
int z=0;
cout<<"Press '0' for (a)\nPress '1' for (b)\nPress '2' for (c)\nPress '3' for
(d)\n";
cin>>z;
if (z==0)
c1.display_location();
if (z==1)
c2.display_location();
if (z==2)
c3.display_location();
if (z==3)
c4.display_location();
}
if (y==2)
int z=0;
cout<<"Press '0' for (a)\nPress '1' for (b)\nPress '2' for (c)\nPress '3' for
(d)\n";
cin>>z;
if (z==0)
c1.display_capital();
if (z==1)
c2.display_capital();
if (z==2)
c3.display_capital();
if (z==3)
c4.display_capital();
if (y==3)
int z=1;
cout<<"Choose the country from the list below:-\n";
cout<<"Press '0' for (a)\nPress '1' for (b)\nPress '2' for (c)\nPress '3' for
(d)\n";
cin>>z;
if (z==0)
c1.display_area();
if (z==1)
c2.display_area();
if (z==2)
c3.display_area();
if (z==3)
c4.display_area();
if (y==4)
int z=1;
cin>>z;
if (z==0)
c1.display_population();
if (z==1)
c2.display_population();
if (z==2)
c3.display_population();
if (z==3)
c4.display_population();
if (x==1)
int z=0;
cout<<"Press '0' for (a)\nPress '1' for (b)\nPress '2' for (c)\nPress '3' for (d)\n";
cin>>z;
if (z==0)
c1.get_info();
if (z==1)
c2.get_info();
if (z==2)
c3.get_info();
if (z==3)
c4.get_info();
if (x==2)
return 0;
cout<<endl;
goto label;
OUTPUT
Lab Task #4:
• Default constructor
• Parameterized constructor
• Input
• Output
• Midlepoint(returns the middle point, takes one object as an input and returns the answer in a
POINT object)
• isGreaterThan (compares two POINT in terms of the distance from the center )
• Above two function takes one POINT object as an argument and returns true if the condition is
satisfied and false otherwise
CODE
#include<iostream>
#include<cmath>
class Point
float x;
float y;
public:
Point()
x = 0.0;
y = 0.0;
x = xi;
y = yi;
x = x_i;
y = y_i;
}
void Setxu ()
cin>>x;
void Setyu ()
cin>>y;
float Getx()
cout<<"X = "<<x<<endl;
return x;
float Gety()
cout<<"Y = "<<y<<endl;
return y;
}
float Distance( Point &P1)
float distance;
cout<<"Distance = "<<distance<<endl;
return distance;
Point Mid;
Mid.Setx((P1.x + x)/2 );
Mid.Sety((P1.y + y)/2);
return Mid;
void isZero()
else
if ( d1 > d2 )
else
};
int main()
Point g;
g.Setx(-2);
g.isZero();
g.Gety();
g.Midlepoint(r);
r.isGreater(g);
Point j=g;
g.isEqualTo(r);
g.isEqualTo(j);
Point f(-6.7,-9.5);
f.Setx(5.5);
f.Distance(r);
g.Distance(f);
g.Distance(r);
}
OUTPUT
Lab Task #5:
CODE
#include <iostream>
class SavingAccount
private:
double balance;
double annual_interest_rate;
public:
SavingAccount()
cin>>balance;
annual_interest_rate = 1.05;
void getBalance();
void getMonthlyInterest();
};
void SavingAccount::getBalance()
void SavingAccount::getMonthlyInterest()
int main()
SavingAccount a1;
a1.getBalance();
a1.getMonthlyInterest();
return 0;
}
OUTPUT
Conclusion:-
In this lab we have been introduced with the concepts of Constructors and
Destructors and their types. We have learned about setting up a default
constructor, giving a value to parametrize a constructor. We have also learned
about the use of copy constructor and its importance. Implementation of
Constructors with Destructors in a code was an important step.