Object Oriented Programming Lab Journal - Lab 3

You might also like

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

Lab Journal – Lab 3

Object Oriented Programming

Lab Journal - Lab 3


Name: _____________Dilshad Ali__________________

Enrollment #: ___________01-235191-009________________

Class/Section: ______________BS IT (2A)__________________

Objectives:
Following programming skills should be acquired in this lab:

 To understand the basic principles of data hiding and encapsulation.


 To differentiate between a class and an object.
 To understand and use keyword public and private.
 To practice and use constructor and destructor.
 To understand the use of scope resolution operator.
 To define a class with overloaded constructors.

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.

S No. Exercise Checked By:


1. Exercise 3. 1

2. Exercise 3.2

3. Exercise 3.3

4. Exercise3. 4

Object Oriented Programming Page 1


Lab Journal – Lab 3

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.

Provide public member functions to perform the following operations:


 Addition of two Complex numbers
 Subtraction of two Complex numbers
 Multiplication of two Complex numbers
 Printing of a Complex number in a+ib format

Write a driver program to test your class.

//Use IDE XCODE


#include<iostream>
#include<math.h>
using namespace std;
class complex
{

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

Object Oriented Programming Page 2


Lab Journal – Lab 3

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

Object Oriented Programming Page 3


Lab Journal – Lab 3

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

//Use IDE XCODE


#include<iostream>
#include<math.h>
using namespace std;
float pi=3.14;
class cricle
{
float crum , area;
float radius;
public:

Object Oriented Programming Page 4


Lab Journal – Lab 3

void computeArea();
void circumference();
};

void cricle:: computeArea()


{

cin>>radius;
}

void cricle:: circumference()


{
area=pi*radius;
crum=2*area;
cout<<"Total Circumference "<<endl;
cout <<crum;
}
int main()
{
cricle a;
cout<<"Enter the Radius "<<endl;
a.computeArea();
a.circumference();
cout<<endl;
}

Object Oriented Programming Page 5


Lab Journal – Lab 3

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.

//Use IDE XCODE


#include<iostream>
#include<math.h>
using namespace std;
class date
{
int month;
int day;
int year;
public:
void getdate()
{
cin>>month;
cin>>day;
cin>>year;
}

void showdate()

Object Oriented Programming Page 6


Lab Journal – Lab 3

{
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

Object Oriented Programming Page 7


Lab Journal – Lab 3

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.

//Use IDE XCODE


1#include<iostream>
using namespace std;
class ship{
private:
int serial;
int static num;
int deg;
float min;
char direction;
public:
ship()
{

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;

Object Oriented Programming Page 8


Lab Journal – Lab 3

cout << "Enter Location Of longitude Ship 1 " << endl;


a.getvalues();
cout << "Enter Location Of longitude Ship 2 " << endl;
b.getvalues();
cout << "Enter Location Of longitude Ship 3 " << endl;
c.getvalues();

cout << "Enter Location Of latitude Ship 1 " << endl;


e.getvalues();
cout << "Enter Location Of latitude Ship 2 " << endl;
f.getvalues();
cout << "Enter Location Of latitude Ship 3 " << endl;
g.getvalues();

a.display();
e.display1();
b.display();
f.display1();
c.display();
g.display1();
}

+++++++++++++++++++++++++

Object Oriented Programming Page 9

You might also like