OOP Lab Report 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

EXPERIMENT NO 3

Lab Title: Constructor, Destructor and its types.

Student Name: haris umer Reg. No: 210285

 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

Ability to assimilate the


results

Effective use of lab


equipment and follows the
lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:


Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:


LAB TASKS:-

 Lab Task #1:

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().

• Use constructor to set the default values to each of the field.

• Overload at least two constructors of the customer class.

• Define a function that takes input from user and set all fields of customer data.

• Using copy constructor, copy the data to another object

• Define a function that displays the entire customer’s data using this new object you created.

• Create Destructors for each Constructor.

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

Customer (const Customer &c3)


{
customer_id = c3.customer_id;
first_name = c3.first_name;
last_name = c3.last_name;
address = c3.address;
credit_limit = c3.credit_limit;
}

void get_customer();
void show_customer();

};

void Customer :: get_customer()


{
cout<<"Enter Customer ID:- ";
cin>>customer_id;
cout<<"Enter First name of Customer:- ";
cin>>first_name;
cout<<"Enter Last Name of Customer:- ";
cin>>last_name;
cout<<"Enter Address of Customer:- ";
cin>>address;
cout<<"Enter Credit LImit of Customer:- ";
cin>>credit_limit;
cout<<endl;
}

void Customer :: show_customer()


{
cout<<"Customer ID:- "<<customer_id<<endl;
cout<<"First Name:- "<<first_name<<endl;
cout<<"Last Name:- "<<last_name<<endl;
cout<<"Address:- "<<address<<endl;
cout<<"Credit Limit:- "<<credit_limit<<endl<<endl;
}

int main()
{

Customer c1 (101, "Umer", "Shahid", "Kahuta", 10000);

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>

using namespace std;

class Travel

int kilometers, hours;

public:

Travel()

kilometers=0;

hours=0;

void set()

cout<<"Enter Kilometers:- ";

cin>>kilometers;

cout<<"Enter Hours:- ";

cin>>hours;

void show()

{
cout<<"Kilometers:- "<<kilometers<<"\t Hours:- "<<hours<<endl;

Travel add(Travel t2)

Travel t3;

t3.kilometers = kilometers + t2.kilometers;

t3.hours = hours + t2.hours;

return t3;

};

int main()

Travel t1, t2, t3;

t1.set();

t1.show();

cout<<endl;

t2.set();

t2.show();

cout<<endl;

cout<<"Total trvaelling:- "<<endl;

t3 = t1.add(t2);

t3.show();

return 0;

OUTPUT
 Lab Task #3:

Create a class Country. Country is identified by its name, location on earth


(which means the longitude & latitude values) area, population & capital.

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

using namespace std;

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();

};

void Country :: get_info()

cout<<"Enter Country's Location:- ";

cin>>country_location;

cout<<"Enter Country's Capital:- ";

cin>>country_capital;

cout<<"Enter Country's Area:- ";


cin>>country_area;

cout<<"Enter country's Population:- ";

cin>>country_population;

cout<<endl;

void Country :: display_allinfo()

cout<<"Country's Name:- "<<country_name<<endl;

cout<<"Country's Location:- "<<country_location<<endl;

cout<<"Country's Capital:- "<<country_capital<<endl;

cout<<"Country's Area:- "<<country_area<<endl;

cout<<"Country's Population:- "<<country_population<<endl;

cout<<endl;

void Country :: display_location()

cout<<"Country's Location:- "<<country_location;

void Country :: display_capital()

cout<<"Country's Capital:- "<<country_capital;

void Country :: display_area()

{
cout<<"Country's Area:- "<<country_area;

void Country :: display_population()

cout<<"Country's Population:- "<<country_population;

int main()

Country c1 ("Pakistan", "30.3753° N, 69.3451° E", "Islamabad", "796,095 km²",


"220.9 million");

Country c2 ("Saudi Arabia", "23.8859° N, 45.0792° E", "Riyadh", "2.15 Million


km²", "34.81 million");

Country c3 ("India", "20.5937° N, 78.9629° E", "New Delhi", "3.287 million km²",
"1.38 billion");

Country c4 ("Germany", "51.1657° N, 10.4515° E", "Berlin", "357,588 km²",


"83.24 million");

int x=0;

label:

cout<<"You want to:\n(a). access data\n(b). edit data\n(c). end program"<<endl;

cout<<"Press '0' to access data.\nPress '1' to edit data.\nPress '2' to end


program.";

cin>>x;

if (x==0)

{
int y=0;

cout<<"What do you want to know:-\n(a). All info\n(b). Location\n(c). Capital\


n(d). Area\n(e). Population\n";

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<<"Choose the country from the list below:-\n";

cout<<"(a). Pakistan\n(b). Saudi Arabia\n(c). India\n(d). Germany\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_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<<"Choose the country from the list below:-\n";

cout<<"(a). Pakistan\n(b). Saudi Arabia\n(c). India\n(d). Germany\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_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<<"Choose the country from the list below:-\n";

cout<<"(a). Pakistan\n(b). Saudi Arabia\n(c). India\n(d). Germany\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_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<<"(a). Pakistan\n(b). Saudi Arabia\n(c). India\n(d). Germany\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;

cout<<"Choose the country from the list below:-\n";

cout<<"(a). Pakistan\n(b). Saudi Arabia\n(c). India\n(d). Germany\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_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<<"Choose the country from the list below:-\n";

cout<<"(a). Pakistan\n(b). Saudi Arabia\n(c). India\n(d). Germany\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.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:

Create a class POINT to represent a point in Cartesian coordinate system.


Choose appropriate data members. Provide member functions:

• Default constructor

• Parameterized constructor

• Input

• Output

• Distance( returns the distance between two POINT

objects, the function takes one object as an input)

• isZero(determines if a point is the center)

• Midlepoint(returns the middle point, takes one object as an input and returns the answer in a
POINT object)

• isEqualTo (compare two POINTs).

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

using namespace std;

class Point

float x;

float y;
public:

Point()

x = 0.0;

y = 0.0;

Point (float xi, float yi)

x = xi;

y = yi;

void Setx (float x_i)

x = x_i;

void Sety (float y_i)

y = y_i;

}
void Setxu ()

cout<<"Enter the X Co-ordinate in Decimals: ";

cin>>x;

void Setyu ()

cout<<"Enter the Y Co-ordinate in Decimals: ";

cin>>y;

float Getx()

cout<<"X = "<<x<<endl;

return x;

float Gety()

cout<<"Y = "<<y<<endl;

return y;

}
float Distance( Point &P1)

float distance;

distance = sqrt (pow( P1.x - x, 2) + pow( P1.y - y, 2));

cout<<"Distance = "<<distance<<endl;

return distance;

Point Midlepoint( Point &P1)

Point Mid;

Mid.Setx((P1.x + x)/2 );

Mid.Sety((P1.y + y)/2);

cout<<"Middle Point = (x = "<<Mid.x<<", y = "<<Mid.y<<")."<<endl;

return Mid;

void isZero()

if (x==0 && y==0)

cout<<"The points are at origin."<<endl;


else

cout<<"The points are not at origin."<<endl;

void isEqualTo (Point &P1)

if (P1.x==x && P1.y==y)

cout<<"Points ("<<P1.x<<" , "<<P1.y<<")"<<" and ("<<x<<" , "<<y<<")"<<"are


equal."<<endl;

else

cout<<"Points ("<<P1.x<<" , "<<P1.y<<")"<<" and ("<<x<<" , "<<y<<")"<<"are


not equal."<<endl;

void isGreater (Point &P1)

float d1, d2;

d1 = sqrt (pow(0 - fabs(x),2) + (pow(0 - fabs(y),2)));

d2 = sqrt (pow(0 - fabs(P1.x),2) + (pow(0 - fabs(P1.y),2)));

if ( d1 > d2 )

cout<<"Distance ("<<x<<" , "<<y<<") is greater than ("<<P1.x<<" ,


"<<P1.y<<")."<<endl;
}

else

cout<<"Distance ("<<x<<" , "<<y<<") is less than ("<<P1.x<<" ,


"<<P1.y<<")."<<endl;

};

int main()

Point g;

g.Setx(-2);

g.isZero();

g.Gety();

Point r(9, 8.5);

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:

Create a class SavingAccountthat helps to maintain the accounts of different


customers each customer having its own savingsBalance and also stores an
annualInterestRateProvide a member function to calculate Monthlyinterestthat
calculate the monthly interest by (balance*annualInterestRate)/12. This interest
should be added to savingBalance.

CODE

#include <iostream>

using namespace std;

class SavingAccount

private:

double balance;

double annual_interest_rate;

public:

SavingAccount()

cout<<"Enter Balance:- ";

cin>>balance;

annual_interest_rate = 1.05;

void getBalance();
void getMonthlyInterest();

};

void SavingAccount::getBalance()

cout<<"Current balance:- "<<balance<<endl;

void SavingAccount::getMonthlyInterest()

cout<<"Monthly interest:- "<<(balance * annual_interest_rate)/12<<endl;

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.

You might also like