1 C++Answer
1 C++Answer
***********
Ques 1: Write a program to find the area of rectangle using class concept.
#include <iostream>
using namespace std;
class rectangle{
int length;
int breadth;
public:
void setData(){
cout<<"Enter height of rectangle : ";
cin>>length;
cout<<"Enter width of rectangle : ";
cin>>breadth;
}
void getArea(){
int area=(length*breadth);
cout<<"Area of rectangle : "<<area<<endl;
}
};
main()
{
rectangle r;
r.setData();
r.getArea();
return 0;
}
*********************************************************************************************
**********
Ques 2: Write a program to find the sum of two complex numbers using friend function.
#include <iostream>
class complex
{
public:
void setData()
{
cout << "enter real and imag part : ";
cin >> real >> imag;
}
void display()
{
cout << "the sum of complex num is : " << real << "+i" << imag;
}
};
complex t;
return t;
}
int main()
{
complex a, b, c;
a.setData();
b.setData();
c = sum(a, b);
c.display();
return (0);
}
*********************************************************************************************
************************
Ques 3: Write a program to find the maximum value of data member among two different classes using friend functi
on.
#include <iostream>
using namespace std;
class two;
class one
{
int x;
public:
void setData()
{
cout << "Enter value of x : ";
cin >> x;
}
void display()
{
cout << "value of x : " << x;
}
};
class two
{
int x;
public:
void setData()
{
cout << "Enter value of x : ";
cin >> x;
}
void display()
{
cout << "value of x : " << x;
}
};
int main()
{
one a;
a.setData();
two b;
b.setData();
compareData(a,b);
return 0;
}
*********************************************************************************************
***************
Ques 4 : Write a program to find the factorial of a number using class and recursion concept.
#include <iostream>
using namespace std;
class fact
{
public:
int factorial(long long int n)
{
if (n == 1)
return 1;
int main()
{
fact f;
return 0;
}
*********************************************************************************************
*******************
Ques 5 : Write a program to overload * (multiply) operator in c++ using using friend function.
#include <iostream>
using namespace std;
class Complex
{
float real;
float imag;
public:
void setData()
{
cout << "Enter real and imaginary parts : ";
cin >> real >> imag;
}
void display()
{
if (imag < 0)
cout << "Complex number: " << real << imag << "i";
else
cout << "Complex number : " << real << "*" << imag << "i";
}
};
int main()
{
Complex complex1, complex2, result;
return 0;
}
*********************************************************************************************
**************************
Ques 6: Write a program to find the root of quadratic equation using parameterized constructor.
#include <iostream>
#include <cmath>
using namespace std;
class quadraticEquation
{
int a, b, c;
int x, y;
int realPart, imaginaryPart;
int discriminant;
public:
quadraticEquation(int x, int y, int z, int d, int r, int i)
{
a = x;
b = y;
c = z;
d = discriminant;
r = realPart;
i = imaginaryPart;
void findQuadraticEquation()
{
if (discriminant > 0)
{
x = (-b + sqrt(discriminant)) / (2 * a);
y = (-b - sqrt(discriminant)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x << endl;
cout << "x2 = " << y << endl;
}
else if (discriminant == 0)
{
cout << "Roots are real and same." << endl;
x = -b / (2 * a);
cout << "x1 = x2 =" << x << endl;
}
else
{
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
}
};
int main()
{
q.findQuadraticEquation();
return 0;
}
*********************************************************************************************
*********************
Ques 7: Write a program to count total characters present in "LPU.DAT" file using file handling concept.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string str;
cout << "Enter a string : ";
getline(cin, str);
ofstream fout;
fout.open("LPU.DAT");
fout << str << endl;
int x = fout.tellp();
fout.close();
return 0;
}
*********************************************************************************************
************************
Ques 8: Write a program to find the velocity of a car if distance and time is given and if time is zero then program
must raise one exception.
#include <iostream>
using namespace std;
int main()
{
int distance;
int time;
int v;
try
{
v = velocity(distance, time);
catch (int e)
{
cout << "You have entered " << e << endl;
cout << "Enter any value except 0" << endl;
}
return 0;
}
*********************************************************************************************
*****************************
9th Question is not clearly visible.
*********************************************************************************************
*****************************
Ques 10: Write a Program to find the sum of two integers as well as two float values using the function template.
#include <iostream>
using namespace std;
void Sum(t1 a, t1 b, t2 c, t2 d)
{
t2 s;
s = (a + b + c + d);
cout << "Sum is " << s << endl;
}
int main()
{
return 0;
}
Thank you................