Professional Documents
Culture Documents
Object Oriented Programming Lab Journal - Lab 3
Object Oriented Programming Lab Journal - Lab 3
Object Oriented Programming Lab Journal - Lab 3
Enrollment #: ___________01-235191-009________________
Objectives:
Following programming skills should be acquired in this lab:
Implement the given exercises and get them checked by your instructor. If you are unable to
complete the tasks in the lab session, deposit this journal alongwith your programs (printed
or handwritten) before the start of the next lab session.
2. Exercise 3.2
3. Exercise 3.3
4. Exercise3. 4
Exercise 3.1
Create a class called Complex for performing arithmetic with complex numbers. Complex
numbers have the form real + i imag, where i = v-1. Use floating point variables to represent the
private data of the class. Provide a constructor to initialize a complex number when it is declared.
The constructor should contain the default values in case no initializing values are provided.
float real;
float imag;
public:
void input()
{
cin>>real>>imag;
}
void showadd()
{
cout<<endl;
cout<<"("<<real<<")"<< " + " <<"("<<imag<<")"<<"i";
cout<<endl;
}
void showsub()
{
cout<<endl;
cout<<"("<<real<<")"<< " - " <<"("<<imag<<")"<<"i";
cout<<endl;
}
void showmul()
{
cout<<endl;
cout<<"("<<real<<")"<< " * " <<"("<<imag<<")"<<"i";
cout<<endl;
}
complex add(complex x ,complex y)
{
complex ad;
ad.real=x.real + y.real;
ad.imag=y.imag + x.imag;
return ad;
}
complex sub(complex x,complex y)
{
complex subtr;
subtr.real=x.real - y.real;
subtr.imag=y.imag - x.imag;
return subtr;
}
complex mul(complex x,complex y)
{
complex multi;
multi.real=x.real * y.real;
multi.imag = (-1)*(x.imag * y.real + y.imag * x.real);
return multi;
}
};
int main()
{
complex a , b , c, d, e, f, g,h ;
cout<<"Enter Frist Two Complex Number"<<endl;
a.input();
cout<<"Enter Second Two Complex Number"<<endl;
b.input();
cout<<endl;
d=c.add(a,b);
cout<<"Addition of Two Complex Number"<<endl;
d.showadd();
cout<<endl;
f=e.sub(a,b);
cout<<"Subtraction of Two Complex Number"<<endl;
f.showsub();
cout<<endl;
g=h.mul(a,b);
cout<<"Multipication of Two Complex Number"<<endl;
g.showmul();
cout<<endl;
}
Exercise 3.2
Write a class to calculate the area and circumference of a circle.
Class Circle
Private:
float radius;
Public:
ComputeArea(); //Area:Πr
ComputeCircumference(); //Circumfernce:2Πr
void computeArea();
void circumference();
};
cin>>radius;
}
Exercises 3.3
Write a C++ class named date. Its member data should consist of three ints: month, day, and
year. It should also have two member functions: getdate(), which allows the user to enter a date in
12/31/02 format, and showdate(), which displays the date.
void showdate()
{
cout<<endl;
cout<<"Today Date"<<endl;
if(month >0 && month <=12 && day>0 && day<=31)
cout<<month<<"/"<<day<<"/"<<year<<endl;
else
cout<<"Enter Invalid Date "<<endl;
}
};
int main()
{
date d1;
cout<<"Enter Date This Format Month/Day/Year "<<endl;
d1.getdate();
d1.showdate();
}
Exercise 3.4
Create a class called ship that incorporates a ship’s number and location in longitudes and
latitudes. A member function of the ship class should get a position from the user and store it in
the object; another should report the serial number and position. Write a main() program that
creates three ships, asks the user to input the position of each, and then
displays each ship’s number and position.
num++;
serial = num;
}
void getvalues()
{
cout << "Enter the Degree: "<<endl;
cin >> deg;
cout << "Enter the Minutes: "<<endl;
cin >> min;
cout << "Enter the Direction: "<<endl;
cin >> direction;
}
void display()
{
cout << "Ship "<< serial << " Logitude & Latitude " << " " << deg << "°"<< min << "'" <<
direction;
}
void display1()
{
cout <<", " << deg << "°"<< min << "'" << direction<<endl;
}
};
int ship::num=0;
int main()
{
ship a,b,c,d,e,f,g;
a.display();
e.display1();
b.display();
f.display1();
c.display();
g.display1();
}
+++++++++++++++++++++++++