Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Date: _____________
Object-Oriented
Programming
Techniques (Lab)
Lab Task #
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);
}
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
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()
{
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:-
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;
}
void showadd();
void showsub();
void showmul();
};
Complex Complex::Add(Complex& b)
{
return Complex(real+b.real,imag+b.imag);
}
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();
system("pause");
}
OUTPUT:-
QUES:-04
CODE:-
/* NAME:- M.Hammad Chaudhry
REG NO:- 1812188
*/
#include<iostream>
class Distance
{
private:
int feet;
float inches;
public:
/
Distance() : feet(0), inches(0.0)
{}
void getdist()
{
cout<<"\nEnter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
}
inline void showdist(); //display distance
inches -= 12.0;
feet++;
} //by 1
feet += d2.feet + d3.feet; //add the feet
}
int main()
{
Distance dist1, dist3;
Distance dist2(11, 6.25);
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
{
private:
int feet;
float inches;
public:
Distance() :
feet(0), inches(0.0)
{
{
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;
}
{
inches = d2.inches - d3.inches; //add the inches
feet = 0;
inches -= 12.0;
feet++;
}
}
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<<"\ndist1 = ";
dist1.showdist();
cout<<"\ndist2 = ";
dist2.showdist();
cout<<"\ndist3 = ";
dist3.showdist();
cout<<endl;
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:-