0% found this document useful (0 votes)
43 views6 pages

C++ Exam Notes Two Full QnA Per Page

The document contains notes on various C++ concepts including operator overloading, memory allocation, pure virtual functions, dynamic constructors, hierarchical inheritance, and more. Each section provides definitions, key points, and examples to illustrate the concepts. The notes serve as a study guide for understanding fundamental C++ programming principles.

Uploaded by

avishkarbhavar19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

C++ Exam Notes Two Full QnA Per Page

The document contains notes on various C++ concepts including operator overloading, memory allocation, pure virtual functions, dynamic constructors, hierarchical inheritance, and more. Each section provides definitions, key points, and examples to illustrate the concepts. The notes serve as a study guide for understanding fundamental C++ programming principles.

Uploaded by

avishkarbhavar19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ University Exam Notes

Q2 (a) Operator Overloading

1. Redefines an operator to work with user-defined types.

2. Enhances code readability for class operations.

3. Uses the `operator` keyword to overload.

4. Only existing operators can be overloaded.

5. Must be a class member or friend function.

6. Binary operators take one argument (if member).

7. Operator precedence is unchanged.

8. Common in arithmetic or comparison for objects.

Example:

class Complex {

int real, imag;

public:

Complex(int r=0, int i=0) : real(r), imag(i) {}

Complex operator + (Complex const &obj) {

return Complex(real + obj.real, imag + obj.imag);

};

Q2 (b) Memory Allocation (Static/Non-static)

1. Non-static members exist per object.

2. Static members are shared across objects.

3. Static data is stored separately from object instances.

4. Static members must be defined outside the class.

5. Non-static initialized by constructors.

6. Static accessed without an object.

7. Memory for static members is allocated once.

8. Useful for counters or shared settings.

Example:

class Demo { Int x;


Static int count;
};

Int Demo : : count = 0;


C++ University Exam Notes

Q2 (c) Pure Virtual Function

1. Declared with `= 0` in base class.

2. Makes the class abstract.

3. Enforces implementation in derived class.

4. Cannot create object of abstract class.

5. Used for runtime polymorphism.

6. Promotes interface-based programming.

7. Allows base pointer to call derived methods.

8. Helps design frameworks and plugins.

Example:

class Shape {

virtual void draw() = 0;

};

class Circle : public Shape {

void draw() { cout << "Circle"; }

};

Q2 (d) Dynamic Constructor

1. Allocates memory at runtime.

2. Uses `new` keyword inside constructor.

3. Flexible memory handling for objects.

4. Must define destructor to avoid leaks.

5. Ideal for strings and arrays.

6. Constructor performs allocation logic.

7. Common when data size is unknown at compile time.

8. Helps in managing pointers inside objects.

Example:

class String {

char* str;

String(const char* s) {
Str =new [strlen(s)+1];
Strcpy (str , s);
}
~ String () {delete [] str ; }
};
C++ University Exam Notes

Q2 (e) Hierarchical Inheritance

1. One base class, multiple derived classes.

2. Promotes code reusability.

3. Derived classes inherit common features.

4. Enhances modular design.

5. Base class contains general functions.

6. Each subclass can add specific functions.

7. Supports real-world hierarchical structures.

8. Reduces code redundancy.

Example:

class Animal { void eat(); };

class Dog : public Animal { void bark(); };

class Cat : public Animal { void meow(); };

Q4 (a) Object as Function Argument

1. Objects passed like normal variables.

2. Pass by value (copy), reference or pointer.

3. Reference allows modifying original.

4. Value is safe but creates overhead.

5. Useful for comparing or combining objects.

6. Supports modular design.

7. Maintains data encapsulation.

8. Often used in operator overloading.

Example:

class Sample {

int data;

void compare(Sample s) {

if (data == s.data) cout << "Equal";

};
C++ University Exam Notes

Q4 (b) Friend Function

1. Uses `friend` keyword in class.

2. Can access private/protected data.


3. Not a member function.

4. Defined outside the class.

5. Can be used in multiple classes.

6. Used in operator overloading.

7. Breaks encapsulation (use carefully).

8. Useful when external functions need access.

Example:

class A {

friend void show(A);

};

void show(A a) { cout << a.x; }

Q4 (c) Class Template

1. Supports generic programming.

2. Works with any data type.

3. Reduces code duplication.

4. Uses `template<class T>` syntax.

5. Compiler generates required versions.

6. Enables type-safe flexible code.

7. Used in STL (vectors, stacks).

8. Can take multiple type parameters.

Example:

template <class T>

class Calc {

T a, b;

Calc(T x, T y) { a = x; b = y; }

T add() { return a + b; }

};
C++ University Exam Notes

Q5 (a) This Pointer

1. Pointer to current object.

2. Implicit in all non-static methods.

3. Resolves naming conflicts.

4. Used in method chaining.

5. Cannot be modified.

6. Helps return reference to calling object.

7. Points to calling instance.

8. Supports fluent interface designs.

Example:

class Box {

Box(int length) { this->length = length; }

};

Q5 (b) Function Overriding

1. Redefines base function in derived class.

2. Requires inheritance.

3. Signatures must match exactly.

4. Base function should be `virtual`.

5. Supports dynamic polymorphism.

6. Enables run-time decision making.

7. Function called depends on object type.

8. Used in interfaces and plugins.

Example:

class Base {

virtual void show(); };

class Derived : public Base {

void show(); };
C++ University Exam Notes

Q5 (c) Exception Handling

1. Handles runtime errors gracefully.

2. Uses `try`, `throw`, `catch`.

3. Prevents program crashes.

4. Specific and general catch blocks.

5. Catch-all: `catch(...)`.

6. Can throw objects or primitives.

7. Custom exception classes supported.

8. Encourages error-resilient code.

Example:

try {

throw 5; }

catch(int e) { cout << e; }

You might also like