AIR UNIVERSITY
DEPARTMENT OF ELECTRICAL ENGINEERING
Lab NO 03
Lab Title: Classes,constructor,destructor.
Student Name: Muhammad Zeeshan Reg. No: 210711
Objective: To know about use of constructor in class and different types of
constructor.
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:
TASK 01:
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 the customer. For example
setCustomerID(). • Define functions that get and show the fields of the customer. For example
getCustomerID(). • Use a constructor to set the default values to each of the fields. • Overload at
least two constructors of the customer class. • Define a function that takes input from the user
and sets 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?
INPUT:
#include <iostream>
using namespace std;
class customer
{
int amount;
int costomerid;
string firstname;
string lastname;
string adress;
int credit;
public:
void setcustomerid()
{
cout<<"enter costomer id:";
cin>>costomerid;
}
void getcustomerid()
{
cout<<"costomer id is:"<<costomerid<<endl;
}
void setdata()
{
cout<<" fist name:";
cin>>firstname;
cout<<"last name:";
cin>>lastname;
cout<<"adress:";
cin>>adress;
cout<<"credit amount";
cin>>credit;
cout<<"amount in bank:";
cin>>amount;
}
void getdata()
{
cout<<"name:"<<firstname<<" "<< lastname<<endl ;
cout<<"adress:"<<adress<<endl;
cout<<"credit amount"<<credit<<endl;
cout<<"amount in bank:"<<amount<<endl;
}
customer ()
{
firstname="ALI";
lastname="Khan";
adress="islamabad";
credit=7000;
amount=5500;
}
customer (string x,string y,string z,int w,int a)
{
firstname=x;
lastname=y;
adress=z;
credit=w;
amount=a;
}
customer (customer & c1)
{
firstname=[Link];
lastname=[Link];
adress=[Link];
credit=[Link];
amount=[Link];
}
};
int main()
{
customer c1,c2("ali","ahmed","rawlpindi",4500,2000 ),c3(c1);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
OUTPUT:
Task 02
Write a class Travel that has the attributes of kilometres and hours. A constructor with
noparameters 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 kilometres and hours of calling object and the parameter and return an object
with added values?
Input :
#include <iostream>
#include<stdio.h>
using namespace std;
class travel
{
int dist;
int time;
public:
travel()
{
dist=0;
time=0;
}
void getdata()
{
cout<<"distance in KM:";
cin>>dist;
cout<<"time in hour:";
cin>>time;
}
void display()
{
cout<<"DISYANCE IN KM:"<<dist<<" km."<<endl;
cout<<"TIME taken to cover "<<dist<<" km distance is "<<time<<"
hour"<<endl;
}
travel operator+(travel t)
{
travel temp;
[Link]=dist+[Link];
[Link]=time+[Link];
return temp;
}
};
int main()
{
travel t1,t2,t3;
[Link]();
[Link]();
[Link]();
[Link]();
t3=t1+t2;
cout<<"total covered:"<<endl;
[Link]();}
Output:
Task 03:
Q3. 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.
Input:
#include <iostream>
using namespace std;
class country
{
string con,capital, log,lat,area,population;
public:
country()
{
cout<<"name of the country:";
cin>>con;
cout<<"location of the country:"<<endl;
cout<<"longitude:";
cin>>log;
cout<<"latitude:";
cin>>lat;
cout<<"area:";
cin>>area;
cout<<"population:";
cin>>population;
}
void display()
{
cout<<"COUNTRY:"<<con<<endl;
cout<<"LOCATION:"<<endl<<"LONGITUDE:"<<log<<"
LATITUDE:"<<lat<<endl;
cout<<"AREA:"<<area;
cout<<"POPULATION:"<<population;
}
void namedisplay()
{
cout<<"name of the country:"<<con<<endl;
}
void locationdisplay()
{
cout<<"location of the country:"<<endl;
cout<<"LOCATION:"<<endl<<"LONGITUDE:"<<log<<"
LATITUDE:"<<lat<<endl;
}
void areadisplay()
{
cout<<"AREA OF THE COUNRY:"<<area<<endl;
}
void populationdisplay()
{
cout<<"POPULATION OF THE COUNTRY:"<<population;
}
};
int main()
{
country c1,c2,c3;
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
Output:
Task 04:
Q4. Create a class POINT to represent a point in Cartesian coordinate system.
Chooseappropriate 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
Input:
#include<iostream>
#include <cmath>
using namespace std;
class point
{
float x,y,e,r;
public :
point (){
x=0;
y=0;
}
point(int a,int b){
x=a;
y=b;
}
point(point &p){
x=p.x;
y=p.y;
}
void setdata(){
cout<<" Enter x co-ordinate :";
cin>>e;
cout<<endl;
cout<<" Enter y co-ordinate :";
cin>>r;
cout<<endl;
}
void zero(){
if(e==0 && r==0)
cout<<"Points are (0,0)"<<endl;
else
cout<<"Point are not zero"<<endl;
}
void isequalto(point a){
if(e==a.e && r==a.r)
cout<<"Points are Equal "<<endl;
else
cout<<"Points are not Equal "<<endl;
}
void mid(point b){
point t1;
t1.x=(sqrt((b.x+x)*(b.x+x)))/2;
t1.y=(sqrt((b.y+y)*(b.y+y)))/2;
cout<<"Mid Point : "<<" ("<<t1.x<<","<<t1.y<<")\n";
point distance(point b){
point t2;
t2.x=sqrt((b.x-x)*(b.x-x));
t2.y=sqrt((b.y-y)*(b.y-y));
return t2;
}
float distance(){
float t3;
t3=sqrt(x*x+y*y);
cout<<"distance is :"<<t3<<endl;
return t3;
}
float isgreater(point c){
if(x>=c.x && y>=c.y)
cout<<"Point is greater than : "<<"("<<c.x<<","<<c.y<<")\n";
else
{ cout<<"Point is less than : "<<"("<<c.x<<","<<c.y<<")\n";}
}
void display(){
cout<<"Point :"<<" ("<<x<<","<<y<<")\n";
}
};
int main (){
point p1,p2(4,3),p3,p4;
[Link]();
[Link]();
[Link]();
[Link]();
[Link](p3);
[Link](p3);
[Link](p2);
}
Output:
Task 05:
Q5. Create a class SavingsAccount that helps to maintain the accounts of different customers
each customer having its own savingsBalance and also stores an annualInterestRateProvide a
member function to calculate Monthly Interest That calculate the monthly interest by
(balance*annualInterestRate)/12. This interest should be added to savingBalance.
Input:
#include <iostream>
#include<stdio.h>
using namespace std;
class savingaccount
{
float bal,ir,moir;
public:
void setdata()
{
cout<<"enter the annual saving balance of the member:";
cin>>bal;
cout<<"enter the interst rate:";
cin>>ir;
}
void displaydata()
{
cout<<"ANNUAL SAVING BALANCE:"<<bal<<endl;
cout<<"INTEREST RATE:"<<ir<<endl;
}
void mir()
{
moir=(bal*ir)/12;
cout<<"mounthly interest rate:";
cout<<moir<<endl;
}};
int main()
{
savingaccount s[100000];
int k;
cout<<"enter no of members:";
cin>>k;
for(int i=0;i<k;i++)
{s[i].setdata();
s[i].displaydata();
s[i].mir();
}
}
Output:
Conclusion:
In this lab we have used constructors of different types(default,copy,parameterized).constructors
are declared inside the class without data [Link] are used to construct the objects .
While we have also used destructors .Destructors are used to destroy [Link] have also
studied operator overloading these are used to increment ,decrement,multiply and divide.
This has different ways to call and the operation is stored in a new object. And that new object
is stored in the output [Link] this we can save the data of different [Link] also can
use arrays these are used to call a lot of objects.