Part 1: High-Yield Revision Notes (Quick Theory)
1. Fundamentals of Object-Oriented Programming (OOPs)
OOPs is a programming paradigm that models real-world entities using objects.
Class: A user-defined data type that binds data members (variables) and member functions (methods) into a single unit. It acts as a blueprint or template.
Object: An instance of a class. It is a dynamic real-world entity that allocates memory when created.
4 Pillars of OOPs:
Encapsulation: Wrapping data members and functions into a single unit (class). Helps achieve data hiding through private access modifiers.
Abstraction: Hiding complex internal implementation details and displaying only essential features to the user (achieved using Abstract Classes and Interfaces).
Inheritance: The mechanism by which an existing class (Base Class) allows a new class (Derived Class) to acquire its properties and behaviors, promoting code reusability.
Polymorphism: The ability of a message or function call to exhibit different behaviors based on context ("One interface, multiple implementations").
2. Access Modifiers & Class Scope
Access modifiers dictate the accessibility of class members within or outside the class scope:
Private (
private): Members are accessible only within the class itself (Default access modifier for C++ classes).Protected (
protected): Members are accessible inside the class and its derived (child) classes, but hidden from the outside world.Public (
public): Members are accessible from anywhere in the program.
3. Constructors & Destructors
Constructor: A special member function automatically executed when an object is instantiated. It shares the exact same name as the class and has no return type (not even
void).Default Constructor: Takes no arguments.
Parameterized Constructor: Takes arguments to initialize custom values during object creation.
Copy Constructor: Initializes an object using values from an existing object of the same class (Syntax:
ClassName(const ClassName &obj)).
Destructor: Automatically executes when an object goes out of scope to release allocated memory. It uses the exact same name as the class, preceded by a tilde symbol (
~ClassName()). It takes no parameters and cannot be overloaded.
4. Inheritance & Access Visibility
Inheritance Types in C++
|
+-----------+-----------+-----+-----+-----------+
| | | | |
Single Multiple Multilevel Hierarchical Hybrid
Member Access Visibility Under Inheritance Modes:
| Base Class Access | Public Inheritance (public) | Protected Inheritance (protected) | Private Inheritance (private) |
| Private | Not Inherited (Inaccessible) | Not Inherited (Inaccessible) | Not Inherited (Inaccessible) |
| Protected | Protected in Derived | Protected in Derived | Private in Derived |
| Public | Public in Derived | Protected in Derived | Private in Derived |
Diamond Problem: Occurs in Multiple Inheritance when two parent classes inherit from the same base class, causing ambiguity in the derived class. Resolved using
virtualbase classes.
5. Polymorphism & Virtual Functions
Polymorphism is broadly categorized into two types:
Compile-time (Static) Polymorphism: Solved at compile time via Early / Static Binding.
Function Overloading: Multiple functions in the same scope with the same name but different parameter lists (type, count, or order).
Operator Overloading: Redefining existing C++ operators for user-defined types (Operators that cannot be overloaded:
.,::,?:,sizeof,.*).
Run-time (Dynamic) Polymorphism: Solved at run time via Late / Dynamic Binding.
Function Overriding: Redefining a base class function in a derived class using the
virtualkeyword.Pure Virtual Function: A function with no implementation body in the base class (Syntax:
virtual void display() = 0;).Abstract Class: A class containing at least one Pure Virtual Function. It cannot be instantiated (objects cannot be created).
6. Dynamic Memory Allocation & Pointers
newOperator: Dynamically allocates memory on the Heap.deleteOperator: Deallocates memory previously allocated on the Heap (e.g.,delete ptr;ordelete[] arr;).thisPointer: An implicit pointer available inside non-static member functions pointing directly to the invoking object's memory address.Friend Function: A non-member function granted explicit access to a class's
privateandprotectedmembers using thefriendkeyword.
Part 2: Top 35 Important MCQs (With Solutions)
What is the default access modifier for members in a C++
class?Answer:
private(Note: Default access in a C++structispublic).
Who created the C++ programming language?
Answer: Bjarne Stroustrup (AT&T Bell Labs, 1979).
Which OOP principle is primarily used to achieve Data Hiding?
Answer: Encapsulation
A class containing at least one Pure Virtual Function is called an:
Answer: Abstract Class
Which of the following is the correct syntax for a Pure Virtual Function in C++?
Answer:
virtual void display() = 0;
What is the return type of a Constructor in C++?
Answer: Constructors do not have any return type (not even
void).
Which symbol precedes the class name to declare a Destructor?
Answer: Tilde symbol (
~)
What is the standard parameter format for a Copy Constructor in C++?
Answer: Pass by reference with
const(e.g.,ClassName(const ClassName &obj)).
When a single derived class inherits directly from two or more base classes, the inheritance is called:
Answer: Multiple Inheritance
Which keyword is used to resolve the 'Diamond Problem' ambiguity in Multiple Inheritance?
Answer:
virtual(Virtual Base Class)
Run-time Polymorphism (Dynamic Binding) is achieved in C++ using:
Answer: Virtual Functions (Function Overriding)
Which of the following is an example of Compile-time Polymorphism?
Answer: Function Overloading and Operator Overloading
Which of the following C++ operators CANNOT be overloaded?
Answer: Scope Resolution Operator (
::), Sizeof (sizeof), Member Access Operator (.), Ternary Operator (?:)
Which operator is used to allocate memory dynamically on the heap in C++?
Answer:
newoperator
What is the correct syntax to free memory dynamically allocated for an array in C++?
Answer:
delete[] ptr;
Which implicit pointer holds the address of the currently invoking object?
Answer:
thispointer
Which function can access the private and protected members of a class despite not being a member function?
Answer: Friend Function (declared using the
friendkeyword)
Function Overloading resolution is based on differences in the function's:
Answer: Function Signature (parameter list types, order, or number). Return type alone is not sufficient.
Which header file must be included for standard input and output stream operations in C++?
Answer:
What are
coutandcinin C++?Answer: Pre-defined Objects (
coutis an instance ofostream,cinis an instance ofistream).
Protected members of a Base Class become
privatein a Derived Class under which inheritance mode?Answer: Private Inheritance (
private)
Redefining a Base Class function in a Derived Class with the exact same signature is known as:
Answer: Function Overriding
Why should a Base Class destructor be declared as
virtualwhen using polymorphic behavior?Answer: To ensure the Derived Class destructor executes first, preventing memory leaks during dynamic object deletion.
Templates in C++ represent which programming paradigm?
Answer: Generic Programming (Writing type-independent reusable code)
Which three keywords form the structure of Exception Handling in C++?
Answer:
try,catch, andthrow
What mechanism does the C++ compiler use internally to resolve Virtual Function calls dynamically?
Answer: Virtual Method Table (vtable) and vptr pointer.
How many Destructors can a single class possess?
Answer: Exactly ONE (Destructors cannot be overloaded).
What is the primary operational difference between a
structand aclassin C++?Answer: Members of a
structarepublicby default, whereas members of aclassareprivateby default.
Which statement is true regarding Static Member Variables in C++?
Answer: A single copy is created and shared across all instantiated objects of that class.
Static Member Functions in C++ can directly access:
Answer: Only static data members and other static member functions (
thispointer is unavailable).
What does the term "Class Instantiation" mean?
Answer: Creating an Object of that class.
Which C++ stream class is specifically used to perform file output (write) operations?
Answer:
ofstream
What is the primary function of the Scope Resolution Operator (
::)?Answer: Accessing global variables or defining member functions outside the class declaration.
Every C++ program begins its execution from which function?
Answer:
main()function
Early binding (Static Binding) takes place during:
Answer: Compile time