Sei sulla pagina 1di 14

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: _____________

Obtained Marks: _____________

Date: _____________

Object-Oriented
Programming
Techniques (Lab)
Lab Task #

Submitted To: Hafiz Syed Ahmed Qasim


__________________________________________________________________________

Student Name: M.Hammad Chaudhry


__________________________________________________________________________

Reg. Number: 1812188


___________________________________________________________________________

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

QUES:-01
CODE:-
/* NAME:- M.Hammad Chaudhry
REG NO:- 1812188
*/
#include <iostream>
using namespace std;

class Complex
{
private:
double real, imag;
public:
Complex()
{
real=0.0;
imag=0.0;
}
Complex(double r,double im)
{
real=r;
imag=im;
}

Complex Add(Complex& b)
{
return Complex(real+b.real,imag+b.imag);
}
void showadd()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
Complex Sub(Complex& b)
{
return Complex(real-b.real,imag-b.imag);
}

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

void showsub()
{
cout<<real<<"-"<<imag<<"i"<<endl;
}
Complex Mul(Complex& b)
{
return Complex(real*b.real,imag*b.imag);
}
void showmul()
{
cout<<real<<"*"<<imag<<"i"<<endl;
}
};

int main()
{
Complex C1(12,5.5);
Complex C2(5,2.1);
Complex C3;
C3=C1.Add(C2);
C3.showadd();
C3=C1.Sub(C2);
C3.showsub();
C3=C1.Mul(C2);
C3.showmul();
system("pause");
}
OUTPUT:-

QUES:02
CODE:-
/* NAME:- M.Hammad Chaudhry

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

REG NO:- 1812188


*/
#include <iostream>
using namespace std;

class Complex
{
private:
double real, imag;
public:
Complex()
{
real=0.0;
imag=0.0;
}
Complex(double r,double im)
{
real=r;
imag=im;
}

Complex Add(Complex& b)
{
return Complex(real+b.real,imag+b.imag);
}
void showadd();
Complex Sub(Complex& b)
{
return Complex(real-b.real,imag-b.imag);
}
void showsub();
Complex Mul(Complex& b)
{
return Complex(real*b.real,imag*b.imag);
}
void showmul();
};
inline void Complex::showadd()

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

{
cout<<real<<"+"<<imag<<"i"<<endl;
}
inline void Complex::showsub()
{
cout<<real<<"-"<<imag<<"i"<<endl;
}
inline void Complex::showmul()
{
cout<<real<<"*"<<imag<<"i"<<endl;
}

int main()
{
Complex C1(12,5.5);
Complex C2(5,2.1);
Complex C3;
C3=C1.Add(C2);
C3.showadd();
C3=C1.Sub(C2);
C3.showsub();
C3=C1.Mul(C2);
C3.showmul();
system("pause");
}

OUTPUT:-

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

QUES:-03
CODE:-
/* NAME:- M.Hammad Chaudhry
REG NO:- 1812188
*/
#include <iostream>
using namespace std;

class Complex
{
private:
double real, imag;
public:
Complex()
{
real=0.0;
imag=0.0;
}
Complex(double r,double im)
{
real=r;
imag=im;
}

Complex Add(Complex& b);

void showadd();

Complex Sub(Complex& b);

void showsub();

Complex Mul(Complex& b);

void showmul();
};
Complex Complex::Add(Complex& b)

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

{
return Complex(real+b.real,imag+b.imag);
}

inline void Complex::showadd()


{
cout<<real<<"+"<<imag<<"i"<<endl;
}
Complex Complex::Sub(Complex& b)
{
return Complex(real-b.real,imag-b.imag);
}

inline void Complex::showsub()


{
cout<<real<<"-"<<imag<<"i"<<endl;
}

Complex Complex::Mul(Complex& b)
{
return Complex(real*b.real,imag*b.imag);
}
inline void Complex::showmul()
{
cout<<real<<"*"<<imag<<"i"<<endl;
}

int main()
{
Complex C1(12,5.5);
Complex C2(5,2.1);
Complex C3;
C3=C1.Add(C2);
C3.showadd();
C3=C1.Sub(C2);
C3.showsub();
C3=C1.Mul(C2);
C3.showmul();

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

system("pause");
}
OUTPUT:-

QUES:-04
CODE:-
/* NAME:- M.Hammad Chaudhry
REG NO:- 1812188
*/

#include<iostream>

using namespace std;

class Distance
{
private:
int feet;
float inches;
public:
/
Distance() : feet(0), inches(0.0)
{}

Distance(int ft, float in) : feet(ft), inches(in)


{}

void getdist()

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

{
cout<<"\nEnter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
}
inline void showdist(); //display distance

void add_dist( Distance, Distance ); //declaration


};

inline void Distance::showdist()


{
cout<<feet<<"\""<<inches<<"\'";
}

void Distance::add_dist(Distance d2, Distance d3)


{
inches = d2.inches + d3.inches; //add theinches
feet = 0; //(for possible carry)
if(inches >= 12.0)
{

inches -= 12.0;
feet++;
} //by 1
feet += d2.feet + d3.feet; //add the feet
}

int main()
{
Distance dist1, dist3;
Distance dist2(11, 6.25);

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

dist1.getdist();
dist3.add_dist(dist1, dist2); //dist3 = dist1 + dist2

cout<<"\ndist1 = ";
dist1.showdist();
cout<<"\ndist2 = ";
dist2.showdist();
cout<<"\ndist3 = ";
dist3.showdist();
cout<<endl;
system("pause");
}
OUTPUT:-

QUES:-05
CODE:-
/* NAME:- M.Hammad Chaudhry
REG NO:- 1812188
*/

#include<iostream>
using namespace std;
class Distance

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

{
private:
int feet;
float inches;
public:
Distance() :
feet(0), inches(0.0)
{

Distance(int ft, float in) : feet(ft), inches(in)


{}
void getdist()
{
cout<<"\nEnter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
}
inline void showdist(); //display distance

void add_dist( Distance, Distance );


void sub_dist( Distance, Distance );
void mul_dist( Distance, Distance ); //declaration
};

inline void Distance::showdist()


{
cout<<feet<<"\""<<inches<<"\'";
}

void Distance::add_dist(Distance d2, Distance d3)

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

{
inches = d2.inches + d3.inches;
feet = 0;
if(inches >= 12.0)
{
inches -= 12.0; //by 12.0 and
feet++; //increase feet
}
feet += d2.feet + d3.feet;
}

void Distance::sub_dist(Distance d2, Distance d3)

{
inches = d2.inches - d3.inches; //add the inches
feet = 0;

if(inches >= 12.0)


{

inches -= 12.0;
feet++;
}

feet -= d2.feet - d3.feet;


}
void Distance::mul_dist(Distance d2, Distance d3)
{
inches = d2.inches * d3.inches; //add the inches
feet = 0;
if(inches >= 12.0)
{
inches -= 12.0;
feet++;

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

}
feet *= d2.feet * d3.feet;
}

int main()
{
Distance dist1, dist3;
Distance dist2(11, 6.25);

dist1.getdist();
dist3.add_dist(dist1, dist2);
cout<<"\ndist1 = ";
dist1.showdist();
cout<<"\ndist2 = ";
dist2.showdist();
cout<<"\ndist3 = ";
dist3.showdist();
cout<<endl;

cout<<" for subtraction :"<<endl;


dist1.getdist();
dist3.sub_dist(dist1, dist2);

cout<<"\ndist1 = ";
dist1.showdist();
cout<<"\ndist2 = ";
dist2.showdist();
cout<<"\ndist3 = ";
dist3.showdist();
cout<<endl;

cout<<"for multiplication :"<<endl;

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

dist1.getdist();
dist3.mul_dist(dist1, dist2); //dist3 = dist1 + dist2
//display all lengths
cout<<"\ndist1 = ";
dist1.showdist();
cout<<"\ndist2 = ";
dist2.showdist();
cout<<"\ndist3 = ";
dist3.showdist();
cout<<endl;
system("pause");
}
OUTPUT:-

Object-Oriented Programming Techniques (Lab) BSCS-2A SZABIST-ISB

Potrebbero piacerti anche