Object-Oriented Programming & Class Design Flashcards
7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Object-Oriented Programming & Class Design flashcards as text
Which access specifier should be used for data members that must be accessible only within the class and its derived classes?
Answer: protected
Protected members are accessible within the class itself and by derived classes, but not from outside the class hierarchy.
What is the purpose of a copy constructor in C++?
Answer: To initialize an object as a copy of another object of the same type
A copy constructor initializes a new object by copying the state from an existing object of the same type.
In C++, what does the 'explicit' keyword prevent when applied to a constructor?
Answer: Implicit type conversions using that constructor
The explicit keyword prevents the compiler from using a constructor for implicit conversions or copy-initialization.
What is object slicing in C++?
Answer: Losing derived-class data when a derived object is assigned to a base-class object by value
Object slicing occurs when a derived class object is copied into a base class object by value, stripping away the derived portion.
Which of the following correctly declares a pure virtual function in C++?
Answer: virtual void draw() = 0;
A pure virtual function is declared with 'virtual' and assigned '= 0', making the class abstract.
What does the 'this' pointer represent inside a non-static member function?
Answer: A pointer to the calling object
Inside a non-static member function, 'this' is an implicit pointer to the object on which the function was called.
When a class declares a destructor as virtual, what is the primary benefit?
Answer: The correct derived-class destructor is called when deleting through a base-class pointer
A virtual destructor ensures that when a derived object is deleted through a base-class pointer, the derived destructor runs first, preventing resource leaks.