0% found this document useful (0 votes)
7 views

1 C++Answer

The document contains 10 programming questions related to C++ concepts like classes, objects, functions, templates, file handling, and exceptions. Each question provides code snippets to implement the given task, such as calculating area of rectangle using classes, finding sum of complex numbers using friend functions, comparing data members of different classes using friend functions, finding factorial using recursion in classes, overloading multiplication operator for complex numbers class, solving quadratic equations using parameterized constructor, counting characters in a file, calculating velocity by handling exception if time is zero, and finding sum of integers and floats using function templates.

Uploaded by

Sukhpreet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

1 C++Answer

The document contains 10 programming questions related to C++ concepts like classes, objects, functions, templates, file handling, and exceptions. Each question provides code snippets to implement the given task, such as calculating area of rectangle using classes, finding sum of complex numbers using friend functions, comparing data members of different classes using friend functions, finding factorial using recursion in classes, overloading multiplication operator for complex numbers class, solving quadratic equations using parameterized constructor, counting characters in a file, calculating velocity by handling exception if time is zero, and finding sum of integers and floats using function templates.

Uploaded by

Sukhpreet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

*********************************************************************************************

***********
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>

using namespace std;

class complex
{

int real, imag;

public:
void setData()
{
cout << "enter real and imag part : ";
cin >> real >> imag;
}

friend complex sum(complex, complex);

void display()
{

cout << "the sum of complex num is : " << real << "+i" << imag;
}
};

complex sum(complex a, complex b)


{

complex t;

t.real = a.real + b.real;


t.imag = a.imag + b.imag;

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

friend void compareData(one, two);

void display()
{
cout << "value of x : " << x;
}
};

class two
{

int x;

public:
void setData()
{
cout << "Enter value of x : ";
cin >> x;
}

friend void compareData(one, two);

void display()
{
cout << "value of x : " << x;
}
};

void compareData(one a, two b)


{

if (a.x > b.x)


{
cout << "class one data member value is greater";
}
else
{
cout << "class two data member value is greater";
}
}

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;

return (n * factorial(n - 1));


}
};

int main()
{

long long int x;


cout << "Enter a number : ";
cin >> x;

fact f;

cout << "Factorial is : " << f.factorial(x);

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

Complex operator*(const Complex &obj)


{
Complex temp;
temp.real = real * obj.real;
temp.imag = imag * obj.imag;
return temp;
}

void display()
{
if (imag < 0)
cout << "Complex number: " << real << imag << "i";
else
cout << "Complex number : " << real << "*" << imag << "i";
}
};

int main()
{
Complex complex1, complex2, result;

cout << "Enter first complex number:\n";


complex1.setData();

cout << "Enter second complex number:\n";


complex2.setData();

result = complex1 * complex2;


result.display();

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

float a, b, c, discriminant, realPart, imaginaryPart;


cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
discriminant = b * b - 4 * a * c;

quadraticEquation q(a, b, c, discriminant, realPart, imaginaryPart);

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

cout << "Total character in the string : " << (x-2);

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 velocity(int distance, int time)


{
int v;
if (time == 0)
{
throw 0;
}
else
{
v = distance / time;
}
return v;
}

int main()
{

int distance;
int time;
int v;

cout << "Enter value of distance : ";


cin >> distance;

cout << "Enter value of time : ";


cin >> time;

try
{

v = velocity(distance, time);

cout << "Velocity of car : " << v << endl;


}

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;

template <class t1, class t2>

void Sum(t1 a, t1 b, t2 c, t2 d)
{
t2 s;
s = (a + b + c + d);
cout << "Sum is " << s << endl;
}

int main()
{

Sum(10, 20, 10.20f, 30.30f);

Sum(40, 50, 60.20f, 70.30f);

return 0;
}

Thank you................

You might also like