Wk7-12 Content Flashcards
What is inheritance in C++?
A feature that allows a class (derived) to inherit properties and behavior from another class (base).
How is a class declared as a derived class in C++?
By using the syntax: class DerivedClass : accessSpecifier BaseClass.
What is polymorphism in C++?
The ability of objects of different classes to respond differently to the same function call.
What is virtual function in C++?
A function declared in the base class using the keyword ‘virtual’ and is redefined by the derived class.
How does virtual function support polymorphism?
It allows derived classes to override the base class method implementation.
What is the significance of the ‘protected’ access specifier?
Protected members are accessible within the class, its derived classes, but not outside of them.
Why is inheritance used in C++, and what are its benefits?
For code reusability and to establish a relationship between classes.
What is multiple inheritance?
A feature where a class can inherit from more than one base class.
What are the risks of multiple inheritance?
It can lead to ambiguities and complexity, especially with diamond problem.
How is the ‘override’ keyword used in C++?
It ensures that the function is overriding a virtual function from a base class.
What is an abstract class?
A class that cannot be instantiated and usually contains at least one pure virtual function.
How do you declare a pure virtual function?
By assigning 0 to the virtual function: virtual void function() = 0;
What is the ‘final’ specifier in C++?
It prevents a class from being inherited or a virtual function from being overridden.
How can constructors and destructors be inherited?
Through the use of inheritance, constructors and destructors are called for both base and derived classes.
What is static polymorphism?
It is resolved at compile time, e.g., function overloading and templates.
What is dynamic polymorphism?
It is resolved at runtime, e.g., through the use of virtual functions.
How does the ‘this’ pointer relate to inheritance?
It refers to the current object instance, and is used within class methods to refer to the object.
What is the ‘super’ or ‘base’ keyword used for in inheritance?
It is used to refer to the base class, often used in derived class constructors.
What is method overriding in C++?
Replacing a base class method in a derived class with a new implementation.
What is a virtual destructor?
A destructor that is declared virtual in the base class to ensure proper resource release.
How does C++ handle constructor inheritance?
Derived class constructors call base class constructors implicitly or explicitly.
What is the role of UML in OOP?
Unified Modeling Language, it visually represents the design of an OOP system.
How are class relationships represented in UML?
Through various types of lines and arrows, indicating relationships like inheritance and association.
What is the diamond problem in inheritance?
A problem that arises when two classes separately inherit from the same base class and are combined into another class.
What is the difference between overloading and overriding?
Overloading is having functions with the same name but different parameters, while overriding involves redefining a base class’s virtual function in a derived class.
What is a copy constructor in C++?
A constructor that initializes an object using another object of the same class.
How does a copy constructor work in C++?
It copies the values of all data members from one object to another.
What is a friend function in C++?
A function that is not a member of a class but has access to its private and protected members.
How do you declare a friend function in C++?
By using the keyword ‘friend’ before the function prototype within the class definition.
What is the ‘this’ pointer in C++?
A special pointer that points to the object for which the member function is called.
How is the ‘this’ pointer used in C++?
To refer to members of the class within member functions.
What is operator overloading in C++?
Defining a new behavior for an existing operator when used with objects of a user-defined class.
How do you overload an operator in C++?
By defining a function in the class, using the keyword ‘operator’ followed by the operator to be overloaded.
What is a static member in C++?
A member that belongs to the class itself, rather than to any specific object.
How do you declare a static member in a class?
By using the keyword ‘static’ before the member declaration.
What is the purpose of static members in C++?
To share common data across all objects of the class.
How do you access a static member in C++?
Using the class name followed by the scope resolution operator (::).
What is a pointer to a class in C++?
A pointer that holds the address of an object of a class.
How do you access members of a class using a pointer?
By using the arrow operator (->).
What are the benefits of using pointers to classes?
They allow for dynamic allocation and manipulation of objects at runtime.
What is the difference between deep and shallow copying?
Deep copying copies all fields and allocates separate memory, while shallow copying only copies field values.
How do you define a copy assignment operator in C++?
By overloading the assignment operator (=) to correctly copy objects.
What is the rule of three in C++?
If a class defines one of a destructor, copy constructor, or copy assignment operator, it should define all three.
Why might you declare a friend class?
To allow another class access to the private members of the defining class.
What is the syntax for declaring a static member function?
static returnType functionName(parameters);
How can static member functions be useful?
They can be called without an object and can only access static members of the class.
What is an inline function in C++?
A function whose code is copied to the call site to reduce function call overhead.
How do you ensure proper copying of dynamically allocated resources in a copy constructor?
By doing deep copying of the resources.
What is a unique feature of the copy constructor compared to other constructors?
It takes a reference to an object of the same class as its parameter.
What are the best practices for overloading the assignment operator?
Handling self-assignment and freeing existing resources before copying.
What is an interface in C++?
A set of public functions that provide the behavior of a class without a predefined implementation.
How is an interface implemented in C++?
Using abstract classes with pure virtual functions.
What is an abstract class in C++?
A class that cannot be instantiated and is designed to be a base class for other classes.
What makes a class abstract in C++?
Having at least one pure virtual function.
How is a pure virtual function defined in C++?
Using the syntax ‘= 0’ in the function declaration.
Why use abstract classes in C++?
To provide a base class that defines a common interface for derived classes.
Can abstract classes have constructors in C++?
Yes, but they can only be used for construction of derived class objects.
How do you instantiate a derived class from an abstract class in C++?
By overriding all the pure virtual functions in the derived class.
What is the purpose of virtual functions in abstract classes?
To allow derived classes to have their own implementations of these functions.
What happens if a derived class does not override a pure virtual function?
The derived class also becomes an abstract class.
Can abstract classes have non-pure virtual functions?
Yes, and they can provide a default implementation.
What is the role of an abstract class in a class hierarchy?
It serves as a conceptual foundation upon which concrete classes are built.
How do abstract classes promote reusability?
By allowing multiple derived classes to share common functionality.
Why can’t abstract classes be used to create objects directly?
Because they have incomplete function implementations.
How do you declare a class as abstract in C++?
By including at least one pure virtual function in its definition.
Can an abstract class contain data members in C++?
Yes, abstract classes can have data members.
What is the difference between abstract and concrete classes?
Abstract classes have incomplete implementation, while concrete classes are fully implemented.
How do virtual destructors work in abstract classes?
They ensure that destructors of derived classes are called correctly.
Can an abstract class have a non-virtual destructor?
Yes, but it can lead to improper cleanup of derived class resources.
What is the difference between abstract classes and interfaces in C++?
Abstract classes can have member variables and function implementations, while interfaces cannot.