C++ QUESTION PAPER 2022 (Oct )
C++ QUESTION PAPER 2022 (Oct )
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double len, double wid) {
length = len;
width = wid;
}
double calculateArea() {
return length * width;
}
};
int main() {
double length, width;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
int main() {
int number;
if (number <= 0) {
cerr << "Please enter a positive integer." << endl;
return 1; // Exit with an error code
}
displayFactors(number);
return 0;
}
Q3) Answer the following (any five)
e) Write a program to create a class student having roll no, name and
percentage. Write a member function to accept and display details of
students (use Array of objects)
Ans :- #include <iostream>
#include <iomanip>
class Student {
private:
int rollNo;
string name;
float percentage;
public:
void acceptDetails() {
cout << "Enter Roll Number: ";
cin >> rollNo;
cin.ignore();
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Percentage: ";
cin >> percentage;
}
int main() {
const int numStudents = 3;
Student students[numStudents];
return 0;
}
f) What is Manipulator? Explain syntax and use of any three.
Ans :- a manipulator is an object or function that modifies the
behavior of the input or output stream. Manipulators are part of the
<iomanip> header and are used to format or control the display of
data on the console.
Example :- #include <iostream>
#include <iomanip>
Using namespace std;
int main() {
double pi = 3.1415926535;
cout << fixed << pi << endl; // Outputs: "3.141593"
return 0;
}
g) What are the various file operations performed write a program to read
the contents from the text file.
Ans :- #include <iostream>
#include <fstream>
#include <string>
int main() {
ifstream inputFile; // Input file stream
inputFile.open("example.txt");
if (!inputFile.is_open()) {
cerr << "Unable to open the file." << endl;
return 1;
}
string line;
while (getline(inputFile, line)) {
cout << line << endl;
}
inputFile.close();
return 0;
}
int main() {
D obj;
obj.display();
return 0;
}
b) What is function overloading? Write a program to overload function
volume to calculate volume of cube and cylinder
Ans :-
#include <iostream>
#include <cmath>
int main() {
double cubeSide = 4.5;
cout << "Volume of Cube: " << volume(cubeSide) << endl;
return 0;
}
b) Read the code and answer the questions.
Ans :-
i) The object-oriented feature illustrated here is inheritance.
ii) This explicitly calls the display function from class A on the object
b of class B.
iii) b.display();
iv) iv) Data members x and y are declared in the private section
class Distance {
private:
int feet;
public:
Distance(int ft) {
feet = ft;
}
void display() {
std::cout << "Feet: " << feet << std::endl;
}
};
int main() {
int feetValue = 5;
Distance distanceObj = feetValue;
distanceObj.display();
return 0;
}
f) Write the significance of the following.
Ans :-
i) New Operator:
The new operator in C++ is used for dynamic memory allocation during
runtime.
It allocates memory for a variable or an object on the heap and returns
a pointer to the allocated memory.
These operators are used for formatted input and output in C++.
The << operator is used for output (insertion) to the stream, and >> is
used for input (extraction) from the stream.
g) Find output and justify
Ans :- 1) First code justification :-
Justification:
===============End================