PT 1 QBank OOP Ans
PT 1 QBank OOP Ans
PT 1 QBank OOP Ans
Chapter1
2. setprecision
The setprecision manipulator sets the number of decimal places to display for floating-point
numbers.
Eg.setprecision(2) sets the number of decimal places to 2.
C++ a defines unary operators such as new and delete to perform the tasks, i.e., allocating and
freeing the memory.
•New operator
A new operator is used to create the object while a delete operator is used to delete the object.
When the object is created by using the new operator, then the object will exist until we explicitly
use the delete operator to delete the object. Therefore, we can say that the lifetime of the object is
not related to the block structure of the program
pointer_variable = new data-type
The above syntax is used to create the object using the new operator.
1
In the above syntax, 'pointer_variable' is the name of the pointer variable, 'new' is the operator, and
'data-type' defines the type of the data.
Example 1:
int *p;
p = new int;
In the above example, 'p' is a pointer of type int.
• Delete operator
When memory is no longer required, then it needs to be deallocated so that the memory can be
used for another purpose. This can be achieved by using the delete operator, as shown below:
delete pointer_variable;
In the above statement, 'delete' is the operator used to delete the existing object, and
'pointer_variable' is the name of the pointer variable.
In the previous case, we have created pointer 'p' by using the new operator, and can be deleted by
using the following statements:
delete p;
2
7)Define class and object. (2 m)
Ans
Class:Class is a user defined data type that combines data and functions together. It is a
collection of objects of similar type.
Object: Object is a basic run time entity that represents a person, place or any item that the program
has to handle.
3
cin>>name;
cout<<"\nMarks:";
cin>>marks;
}
void student::display()
{
cout<<"\nStudents data is:";
cout<<"\nRoll number:"<<rollno;
cout<<"\nName:"<<name;
cout<<"\nMarks:"<<marks;
}
int main()
{
student S;
S.accept();
S.display();
}
2. Data Abstraction
Data abstraction refers to the concept of hiding the implementation details of a class and exposing
only the necessary interfaces to the user. In C++, this is often achieved using abstract classes or
interfaces.
3. Data Encapsulation
Data encapsulation is the concept of wrapping data (variables) and methods (functions) that operate
on the data into a single unit or class. It also involves restricting access to some of the object's
components, which is often achieved using access specifiers like private, protected, and public.
4
12)Write a program to declare a class ‘student’ having data members as ‘stud_name’ and
‘roll_no’. Accept and display this data for 5 students. (4 m)
Ans
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string stud_name;
int roll_no;
public:
void setData(string name, int roll) {
stud_name = name;
roll_no = roll;
}
void display() {
cout << "Name: " << stud_name << ", Roll No: " << roll_no << endl;
}
};
int main() {
Student students[5];
string name;
int roll_no;
return 0;
}
5
13)Explain the structure of C++ program. (4 m)
Ans
General C++ program has following structure.
Description:-
1. Include header files
In this section a programmer include all header files which are require to execute given program. The
most important file is iostream.h header file. This file defines most of the C++statements like cout
and cin. Without this file one cannot load C++ program.
2. Class Declaration
In this section a programmer declares all classes which are necessary for given program. The
programmer uses general syntax of creating class.
3. Member Functions Definition
This section allows programmer to design member functions of a class. The programmer can have
inside declaration of a function or outside declaration of a function.
4. Main Function Program
In this section programmer creates objects and calls various functions writer within various class.
14)Write a C++ program to declare a class book with members as book_id and name. Accept
and display data of five books. (4 m)
Ans
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
int book_id;
string name;
public:
void setData(int id, string bookName) {
book_id = id;
name = bookName;
}
int main() {
Book books[5]; // Array of 5 Book objects
int id;
string bookName;
6
cin >> id;
cin.ignore(); // To ignore the newline character left in the input buffer
cout << "Enter name for book " << i + 1 << ": ";
getline(cin, bookName);
books[i].setData(id, bookName);
}
return 0;
}
Chapter2
15)Define destructor. (2 m)
Ans
In C++, a destructor is a special member function of a class that is automatically invoked when an
object of the class is destroyed. The destructor is used to perform cleanup tasks, such as releasing
memory, closing files, or other resources that the object may have acquired during its lifetime.
2)No Parameters and No Return Type: Destructors do not take any parameters, and they do not
return any value.
3)Automatic Invocation: The destructor is called automatically when an object goes out of scope or
is explicitly deleted using the delete keyword.
4)One Per Class: A class can have only one destructor, and it cannot be overloaded.
Syntax
static DataType variableName = initialValue;
Example
static int count;
7
1. Default constructor
2. Parameterized constructor
3. Copy constructor
4. Constructor with default argument
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
int book_id;
string name;
public:
// Constructor to initialize the book details
Book() : book_id(0), name("Unknown") {} // Default constructor
int main() {
// Declaring an array of 3 Book objects
Book books[3];
8
books[0].setData(101, "C++ Programming");
books[1].setData(102, "Data Structures");
books[2].setData(103, "Algorithms");
return 0;
}
2. No Return Type
Description: Constructors do not have a return type, not even void. This is because constructors
are automatically invoked when an object of the class is created, and they are not meant to return
any value.
3. Automatic Invocation
Description: Constructors are called automatically when an object of the class is created. The
constructor is invoked as soon as an object is instantiated, without needing to explicitly call it.
4. Can Be Overloaded
Description: Constructors can be overloaded, meaning a class can have multiple constructors with
different sets of parameters. This allows objects to be initialized in different ways depending on the
constructor used.
class Rectangle {
private:
int length;
int width;
public:
// Parameterized constructor
Rectangle(int l, int w) {
length = l;
width = w;
}
9
return length * width;
}
int main() {
// Creating an object of Rectangle using the parameterized constructor
Rectangle rect1(10, 5);
return 0;
}
22)Write a C++ program to find greatest number among two numbers from two different
classes using friend function. (4 m)
Ans
#include <iostream>
using namespace std;
class ClassA {
private:
int numA;
public:
// Constructor to initialize numA
ClassA(int a) : numA(a) {}
class ClassB {
private:
int numB;
public:
// Constructor to initialize numB
ClassB(int b) : numB(b) {}
10
// Friend function definition
int findGreatest(ClassA a, ClassB b) {
if (a.numA > b.numB)
return a.numA;
else
return b.numB;
}
int main() {
// Create objects of ClassA and ClassB
ClassA objA(15); // Object of ClassA with numA = 15
ClassB objB(20); // Object of ClassB with numB = 20
return 0;
}
11
Chapter 3
protected
Members declared as protected are accessible within the class itself and by derived classes
(subclasses) but not from outside the class.
private
Members declared as private are accessible only within the class itself and not from outside the
class or by derived classes.
12
2) Multiple inheritance: In multiple inheritance, derived class is derived from
more than one base classes.
Diagram:
13
5) Hybrid inheritance: Hybrid inheritance is a combination of single, multiple,
multilevel and hierarchical inheritance.
Diagram:
14