C++ : Unit 01
1. List out the operators that cannot be overloaded.
Operators that cannot be overloaded: scope resolution (::), sizeof, member selector (.), member
pointer selector (.*). These are reserved for specific language functionalities. Overloading them
would break C++ semantics.
2. Compare compile time and run time polymorphism.
Compile Time Polymor- Run Time Polymorphism
phism
Resolved during compilation Resolved during execution (e.g.,
(e.g., function overloading). virtual functions).
Achieved via templates or over- Achieved via inheritance and vir-
loading. tual functions.
Faster, no runtime overhead. Slower due to virtual table
lookup.
3. Is Abstract base class used to create objects? Justify your answer.
No, abstract base classes cannot create objects due to pure virtual functions (=0). They serve as
interfaces for derived classes. Ensures derived classes implement required methods.
4. Explain the types of inheritance with an example.
Types of inheritance in C++ with examples:
• Single Inheritance: One class inherits from one base class.
class Base { public: int x; ; class Derived : public Base public: int y; ; // Derived inherits
x from Base
• Multiple Inheritance: A class inherits from multiple base classes.
class A { public: int a; ; class B public: int b; ; class C : public A, public B public: int
c; ; // C inherits a and b
• Multilevel Inheritance: A class inherits from a derived class, forming a chain.
class A { public: int a; ; class B : public A public: int b; ; class C : public B public: int
c; ; // C inherits from B, B from A
• Hierarchical Inheritance: Multiple classes inherit from one base class.
class Base { public: int x; ; class D1 : public Base public: int y; ; class D2 : public Base
public: int z; ; // D1, D2 inherit x
• Hybrid Inheritance: Combines multiple inheritance types, often using virtual base classes.
class A { public: int a; ; class B : virtual public A public: int b; ; class C : virtual public
A public: int c; ; class D : public B, public C public: int d; ; // Avoids duplicate A
5. Differentiate Function overloading and Function overriding.
Function Overloading Function Overriding
Same function name, different Same function signature in base
parameters in same class. and derived class.
Compile-time polymorphism. Run-time polymorphism via vir-
tual functions.
No inheritance required. Requires inheritance and virtual
keyword.
6. How does dynamic memory allocation is different from static memory allocation?
Dynamic Memory Alloca- Static Memory Allocation
tion
Allocated at runtime using Allocated at compile time, fixed
new/delete. size.
Memory size can change during Memory size fixed, cannot be re-
execution. sized.
Stored in heap, manual manage- Stored in stack or global area,
ment. automatic.
1
7. Can all the predefined C++ operators be overloaded.
No, operators like scope resolution (::), sizeof, member selector (.), member pointer selector
(.*) cannot be overloaded. They are integral to C++ syntax. Overloading others allows custom
behavior.
8. Explain the various types of inheritance in C++ along with a relevant example.
Types: Single, Multiple, Multilevel, Hierarchical, Hybrid. Example: class Animal { }; class
Dog : public Animal { }; (Single). class Amphibian : public Animal, public WaterAnimal
{ }; (Multiple). Facilitates code reuse and hierarchy.
9. Explain how operator overloading is useful in object-oriented programming.
Operator overloading allows custom behavior for operators with user-defined types. Enhances code
readability and usability (e.g., + for string concatenation). Supports intuitive object manipulation.
10. Can we create an object of an abstract class? Interpret with an example.
No, abstract classes with pure virtual functions (=0) cannot be instantiated. Example: class Base
{ virtual void func() = 0; }; prevents object creation. Used as interfaces for derived classes.
11. Outline the operators which cannot be overloaded in C++?
Non-overloadable operators: scope resolution (::), sizeof, member selector (.), member pointer
selector (.*). These are tied to core language features. Overloading would disrupt functionality.
12. Distinguish between function overloading and operator overloading.
Function Overloading Operator Overloading
Different parameters, same func- Redefines operator behavior for
tion name. classes.
No special syntax, regular func- Uses operator keyword (e.g.,
tions. operator+).
Applies to any function. Limited to specific operators.
13. Illustrate how friend function is used in operator overloading? Give an example.
Friend functions access private members for operator overloading. Example: class Complex {
friend Complex operator+(Complex, Complex); }; allows + to add two objects. Enhances
flexibility for external operations.
14. How do you use dynamic memory allocation in C++ to create and manipulate arrays?
Use new to allocate arrays (int* arr = new int[10];) and delete[] to deallocate. Allows run-
time size determination. Example: Resize array based on user input.
15. What is the difference between the new operator and the malloc() function in C++?
new Operator malloc() Function
C++ specific, calls constructors. C function, only allocates mem-
ory.
Returns typed pointer, no cast- Returns void*, requires casting.
ing needed.
Handles memory allocation ex- Returns NULL on failure.
ceptions.
16. Differentiate function overloading and function overriding in C++.
Function Overloading Function Overriding
Same name, different parameters Same signature in base and de-
in same class. rived class.
Compile-time resolution. Run-time resolution with virtual
functions.
No inheritance needed. Requires inheritance hierarchy.
17. How does dynamic binding work in C++? How does it relate to virtual functions?
Dynamic binding resolves function calls at runtime using virtual tables. Virtual functions (virtual
void func();) enable this in derived classes. Ensures correct function is called based on object
type.
2
18. Compare the advantages of new operator over malloc()?
new Operator Advantages malloc() Limitations
Automatically calls constructors Only allocates raw memory, no
for objects. initialization.
Type-safe, no casting required. Returns void*, needs explicit
casting.
Throws exceptions on failure, Returns NULL, requires manual
safer. checks.
19. Identify how memory management is performed dynamically in C++?
Dynamic memory is managed using new for allocation and delete/delete[] for deallocation.
Smart pointers (std::uniquep tr, std::sharedp tr)automatecleanup.P reventsmemoryleaksanddanglingpointers.A
21. Describe how polymorphism allows functions and operators to behave differently based
20.
on the context in object-oriented programming.
Polymorphism enables functions/operators to act differently based on object types. Achieved via
overloading (compile-time) or overriding (run-time with virtual functions). Enhances flexibility
and code reusability.
22. Analyze the behavior of the comma operator in the given program and write the
output.
The comma operator evaluates expressions, returning the last one. In int i = {1, 2, 3};, only
the last value (3) is assigned to i. Output: 3.
23. Outline the syntax for this pointer and explain with example.
this is a pointer to the current object. Syntax: this->member. Example: class X { int x;
void set(int x) { this->x = x; } }; resolves ambiguity between parameter and member.
24. Is Abstract base class used to create objects? Justify your answer.
No, abstract base classes with pure virtual functions (=0) cannot be instantiated. They define
interfaces for derived classes. Example: class Base { virtual void func() = 0; }; enforces
implementation in subclasses.
25. Compare Function overloading and Function overriding.
Function Overloading Function Overriding
Different parameters, same name Same name and signature in
in one class. base/derived classes.
Resolved at compile time. Resolved at run time with virtual
functions.
No inheritance involved. Requires inheritance and virtual
keyword.
26. Will all predefined C++ operators be overloaded?
No, operators like scope resolution (::), sizeof, member selector (.), member pointer selector (.*)
cannot be overloaded. They are core to C++ syntax. Others can be customized for user-defined
types.
27. How runtime polymorphism is achieved in function overriding?
Runtime polymorphism is achieved using virtual functions and inheritance. The virtual keyword
ensures the correct derived class function is called via a base class pointer/reference. Uses virtual
table for dynamic dispatch.