ESCPP
ESCPP
Encapsulation binds data and functions into a single A destructor is a special member function with the
unit and restricts direct access to data members. same name as the class, preceded by a tilde (~),
Inheritance allows one class to derive and reuse and is automatically called when an object is
properties and methods from another class. destroyed. It is used to release resources such as
Polymorphism enables functions to behave memory or file handles.
differently based on the object that invokes them.
Abstraction hides implementation details and only h) What is 'this' pointer?
shows the essential features to the user. The this pointer is an implicit pointer available in all
non-static member functions, which points to the
b) Define pure virtual function. object that invoked the function. It helps
A pure virtual function is a virtual function declared differentiate between instance variables and
in a base class that has no definition there and function parameters when they have the same
must be overridden in derived classes. It is declared name.
using = 0 syntax and makes the class abstract,
preventing its direct instantiation. i) What is Run-Time Polymorphism?
Run-time polymorphism allows the program to
c) What is cascading of I/O operator? determine at runtime which function to call using
Cascading of I/O operators means chaining multiple virtual functions and base class pointers. This
input (>>) or output (<<) operations in one enables dynamic method binding and allows
statement. For example, cout << a << b; prints both behavior to change depending on the object's
variables one after another, improving code actual type.
readability and conciseness.
j) Enlist manipulators in C++.
d) List the ways to define a constant. Some commonly used manipulators in C++ are endl
Constants can be defined using the const keyword (newline), setw (set width), setprecision (set
(e.g., const int x = 10;), #define macro (e.g., #define decimal precision), fixed (fixed-point notation), and
PI 3.14), or by using enum for a group of related showpoint (force display of decimal point). These
constants. These methods ensure that the values are used with output streams to format the output
remain unchanged during execution. neatly.
int main() {
int a = 5, b = 0;
d) What is Destructor? State the importance of creation. This allows flexibility in initializing an
destructor with example object with specific values.
A destructor is a special member function in C++ Example:
that is automatically invoked when an object goes #include <iostream>
out of scope. It has the same name as the class but using namespace std;
is preceded by a tilde (~) and has no return type or
parameters. class Rectangle {
Importance of Destructor: int width, height;
Frees dynamically allocated memory. public:
Closes files or releases system resources. // Parameterized constructor
Prevents memory leaks. Rectangle(int w, int h) {
Example: width = w;
#include <iostream> height = h;
using namespace std; }
void display() {
class Test { cout << "Width: " << width << ", Height: " <<
public: height << endl;
Test() { cout << "Constructor called" << endl; } }
~Test() { cout << "Destructor called" << endl; } };
};
int main() {
int main() { Rectangle rect1(10, 5); // Object creation with
Test obj; values passed to constructor
return 0; rect1.display();
} return 0;
Output: }
Constructor called
Destructor called d) Note on Interrupts
e) What is tokens in C++? Explain in detail Interrupts are signals sent to the processor to gain
Tokens are the smallest meaningful elements of a its attention:
C++ program. The compiler breaks the source code They can be hardware interrupts (from devices like
into tokens during lexical analysis. keyboards) or software interrupts (from programs).
Types of Tokens in C++: The CPU pauses its current task, handles the
Keywords – Reserved words (e.g., int, while, class) interrupt via an interrupt handler, then resumes.
Identifiers – Names for variables, functions, classes Interrupts improve efficiency by allowing the CPU
(e.g., sum, display) to perform other tasks instead of waiting.
Constants – Fixed values (e.g., 10, 'a', 3.14) They are crucial for real-time and multitasking
Operators – Symbols that perform operations (e.g., operating systems.
+, -, *, ==) Examples: Timer interrupts, I/O interrupts, and
Punctuators/Separators – Symbols like ;, ,, {}, () system calls.
Strings/Literals – Sequence of characters inside
double quotes (e.g., "Hello")
Example:
int sum = a + b;
b) Explain parameterized constructor with the
help of a suitable example
A parameterized constructor is a constructor that
takes arguments, allowing different values to be
assigned to the data members at the time of object
c) Explain virtual base class with example
A virtual base class is used in multilevel inheritance template <typename T>
to prevent the diamond problem, where a class is T maximum(T a, T b) {
inherited by multiple classes and those classes are return (a > b) ? a : b;
inherited again. The virtual keyword ensures that a }
base class is shared among all derived classes, int main() {
avoiding multiple instances of the same base class. int x = 10, y = 20;
Example: cout << "Maximum of " << x << " and " << y << "
#include <iostream> is " << maximum(x, y) << endl;
using namespace std;
double p = 3.14, q = 2.71;
class Base { cout << "Maximum of " << p << " and " << q << "
public: is " << maximum(p, q) << endl;
Base() { cout << "Base class constructor" << endl;
} return 0;
}; }
class Derived1 : virtual public Base { e) Write a program to overload the binary plus
public: operator to concatenate two strings
Derived1() { cout << "Derived1 class constructor" Operator overloading allows operators to work
<< endl; } with user-defined data types. We can overload the
}; binary + operator to concatenate two strings in a
class Derived2 : virtual public Base { class.
public: Program:
Derived2() { cout << "Derived2 class constructor" #include <iostream>
<< endl; } #include <string>
}; using namespace std;
class Final : public Derived1, public Derived2 { class MyString {
public: string str;
Final() { cout << "Final class constructor" << endl; public:
} MyString(string s) { str = s; }
};
int main() { // Overload + operator to concatenate two
Final obj; // Only one instance of Base class is strings
created MyString operator+(const MyString& obj) {
return 0; return MyString(str + obj.str);
} }
Output: void display() {
Base class constructor cout << str << endl;
Derived1 class constructor }
Derived2 class constructor };
Final class constructor int main() {
d) Write a C++ program to find the maximum of MyString s1("Hello ");
two integer numbers using function template MyString s2("World!");
Function templates allow a function to work with MyString s3 = s1 + s2; // Using overloaded +
any data type. The compiler generates the code for operator
the specific type when the function is called.
Program: s3.display(); // Output: Hello World!
#include <iostream> return 0;
using namespace std; }
1. What is the ambiguity that arises in multiple 8. What is a file pointer? Write a note on file
inheritance? How can it be overcome? opening and file closing.
In multiple inheritance, ambiguity arises when two A file pointer in C++ is an internal pointer that
or more base classes have methods or data keeps track of the current position within the file. It
members with the same name, and a derived class helps in reading or writing data at specific positions
inherits from them. The compiler cannot decide in a file.
which version of the function or member to call, File Opening:Files are opened using ifstream for
causing confusion. reading, ofstream for writing, and fstream for both.
Virtual Inheritance: This is used in cases of The open() function is used to associate a file with
diamond problem to ensure only one copy of the a file stream object.
base class is inherited by the derived class. File Closing:The close() function is used to close an
Renaming methods: By renaming the functions in open file.It is important to close files to free up
derived classes, we can eliminate name conflicts. system resources.
2. Explain exception handling mechanism in C++ 1. Explain advantages of PHP built-in
Exception handling in C++ allows you to handle functions
runtime errors (exceptions) through a mechanism PHP built-in functions offer predefined solutions for
consisting of three keywords: try, throw, and catch. common tasks such as string manipulation, file
This helps to avoid program termination when an handling, and mathematical operations. They save
error occurs. time, improve reliability, and increase performance
3. Explain class template and function template by being optimized. These functions are thoroughly
In C++, templates enable writing generic code that tested, reducing the likelihood of errors and
works with any data type. Class templates and ensuring code consistency. They also enhance code
function templates are the two main types of readability and make maintenance easier. By using
templates: these functions, developers can focus on more
complex tasks without reinventing the wheel.
1.Class Template: It allows defining a class that can
work with any data type.
2.Function Template: It allows defining a function 2. Explain GET & POST Method
The GET method sends data as part of the URL,
to work with any data type.
making it suitable for retrieving information or
4. Explain namespace
bookmarking, but it has limitations in terms of data
A namespace in C++ is a container used to organize size and security. In contrast, the POST method
and group code into logical units, and prevent sends data in the HTTP request body, which is more
name conflicts. It allows defining variables, secure and capable of handling larger amounts of
functions, and classes that can be referred to using data. While GET is ideal for simple data retrieval,
the namespace name. POST is preferred for submitting sensitive
6. Explain error handling during file operations information, forms, and file uploads. The choice
In C++, errors during file operations can be handled depends on the use case and the amount of data to
using functions like fail(), eof(), bad(), and good() be transmitted.
that are members of the fstream class. These
functions help check if an operation has succeeded 3. List advantages of PHP
or failed. PHP is an open-source, cross-platform scripting
Common error handling functions: language ideal for web development. It integrates
fail(): Returns true if the last operation failed. seamlessly with databases, especially MySQL, and
eof(): Returns true if the end of the file is reached. provides a rich set of built-in functions to simplify
bad(): Returns true if there is an irrecoverable error. development. PHP’s syntax is easy to learn, and it
good(): Returns true if no errors occurred. offers fast performance for dynamic websites. With
strong community support, it is a popular choice for
building robust web applications. PHP’s ability to
work with various web servers like Apache and
Nginx makes it versatile for different environments.