OOPs important questions
OOPs important questions
OOPs important questions
Key Features:
Purpose:
Ques 11. What is inline function? How they are different from other
function?
An inline function is a special type of function in C++ where the compiler
attempts to expand the function's code directly at the place of the function
call, instead of executing a normal function call. This eliminates the
overhead of function call mechanisms like stack operations and context
switching. Inline functions are declared using the inline keyword before
the function definition.
Syntax for inline function:
inline int square(int x) {
//function body;
}
Difference Between Inline Functions and Regular Functions:
• Execution:
o Inline Function: The function code is inserted directly at the
point of the function call, avoiding the overhead of a function
call.
o Regular Function: The function is executed via a standard
function call, involving stack operations and context switching.
• Overhead:
o Inline Function: No overhead of function calls (e.g., stack
usage), but can increase the program size if overused.
o Regular Function: Includes overhead due to function call
mechanisms like pushing and popping values on the stack.
• Program Size:
o Inline Function: Can increase program size if used
excessively, as the code is duplicated at every call site.
o Regular Function: No increase in program size, as the function
code exists only once in memory.
• Usage:
o Inline Function: Best for small, simple functions (e.g.,
getters/setters, small calculations).
o Regular Function: Suitable for both simple and complex
functions, especially those that involve multiple operations or
recursion.
• Recursion:
o Inline Function: Cannot be used with recursive functions.
o Regular Function: Supports recursion naturally.
• Compiler Behaviour:
o Inline Function: The inline keyword is a suggestion to the
compiler, which may ignore it for large or complex functions.
o Regular Function: The function is always treated as a normal
function, regardless of its complexity.
Ques 12. Write a program to multiply two matrices.
Used when the type of data is not known in advance or when a function
needs to handle different types of data.
1. Arithmetic Operators:
• Examples: == (equal to), != (not equal to), < (less than), > (greater
than), <= (less than or equal to), >= (greater than or equal to).
3. Logical Operators:
4. Assignment Operators:
5. Bitwise Operators:
6. Unary Operators:
7. Ternary Operator:
8. Special Operators:
• Examples:
o sizeof: To get the size of a data type or object.
o :: (scope resolution): To access a global variable or a class
member.
o . and ->: To access members of a class or structure.
o new and delete: For dynamic memory allocation and
deallocation.
Example:
class Test {
public:
void display();
};
void Test::display() { // Defined outside
using ::
cout << "This is a member function!" << endl;
}
class Test {
public:
static int count;
};
int Test::count = 0; // Define static member
using ::
class Base {
public:
void display() { cout << "Base class
function" << endl; }
};
class Derived : public Base {
public:
void display() { cout << "Derived class
function" << endl; }
void callBase() { Base::display(); } // Call
base class function
};
Example:
#include <iostream>
class funcOverloading{
public:
}
void sum(double x,double y){
};
int main() {
funcOverloading obj;
obj.sum(10,20);
obj.sum(10,20,30);
obj.sum(12.34,34.21);
obj.sum(12.32,12);
obj.sum(12,23.56);
return 0;
Output :
Ques 19. What is constructor? Explain its types with the help of
program.
Types of Constructors:
1. Default Constructor:
a. A constructor that takes no arguments.
b. Automatically called when an object is created without
passing arguments.
c. If no constructor is explicitly defined in the class, the compiler
provides a default constructor.
2. Parameterized Constructor:
a. A constructor that takes arguments to initialize object data
members with specific values.
3. Copy Constructor:
a. A constructor that creates a new object as a copy of an existing
object.
Example:
#include <iostream>
using namespace std;
class Example{
public:
Example(){
cout<<"default constructor\n";
}
Example(int x){
cout<<"parametrized constructor\n";
}
Example(const Example& obj){
cout<<"copy constructor\n";
}
};
int main() {
Example obj;
Example obj1(10);
Example obj2 = obj1;
return 0;
}
Output:
default constructor
parametrized constructor
copy constructor
In C++, new and delete are operators used for dynamic memory
management, allowing programmers to allocate and deallocate memory
during runtime.
1. new Operator:
2. delete Operator:
#include <iostream>
class Complex {
private:
int real;
int imag;
public:
cout << real << " + " << imag << "i" << endl;
};
int main() {
c1.display();
c2.display();
c3.display();
return 0;
}
Ques 25. What do you mean by dynamic memory allocation?
In C++, the new operator is used to allocate memory, and the delete
operator is used to deallocate it.
Example:
#include <iostream>
class Rectangle{
int length,breadth;
public:
Rectangle(int length,int breadth){
this->length = length;
this->breadth = breadth;
void area(){
};
int main() {
Rectangle rec(4,5);
rec.area();
return 0;
Output:
Types of Inheritance
Ques 30. Define multiple inheritance with the help of suitable program.
Multiple inheritance is a type of inheritance in which a derived class
inherits from two or more base classes. It allows the derived class to
access and combine the properties and methods of multiple base classes.
1. Derived Class Access: The derived class inherits all accessible
members (depending on the access specifiers) from the multiple
base classes.
2. Ambiguity: If two base classes have members with the same name,
ambiguity can arise, which must be resolved explicitly using the
scope resolution operator or virtual base class.
Syntax:
class Base1 {
// Members of Base1
};
class Base2 {
// Members of Base2
};
class Derived : public Base1, public Base2 {
// Members of Derived
};
1. Static Binding
Definition:
Static binding, also known as early binding, occurs at compile time. The
compiler determines which function to call based on the type of the object
at compile time.
Key Features:
class Base {
public:
void display() { // Normal function
cout << "Display from Base class (Static Binding)" << endl;
}
};
int main() {
Base obj; // Object of Base class
obj.display(); // Calls Base::display() (static binding)
return 0;
}
2. Dynamic Binding
Definition:
Key Features:
class Base {
public:
virtual void display() { // Virtual function
cout << "Display from Base class (Dynamic Binding)" <<
endl;
}
};
int main() {
Base* objPtr; // Pointer to Base class
Derived derivedObj; // Object of Derived class
objPtr = &derivedObj; // Base class pointer points to Derived
class object
return 0;
}
Syntax:
class Base{
public:
Example:
#include<iostream>
class shape{
public:
};
int l,b;
public:
this->l = l;
this->b = b;
}
double area(){
return l*b;
void draw(){
cout<<"Drawing a rectangle\n";
};
int main(){
Rec->draw();
C->draw();
return 0;
Output:
Drawing a rectangle
Drawing a circle
Syntax:
class AbstractClass {
public:
};
Typecasting in C++
Example:
double a = 5.7;
• try: the code that might throw error is placed inside try block.
• throw: Used to throw an exception when an error is detected.
• catch: Defines a block of code to handle the exception.
1. Synchronous Exceptions
2. Asynchronous Exceptions
1. Synchronous Exceptions
Example:
cpp
Copy code
try {
int a = 10, b = 0;
if (b == 0) {
throw "Division by zero error"; // Synchronous
exception
}
cout << a / b;
} catch (const char* msg) {
cout << "Exception: " << msg << endl;
}
2. Asynchronous Exceptions
Example:
#include <iostream>
#include <stdexcept>
int main() {
try {
throw runtime_error("This is a runtime error.");
} catch (const runtime_error& e) {
cout << "Caught exception: " << e.what() << endl;
}
return 0;
}
Output:
4. User-Defined Exceptions
Example:
#include <iostream>
#include <exception>
int main() {
try {
throw MyException();
} catch (const MyException& e) {
cout << "Caught exception: " << e.what() << endl;
}
return 0;
}
Output:
5. Nested Exceptions
Example:
#include <iostream>
using namespace std;
void function1() {
try {
throw "Exception from function1";
} catch (const char* msg) {
cout << "Caught in function1: " << msg << endl;
throw; // Rethrow exception
}
}
void function2() {
try {
function1(); // This function may throw an exception
} catch (const char* msg) {
cout << "Caught in function2: " << msg << endl;
}
}
int main() {
try {
function2();
} catch (const char* msg) {
cout << "Caught in main: " << msg << endl;
}
return 0;
}
In C++, file I/O operations allow you to read from and write to files. The
standard C++ library provides classes such as ifstream, ofstream, and
fstream for handling file input and output. These classes are defined in
the <fstream> header file.
1. Opening a File: To perform file I/O operations, you first need to open
the file using one of the file stream classes (ifstream, ofstream, or
fstream).
2. Reading from a File: You can use ifstream to read data from a file.
3. Writing to a File: You can use ofstream to write data to a file.
4. Closing a File: After performing the required operations, the file
should be closed using the .close() function to free resources.
5. Checking if the File is Open:You can check if a file has been
successfully opened using the .is_open() method.
File Modes
When opening a file, you can specify different file modes by passing flags.
These flags can be combined using the bitwise OR operator (|).
Here are the most commonly used file modes:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile("example.txt"); // Open for writing
if (!outFile) {
cout << "Error opening file!" << endl;
return 1;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFile("example.txt"); // Open for reading
if (!inFile) {
cout << "Error opening file!" << endl;
return 1;
}
string line;
while (getline(inFile, line)) { // Read line by line
cout << line << endl;
}
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file("example.txt", ios::in | ios::out); //
Open for reading and writing
if (!file) {
cout << "Error opening file!" << endl;
return 1;
}
string line;
while (getline(file, line)) { // Read and print each
line
cout << line << endl;
}
Use the ios::app mode to append data to the end of an existing file.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile("example.txt", ios::app); // Open
for appending
if (!outFile) {
cout << "Error opening file!" << endl;
return 1;
}
outFile << "This line is appended to the file.\n";
outFile.close(); // Close the file
cout << "Data appended successfully." << endl;
return 0;
}
For reading and writing binary files, use the ios::binary mode. This is
useful when dealing with non-text data (e.g., images, audio).
#include <iostream>
#include <fstream>
using namespace std;
struct Person {
char name[50];
int age;
};
int main() {
Person p1 = {"John Doe", 30};
outFile.write(reinterpret_cast<char*>(&p1),
sizeof(p1)); // Write struct to file
outFile.close();
inFile.read(reinterpret_cast<char*>(&p2), sizeof(p2));
// Read struct from file
inFile.close();
cout << "Name: " << p2.name << ", Age: " << p2.age <<
endl;
return 0;
}