Uploaded by official.hp007

Prabhat Singh CP ASSIGNMENT 5

advertisement
COMPUTER PROGRAMMING
Prabhat Singh Chauhan(FIRST YEAR ITIOT)
08 February 2023
SUBMITTED TO – PROF. SANJIV SHARMA
QUESTION 1- Give an Illustration of copy Constructor, parameterized constructor
and Dynamic constructor
in class.
ANSWER–
A copy constructor is a member function that initializes an object using another
object of the same class. In simple terms, a constructor which creates an object
by initializing it with an object of the same class, which has been created
previously is known as a copy constructor.
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point(int x1, int y1)
{
x = x1;
y = y1;
}
// Copy constructor
Point(const Point& p1)
{
x = p1.x;
y = p1.y;
}
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
// Let us access values assigned by constructors
cout << "p1.x = " << p1.getX()
<< ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX()
<< ", p2.y = " << p2.getY();
return 0;
}
Parameterized Constructors: It is possible to pass arguments to constructors.
Typically, these arguments help initialize an object when it is created. To
create a parameterized constructor, simply add parameters to it the way you would
to any other function.
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX() { return x; }
int getY() { return y; }
};
int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX()
<< ", p1.y = " << p1.getY();
return 0;
}
Default constructor is the constructor which doesn’t take any argument. It has
no parameters. It is also called a zero-argument constructor.
#include <iostream>
using namespace std;
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl << "b: " << c.b;
return 1;
}
QUESTION 2- Write a C++ program to create a class called COMPLEX and accept 2
complex numbers, add them and display their output. Illustrate the passing of
Objects as parameters in the program.
ANSWER –
#include<iostream>
using namespace std;
class complex
{
int re,im;
public:
void get()
{
cout<<"\nEnter Real Part :: ";
cin>>re;
cout<<"\nEnter Imag. Part :: ";
cin>>im;
}
void disp()
{
cout<<re<<"+"<<im<<"i"<<"\n";
}
void sum(complex,complex);
};
void complex::sum(complex c1,complex c2)
{
re=c1.re+c2.re;
im=c1.im+c2.im;
}
int main()
{
complex c1,c2,c3;
cout<<"Enter 1st complex no.: \n";
c1.get();
cout<<"\nEnter 2nd complex no.: \n";
c2.get();
cout<<"\nThe 1st complex no. is :: ";
c1.disp();
cout<<"\nThe 2nd complex no. is :: ";
c2.disp();
c3.sum(c1,c2);
cout<<"\n The Sum of two complex no.s are :: ";
c3.disp();
return 0;
}
Output 
Enter 1st complex no.:
Enter Real Part :: 1
Enter Imag. Part :: 2
Enter 2nd complex no.:
Enter Real Part :: 3
Enter Imag. Part :: 4
The 1st complex no. is :: 1+2i
The 2nd complex no. is :: 3+4i
The Sum of two complex no.s are :: 4+6i
Process returned 0
QUESTION 3- Create a class DISTANCE. Create D1,D2 and D3 as objects of DISTANCE.
Write a C++ program to read the distance D1 and D2 in the form as kms and mts
where kms indicates kilometers and mts indicates meters. Assume both kms and mts
to be integer only. Add then and store result in DISTANCE D3. Finaly print the
value of DISTANCE D3.
ANSWER –
#include <iostream>
using namespace std;
class Distance
{
private:
int kms;
int mts;
public:
Distance (); //Constructor
void getDist ();
void showDist ();
Distance addDist( Distance d2 );
Distance subDist( Distance d2 );
};
Distance:: Distance ()
{
kms = 0; mts = 0;
}
void Distance:: getDist()
{
cout << "Enter Value of kms : "; cin >> kms;
cout << "Enter value of mts : "; cin >> mts;
mts = (mts >= 12) ? 12 : mts;
}
void Distance:: showDist()
{
cout << endl << "\tKms : " << kms;
cout << endl << "\tMts: " << mts;
}
Distance Distance:: addDist( Distance d2 )
{
Distance temp;
temp.kms = kms + d2.kms;
temp.mts = mts + d2.mts;
if( temp.mts >= 12)
{
temp.kms++;
temp.mts -= 12;
}
return temp;
}
Distance Distance:: subDist( Distance d2 )
{
Distance temp;
temp.kms = kms - d2.kms;
temp.mts = mts - d2.mts;
if( temp.mts < 0 )
{
temp.kms--;
temp.mts = 12 + temp.mts;
}
return temp;
}
int main()
{
Distance d1;
Distance d2;
Distance d3;
cout << "Enter Distance1 : " << endl;
d1.getDist();
cout << "Enter Distance2 : " << endl;
d2.getDist();
d3 = d1.addDist(d2);
cout << endl << "Distance1 : " ;
d1.showDist();
cout << endl << "Distance2 : " ;
d2.showDist();
cout << endl << "Distance3 : " ;
d3.showDist();
cout << endl;
return 0;
}
QUESTION 4- Write a C++ program to add and multiply two complex number using
constructors
ANSWER –
#include <iostream>
using namespace std;
class Complex
{
public: int real, imaginary;
Complex(int tempReal = 0, int tempImaginary = 0)
{
real = tempReal;
imaginary = tempImaginary;
}
Complex addComp(Complex C1, Complex C2)
{
Complex temp;
temp.real = C1.real + C2.real;
temp.imaginary = (C1.imaginary + C2.imaginary);
return temp;
}
};
int main()
{
Complex C1(3, 2);
cout << "Complex number 1 : " <<
C1.real << " + i" <<
C1.imaginary << endl;
Complex C2(9, 5);
cout << "Complex number 2 : " <<
C2.real << " + i" <<
C2.imaginary << endl;
Complex C3;
C3 = C3.addComp(C1, C2);
cout << "Sum of complex number : " <<
C3.real << " + i" <<
C3.imaginary;
}
#include <iostream>
using namespace std;
void multiply(Complex c1,Complex c2)
{
int x,y;
x=c1.real*c2.real-c1.img*c2.img;
y=c1.real*c2.img+c1.img*c2.real;
cout<<"\n("<<c1.real<<"+"<<c1.img<<"i)*("<<c2.real<<"+"<<c2.img<<"i)=("<<x<<"+"
<<y<<"i)";
}
Complex operator ++()
{
Complex x;
x.real=++real;
x.img=++img;
return x;
}
};
void main ()
{
clrscr();
Complex a,b,c,d,e;
cout<<"\nEnter real and imaginary part of first complex number:";
cin>>a.real>>a.img;
cout<<"\nEnter real and imaginary part of second complex number:";
cin>>b.real>>b.img;
c.add(a,b);
d.multiply(a,b);
cout<<"\n++("<<a.real<<"+"<<a.img<<"i)=(";
++a;
cout<<a.real<<"+"<<a.img<<"i)";
getch();
}
QUESTION 5- Create a class TIME. Create T1, T2 and T3 as objects of TIME. Write
a C++ program to read time T1 and T2 as hh, mm and ss. Where hh indicates
hours, mm indicates minutes and ss indicate seconds. Assume hh, mm and ss all
to be integers. Add these times T1 and T2 and store the result in time T3.Print
the value of T3.
ANSWER –
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
void getTime(void);
void putTime(void);
void addTime(Time T1,Time T2);
};
void Time::getTime(void)
{
cout << "Enter time:"
cout << "Hours? ";
cout << "Minutes? ";
cout << "Seconds? ";
}
<< endl;
cin>>hours;
cin>>minutes;
cin>>seconds;
void Time::putTime(void)
{
cout << endl;
cout << "Time after add: ";
cout << hours << ":" << minutes << ":" << seconds << endl;
}
void Time::addTime(Time T1,Time T2)
{
this->seconds=T1.seconds+T2.seconds;
this->minutes=T1.minutes+T2.minutes + this->seconds/60;;
this->hours= T1.hours+T2.hours + (this->minutes/60);
this->minutes %=60;
this->seconds %=60;
}
int main()
{
Time T1,T2,T3;
T1.getTime();
T2.getTime();
T3.addTime(T1,T2);
T3.putTime();
return 0;
}
Download