CPP Object-Oriented Programming & Class Design 1 — Questions and Answers
Question 1: What is encapsulation in C++?
- Separating data and functions.
- Allowing unrestricted access to data.
- Bundling data and related functions together (Correct answer)
- Making all members public.
Correct answer: Bundling data and related functions together
Encapsulation is a fundamental principle of Object-Oriented Programming (OOP) in C++. It involves binding data (attributes) and the methods (functions) that operate on that data into a single unit, typically a class. This mechanism hides the internal state of an object from the outside, exposing only necessary interfaces and protecting data integrity.
Question 2: What is the role of a constructor in a class?
- Deletes object memory.
- Prints object data.
- Initializes an object upon creation (Correct answer)
- None of the above.
Correct answer: Initializes an object upon creation
A constructor is a special member function of a class that is automatically called when an object of that class is created. Its primary role is to initialize the object's data members to a valid state, ensuring that the object is ready for use immediately after its instantiation. Constructors can also perform other setup tasks necessary for the object's proper functioning.
Question 3: What is inheritance in OOP?
- Sharing private members.
- Copying all functions manually.
- Acquiring features from a base class (Correct answer)
- None of the above.
Correct answer: Acquiring features from a base class
Inheritance is a fundamental OOP concept where a new class (derived class) acquires properties and behaviors (features) from an existing class (base class). This mechanism promotes code reusability and establishes an "is-a" relationship, allowing derived classes to extend or specialize the functionality of their base classes.
Question 4: What is polymorphism in C++?
- One function for all tasks.
- Multiple meanings for the same interface (Correct answer)
- Changing data type of variables.
- None of the above.
Correct answer: Multiple meanings for the same interface
Polymorphism, meaning "many forms," allows objects of different classes to be treated as objects of a common base class through a unified interface. In C++, this is often achieved using virtual functions, enabling a single function call to invoke different implementations based on the actual type of the object at runtime, providing flexibility and extensibility.
Question 5: Which keyword is used to inherit a class?
- extend
- implements
- : (Correct answer)
- inherits
Correct answer: :
In C++, the colon (:) symbol is used in the class declaration to specify inheritance. It is placed after the derived class name, followed by an access specifier (e.g., `public`, `protected`, `private`) and then the base class name, indicating that the derived class inherits from the specified base class.
Question 6: Which of the following is an access specifier in C++?
- class
- object
- private (Correct answer)
- method
Correct answer: private
Access specifiers in C++ control the visibility and accessibility of class members (data and functions). `private` is an access specifier that restricts access to members only from within the class itself, ensuring data encapsulation. Other common specifiers include `public` and `protected`.
Question 7: Which method is called automatically when an object is destroyed?
- Constructor
- Destructor (Correct answer)
- Cleaner
- Clearer
Correct answer: Destructor
A destructor is a special member function in C++ that is automatically called when an object goes out of scope or is explicitly deleted. Its primary purpose is to perform necessary cleanup tasks, such as releasing dynamically allocated memory or closing file handles, to prevent resource leaks.
Question 8: How is a class object created in C++?
- create ClassName;
- new objectClass();
- ClassName obj; (Correct answer)
- make ClassName obj;
Correct answer: ClassName obj;
In C++, a class object is typically created on the stack by simply declaring a variable of the class type, followed by the object's name. For example, `ClassName obj;` creates an object named `obj` of type `ClassName`, which automatically invokes its constructor for initialization.
Question 9: Which feature allows a derived class to redefine a base class function?
- Overloading
- Hiding
- Overriding (Correct answer)
- Extending
Correct answer: Overriding
Overriding is a feature in object-oriented programming where a derived class provides a specific implementation for a function that is already defined in its base class. This is achieved using virtual functions in the base class and allows for polymorphic behavior, where the correct function version is called based on the object's actual type at runtime.
What is encapsulation in C++?