Four features of OOP- The main features of object-oriented programming are
encapsulation, inheritance, polymorphism, and abstraction. Encapsulation binds data and
functions together, inheritance allows code reuse, polymorphism enables the same function
to work differently in different contexts, and abstraction hides unnecessary details from the
user.
Pure virtual function- A pure virtual function is a function in a base class that has no
definition and is declared using = 0. It must be overridden by derived classes. Classes
containing pure virtual functions become abstract classes and cannot be instantiated
directly.
Cascading of I/O operators- Cascading occurs when multiple input or output operations
are performed in a single statement using the >> or << operators consecutively. This allows
chaining multiple inputs or outputs, making the code shorter and more readable.
Ways to define a constant- Constants can be defined using the const keyword, #define
preprocessor directive, enum, or constexpr. Using constants makes the program safer and
easier to maintain by preventing accidental changes to fixed values.
Abstract class and encapsulation- An abstract class contains at least one pure virtual
function and cannot be instantiated. Encapsulation is the concept of binding data and
functions together while restricting direct access to the data. It protects data and maintains
the integrity of the object.
Multiple inheritance- Multiple inheritance occurs when a derived class inherits from more
than one base class, allowing it to access properties and functions of all the base classes.
This enables code reuse and flexibility, but can lead to ambiguity if multiple base classes
have members with the same name.
Destructor and constructor- A constructor is a special function that initializes an object
automatically when it is created. A destructor is a function that releases resources or
performs cleanup when the object goes out of scope. Constructors can be parameterized,
while destructors do not take arguments.
‘This’ pointer- The this pointer is an implicit pointer available in all non-static member
functions that points to the current object. It is used to access members of the object,
distinguish between local and class variables, and return the object itself from a function.
Runtime polymorphism- Runtime polymorphism occurs when the function to be called is
determined at runtime, usually using function overriding. It allows a base class pointer or
reference to call derived class functions dynamically, providing flexibility in behavior.
Static polymorphism- Static polymorphism occurs when the function to be executed is
determined at compile time, usually using function overloading or operator overloading. It
makes the code more readable and efficient, as the compiler decides which function to call.
Manipulators- Manipulators are used to format input and output in C++. Common
manipulators include endl to end a line, setw() to set field width, setprecision() for decimal
1
precision, fixed to display floating numbers in fixed-point, and left, right, showpoint to control
alignment and display format.
Visibility labels- Visibility labels like private, protected, and public control the accessibility of
class members. private members are accessible only within the class, protected members
are accessible in the class and its derived classes, and public members are accessible
anywhere
Extraction and insertion operators- The extraction operator >> is used to read input from
standard input (keyboard), while the insertion operator << is used to display output on the
screen. They allow streamlined input and output operations in C++.
Default argument in function- A default argument is a value given in a function declaration
that is automatically used if the corresponding argument is not provided during a function
call. It helps to simplify function calls and avoid overloading functions unnecessarily.
Uses of scope resolution operator (::)- The scope resolution operator is used to access
global variables when they are hidden by local variables and to define class member
functions outside the class. It ensures that the correct scope is referenced in the program.
Inline function- An inline function is a function whose code is expanded at the point of call
rather than performing a normal function call. This reduces function call overhead and
improves execution speed for small, frequently used functions.
Reference variable and its major use- A reference variable is an alias for an existing
variable. It allows a function to access the original variable directly, which is useful for
efficient argument passing without creating copies.
Compile-time polymorphism- Compile-time polymorphism occurs when the function to be
called is decided at compile time, achieved using function overloading or operator
overloading. It provides efficiency and readability in programs.
Access specifiers Access specifiers private, protected, and public control the accessibility
of class members. They ensure data security and determine how class members can be
accessed by other classes or functions.
Reference variable- A reference variable is an alternative name (alias) for another variable,
allowing direct access and modification of the original variable without creating a new copy.
Keyword: A keyword is a reserved word in C+ with a predefined meaning that cannot be
used as an identifier, such as int, return, class, or if. Keywords define the syntax and
structure of the language.
Memory management operator- Memory management operators new and delete are used
to dynamically allocate and deallocate memory at runtime. They help in efficient memory
usage and avoid memory leaks.
2
width() and fill(): The width(n) function sets the field width for the next output, while the
fill(c) function specifies the character used to fill extra spaces when the output is shorter than
the field width. They help format console output neatly.
Functions of overloading with eg- Function overloading is when two or more functions
have the same name but different parameters (number or type). The compiler decides which
function to call based on arguments. Eg int add(int a, int b); int add(int a, int b, int c); Both
functions have the same name add, but the number of parameters is different.
Operators of overloading- allows redefining the behavior of operators for user-defined data
types, so they can work with objects of a class just like built-in types. It is done by writing a
function using the operator keyword followed by the operator symbol, eg operator+ to add
two objects. Operator overloading improves code readability and makes expressions
involving objects more intuitive and natural. It should be used carefully to maintain clarity and
avoid confusion
Inheritance Types of inheritance- Inheritance is an OOP concept where a class derived
class acquires properties and methods of another class (base class). It promotes code
reusability. Types of Inheritance 1 Single Inheritance: Derived class inherits from one base
class 2 Multiple Inheritance: Derived class inherits from more than one base class 3
Multilevel Inheritance: A class is derived from a derived class 4 Hierarchical Inheritance:
Multiple classes inherit from a single base class 5 Hybrid Inheritance: Combination of two or
more types of inheritance
Inheritance explain hierarchical inheritance-
Inheritance is an object-oriented programming concept where a derived class inherits
properties and methods from a base class allowing code reuse and reducing redundancy
Hierarchical inheritance is a type of inheritance in which multiple derived classes inherit from
a single base class Each derived class can access the members of the base class
independently & can also have its own additional members This allows common features to
be defined once in the base class while letting derived classes extend or customize
functionality as needed
Static data members & static member function- Static Data Members: Shared by all
objects of a class. Static Member Functions: Can access only static data members; called
using class name. Eg class Example {
static int count;
public:
static void show() { cout << count; }
}; int Example::count = 5;
Example::show(); // Output: 5
Memory allocation for object with non static data members and static data members-
For a class object non-static data members are allocated separate memory for each object
so every object has its own copy In contrast static data members are shared among all
objects of the class and are allocated memory only once at the class level not with each
object This allows static members to retain their value across all objects while non-static
members store data unique to each object
3
Memory management operator with help of suitable eg- Memory management operators
in C++ are used to dynamically allocate and deallocate memory at runtime. The new
operator allocates memory for a variable or an object from the heap and returns its address
while the delete operator frees the previously allocated memory to prevent memory leaks.
Dynamic memory is useful when the required size is not known during compile time
int *ptr = new int;
*ptr = 10;
delete ptr;
Friend function & its characteristics
A friend function is a function that is not a member of a class but can access its private and
protected members It is declared inside the class using the friend keyword and is called like
a normal function, without using any object Friend functions are useful when multiple classes
need access to each other’s data They are not members of the class, can access private
and protected members, are declared with friend & while they increase flexibility they
partially break encapsulation
Use of any four file opening modes- files can be opened in different modes depending on
the operation required The ios::in mode opens a file for reading, ios::out opens a file for
writing (creates the file if it does not exist) ios::app opens a file for appending data at the
end, and ios::binary opens a file in binary mode for reading or writing non-text data Using the
correct mode ensures the program handles the file safely and efficiently
What is pure virtual function w eg- Pure Virtual Function:
A pure virtual function is a function in a base class that has no definition and must be
overridden by derived classes. It is declared by assigning 0 in the declaration. Classes
containing at least one pure virtual function become abstract classes and cannot be The
show() function in Base is pure virtual so Derived must provide its definition
Dynamic constructor with eg- A dynamic constructor is a constructor that allocates
memory at runtime using pointers and the new operator. It is used when the size of data or
objects is not known at compile time, allowing flexible memory management. Dynamic
constructors are often paired with destructors to free memory and avoid leaks.
Demo(int size) { ptr = new int[size]; } The constructor allocates memory dynamically
based on runtime input
Class virtual base class w eg- A class is made a virtual base class when we want to avoid
duplicate copies of the base class in a multiple-inheritance situation. This mostly happens in
hybrid or diamond inheritance, where two intermediate classes inherit from the same base
class, and a further derived class inherits from both intermediates. Using virtual inheritance
ensures that the derived class gets only one shared copy of the base class members,
preventing ambiguity.
class A { public: int x; }; class B : virtual public A { }; class C : virtual public A { };
class D : public B, public C { }; // Only one copy of A in D Exp Here classes B and C
virtually inherit A, so class D receives a single copy of A instead of two separate ones,
removing ambiguity.
4
Array of object- An array of objects is a collection where multiple objects of the same class
are stored in a single array, just like normal data types. It allows creating and managing
several objects together using a common name and index. When an array of objects is
created, the constructor (if defined) is called for each element, and every object gets its own
copy of the non-static data members. This is useful when handling a group of similar entities,
like students, employees, or items, using loops for input and output.
Formatted input output functions- Formatted I/O functions allow the programmer to
control how data is displayed and accepted The function setw() is used to set the width of
the output field. setprecision() controls the number of digits displayed after the decimal point.
setfill() fills empty output spaces with a specified character setw(), setprecision(), and setfill()
are part of the <iomanip> library. Similarly, endl is used to insert a new line and flush the
output buffer These functions help in presenting data neatly and in a structured manner
class Demo {
Constructor and Parameterized Constructor- A constructor is a special member function
of a class that is automatically called when an object is created. It has the same name as the
class and is used to initialize the data members of the object. A parameterized constructor is
a constructor that accepts one or more arguments allowing objects to be initialized with user-
defined values at the time of creation It helps in assigning different values to different objects
Template? with types- A template in C++ is a feature that allows writing generic classes
and functions so the same code can work with different data types Templates help in
achieving code reusability and type flexibility without rewriting the logic. There are two
types of templates 1. Function Template: Used to create a single function that can operate
on different data types by using a placeholder type 2. Class Template: Used to define a
class that works with different data types allowing objects to be created with any type
specified during declaration Templates are especially useful in data structures and
algorithms where the operations remain the same but data types vary
Explain polymorphism with its types-
Polymorphism is the ability of a function or operator to behave in different ways depending
on the context It allows the same name to perform different tasks improving flexibility and
code reusability There are two main types of polymorphism: 1 Compile-time
Polymorphism: Achieved through function overloading and operator overloading, where the
function call is resolved at compile time 2 Run-time Polymorphism: Achieved using virtual
functions where function calls are resolved at runtime based on the object being referred to
Polymorphism helps in writing cleaner extensible programs where the same operation can
work differently for different objects
Object as function argument- Object as Function Argument (4 marks):
In C++, an object can be passed to a function just like normal variables. This allows the
function to access the data members of the object and perform operations on them. Objects
may be passed by value, where a copy of the object is sent, or by reference, where the
function works on the original object. This concept is useful when we want to compare
objects, assign values, or transfer object data through functions the object obj is passed to
the function show(), allowing the function to use its data member.
5
pass the object as function argument exp w eg- Yes C++ allows class objects to be
passed as function arguments just like normal variables. Passing objects makes it possible
to access or process the data stored inside the object within a function. Objects can be
passed by value (a copy is sent) or by reference (the original object is used). This feature is
useful for comparing objects, displaying object data, or performing operations between two
objects the object obj is passed to the function display(). The function receives the object
and accesses its data member
classes used to perform console input output I/O operator- C++ uses a hierarchy of
stream classes to manage console I/O. The important classes are defined in <iostream> and
help handle input from the keyboard and output to the screen. The main classes are:
1. istream Used for input operations. It handles reading data from standard input. The object
cin is an instance of istream. 2. ostream Used for output operations. It sends data to
standard output. The object cout belongs to this class 3. iostream This class combines
features of both istream and ostream, supporting both input and output. It is used when a
single stream must perform read and write operations. 4. ios This is the base class for all I/O
stream classes. It stores common features like stream state flags, formatting options, and
buffer control that other stream classes inherited I/O Operators:
>> extraction operator (used with cin)
<< insertion operator (used with cout)
These operators work by overloading inside the stream classes to handle different data
types.
function overloading with example- Function overloading in C++ is a feature that allows
multiple functions to have the same name but different parameter lists. The compiler
identifies which function to run based on the number, type, or order of the arguments. It
helps achieve compile-time polymorphism and makes programs more readable
Features of C++- C++ is a powerful, general-purpose programming language that supports
both procedural and object-oriented programming. Its main features include: 1. Object-
Oriented Programming C++ supports core OOP concepts like classes, objects, inheritance,
polymorphism, encapsulation, and abstraction.
These help in creating modular, reusable, and organized code 2. Encapsulation & Data
Hiding Data and functions are bundled together inside classes. Access specifiers (private,
public, protected) prevent unauthorized access and protect data 3. Inheritance C++ allows
one class to derive properties from another, which supports code reuse and establishes
relationships between classes 4 Polymorphism C++ supports compile-time (function
overloading, operator overloading) and run-time (virtual functions) polymorphism, making
functions behave differently based on context 5. Low-Level Programming Features C++
supports pointers, direct memory manipulation, and system-level operations making it
suitable for performance-critical applications
Array of object- An array of objects is a collection of multiple objects of the same class
stored in continuous memory locations Instead of creating each object separately an array
allows you to create many objects using a single declaration Each object in the array can be
accessed using an index just like elements of a normal array This concept is useful when
handling large sets of similar data such as lists of students employees or products It
6
improves organization and makes operations like input, processing, and output easier and
more systematic
Access specifier- Access specifiers in C++ define how class members can be accessed
from outside the class. C++ provides three access specifiers: • Public members are
accessible from anywhere in the program • Private members can be accessed only within
the same class and are hidden from outside code • Protected members are accessible within
the class and its derived classes Access specifiers help in data encapsulation, ensuring
security, controlled access and proper object-oriented design
Constructor is derived class- A derived class constructor is used to initialize the data
members of the derived class and also to call the constructor of the base class. When an
object of a derived class is created the base class constructor is executed first followed by
the derived class constructor This ensures proper initialization of inherited members. If the
base class has a parameterized constructor the derived class must explicitly call it using the
initialization list This mechanism maintains correct construction order and supports
inheritance-based object creation
‘This’ pointer- this pointer is an implicit pointer passed to all non-static member functions of
a class. It points to the current object for which the member function is called and is used to
access the object’s members or return the object itself
Function overriding- Function overriding occurs when a derived class defines a function
with the same name and parameters as a function in its base class. The derived class
version replaces the base class version for objects of the derived class, enabling runtime
polymorphism
Exception handling- Exception handling in C++ is a mechanism to detect and handle
runtime errors so that the program does not terminate abruptly. Errors or exceptional
situations are thrown using the throw keyword, caught using catch blocks, and the code that
may generate exceptions is enclosed in a try block. This allows the program to handle errors
gracefully and continue execution safely
Operator overloading- Operator overloading in C++ allows redefining the behavior of
operators for user-defined data types, so objects of a class can be used with operators like
+, -, *, etc. It is done by creating a function using the operator keyword followed by the
operator symbol. Operator overloading makes operations on objects more intuitive and
improves code readability
Pointer to object- A pointer to an object is a pointer that stores the address of a class
object. It is used to access members of the object using the -> operator.
ClassName *ptr; ptr = &obj;
ptr->display(); Ptr points to obj, and the member function display() is accessed using ->.
Inline function- An inline function is a function whose code is expanded at the point of call
rather than being invoked normally. It is defined using the inline keyword and helps to reduce
function call overhead, improving program efficiency for small, frequently called functions.