CPP 4-mark Important questions
1. Explain function overloading with example.
1. Function overloading means having multiple functions with the same name but
different parameters.
2. The functions differ in the number or type of arguments.
3. The correct function is selected at compile time based on the arguments passed.
4. It increases code readability and reusability.
5. Overloaded functions must have different signatures.
6. Return type alone cannot differentiate overloaded functions.
7. Example:
8. void display(int a);
9. void display(double b);
10. Both functions can be called depending on the argument type.
2. What is inheritance? Explain types of inheritance.
1. Inheritance allows a class to acquire properties of another class.
2. The existing class is called base class, and the new class is called derived class.
3. It promotes code reuse and supports polymorphism.
4. Single inheritance has one base and one derived class.
5. Multiple inheritance has one derived class with more than one base class.
6. Multilevel inheritance has a chain of inheritance (A → B → C).
7. Hierarchical inheritance has multiple derived classes from one base class.
8. Hybrid inheritance combines more than one type of inheritance.
3. What is friend function? Write characteristics of friend function.
1. A friend function is not a member of a class but has access to its private members.
2. It is declared using the friend keyword inside the class.
3. It is defined outside the class like a normal function.
4. It can be a standalone function or a member of another class.
5. It does not use the this pointer.
6. It can access all members (public, private, protected) of the class.
7. It cannot be inherited.
8. Example:
9. friend void showData(MyClass obj);
4. What is class Template? Explain Syntax of class Template with example.
1. A class template is used to create generic classes.
2. It allows a class to work with different data types.
3. The template <typename T> syntax is used.
4. T is a placeholder for a data type.
5. A template class is defined using template followed by class definition.
6. You can create multiple objects with different types using the same class.
7. Example:
8. template <class T>
9. class Box { T data; };
10. Object creation: Box<int> b1; Box<float> b2;
5. Explain memory allocation for objects with non static and static data
member.
1. Memory for non-static data members is allocated separately for each object.
2. Each object has its own copy of non-static members.
3. Memory for static data members is shared among all objects.
4. Static data members are stored only once in memory.
5. Static members are not counted in object size.
6. Static members are declared inside the class but defined outside.
7. Access to static members is possible without creating an object.
8. Example:
9. static int count; // shared by all objects
6. Explain operator overloading in C++ with an example.
1. Operator overloading allows you to redefine the meaning of an operator.
2. It is done by defining a special function using operator keyword.
3. It provides user-defined implementation for operators.
4. It increases code readability and object-oriented behavior.
5. Most operators can be overloaded except some (like ::, sizeof).
6. Example:
7. Complex operator+(Complex c);
8. The function adds two complex numbers using +.
9. Operator overloading is done inside or outside the class.
7. Explain array of object in C++ with example.
1. An array of objects is a collection of multiple objects of a class.
2. It helps in storing and accessing similar data types using indexing.
3. Each element in the array is an object.
4. Useful in cases like managing records of students, employees, etc.
5. Syntax:
6. MyClass obj[5];
7. You can access members like obj[i].display();
8. Constructors are called for each object in the array.
9. Looping helps in initializing and displaying array objects.
8. List different types of constructor. Explain any one constructor with
example.
1. Types: Default constructor, Parameterized constructor, Copy constructor.
2. Default constructor takes no arguments.
3. Parameterized constructor takes arguments to initialize members.
4. Copy constructor creates a copy of an existing object.
5. Let’s explain Parameterized constructor.
6. It initializes an object with user-supplied values.
7. Example:
8. MyClass(int x) { a = x; }
9. Object creation: MyClass obj(10);
9. Explain Memory management operators in detail.
1. C++ provides new and delete for dynamic memory management.
2. new allocates memory at runtime.
3. delete deallocates memory and avoids memory leaks.
4. Syntax: int *ptr = new int;
5. To delete: delete ptr;
6. For arrays: int *arr = new int[5]; delete[] arr;
7. These operators replace malloc() and free() of C.
8. They handle constructor and destructor calls automatically.
10. Explain file opening methods in C++ with syntax
1. C++ uses fstream, ifstream, and ofstream for file handling.
2. ifstream is used to read from files.
3. ofstream is used to write to files.
4. fstream can read and write both.
5. Syntax:
6. ofstream fout("file.txt");
7. ifstream fin("file.txt");
8. You can also open files using open() method.
9. Example: fin.open("data.txt");
10. Always close the file using file.close();
11. Explain the types of polymorphism in detail.
1. Polymorphism means same function behaving differently in different situations.
2. Two types: Compile-time and Run-time polymorphism.
3. Compile-time polymorphism is achieved using function or operator overloading.
4. Example: multiple display() functions with different parameters.
5. Run-time polymorphism is achieved using virtual functions.
6. Base class function is overridden by derived class.
7. Function call is resolved at runtime using a pointer.
8. Example: virtual void show();
12. Explain the concept of virtual base class in C++ with an example.
1. Virtual base class solves the diamond problem in multiple inheritance.
2. It ensures only one copy of base class is inherited.
3. It prevents duplication of data from the common base class.
4. Declared using the virtual keyword.
5. Syntax:
6. class A { };
7. class B : virtual public A { };
8. class C : virtual public A { };
9. class D : public B, public C { };
10. Only one instance of class A is inherited in class D.
11. This helps in avoiding ambiguity.
12. Used mainly in complex inheritance structures.
13. Explain the concept of objects as a function argument in C++
1. Objects can be passed to functions like variables.
2. It helps in accessing class members inside a function.
3. Objects can be passed by value, by reference, or by pointer.
4. By value creates a copy of the object.
5. By reference allows direct access to original object.
6. Syntax:
7. void show(MyClass obj);
8. void display(MyClass &obj);
9. Passing objects improves reusability and modularity.
10. Useful in operations like comparison, copy, display, etc.