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.
How does polymorphism relate to abstract classes?
Abstract classes allow polymorphic behavior by letting derived classes implement their versions of virtual functions.
What are the benefits of using interfaces?
They allow for flexible and interchangeable object designs.
How does inheritance work with abstract classes?
Derived classes inherit the interface and must implement the abstract methods.
Why is it important to override pure virtual functions in derived classes?
To provide specific implementations for these functions, making the derived class non-abstract.
Can an abstract class be derived from another abstract class?
Yes, and the derived class remains abstract if it doesn’t implement all inherited pure virtual functions.
What is a static library in C++?
A collection of pre-compiled code that can be linked into a program.
How is a static library created in C++?
By compiling code into object files and then archiving them into a library file.
What are the file extensions for static libraries in C++, typically?
.lib in Windows and .a in Linux.
What is a shared library in C++?
A library that allows its code to be shared by multiple programs at runtime.
What are the benefits of using shared libraries in C++?
They reduce the memory footprint and allow for easy updates.
What are the file extensions for shared libraries in C++?
.dll in Windows and .so in Linux.
How do you include a static library in a C++ program?
By linking the library file during the compilation process.
What is exception handling in C++?
A way to handle runtime errors in a program, using try, catch, and throw statements.
Why is exception handling important in C++?
It allows for more robust and maintainable error handling.
What are enumerations in C++?
A way to define a set of named integer constants.
How do you define an enumeration in C++?
Using the enum keyword followed by a name and a list of values.
What is the difference between scoped and unscoped enums in C++?
Scoped enums (enum class) provide better type safety than unscoped enums.
What is the main use of a C++ library?
To provide reusable functions and classes to multiple programs.
How do you link a shared library in a C++ program?
By specifying the library in the compiler/linker settings.
What is the main difference between static and shared libraries?
Static libraries are included in the executable, while shared libraries are loaded at runtime.
How do you handle exceptions thrown by library functions?
By using try-catch blocks around the function calls.
Can you throw objects as exceptions in C++?
Yes, you can throw objects of any type as exceptions.
What is the purpose of the ‘.h’ and ‘.cpp’ files in a C++ library?
‘.h’ files contain declarations, while ‘.cpp’ files contain implementations.
What is the command to create a static library from object files in Linux?
ar -crv libname.a file1.o file2.o
How do you specify the library path when compiling a C++ program?
Using the -L flag to specify the directory and -l to specify the library.
What is the significance of using libraries in software development?
Libraries promote code reuse and modular design.
How do you declare and use an enumeration in C++?
Declare with ‘enum’ or ‘enum class’, and use by specifying the enum value.
What are the common operations performed in library management?
Creating, updating, and linking libraries in software projects.
How do shared libraries save space on a system?
By allowing multiple programs to use the same library code in memory.
What are the advantages of using exception handling in libraries?
It provides a standardized way for libraries to report and handle errors.
What is an L-value in C++?
A variable that points to a specific memory location.
What is an R-value in C++?
A temporary and short-lived value.
How do you create a pointer to an L-value?
Using the syntax ‘int &ref’ to make a reference.
How do you create a pointer to an R-value?
Using the syntax ‘int &&ref’ for an R-value reference.
What is the purpose of R-value references in C++?
To support move semantics and efficient data transfer.
What is a lambda expression in C++?
An anonymous function object capable of capturing variables in scope.
What is the syntax of a lambda expression in C++?
How are lambda expressions used in C++?
As concise function objects for short snippets of code.
What is the capture list in a lambda expression?
It allows the lambda to use variables from its enclosing scope.
What is the significance of the capture list in lambdas?
It controls what external variables are accessible within the lambda.
How do you specify a return type in a lambda?
Using ‘->’ followed by the return type after the parameter list.
Why are lambdas useful in C++?
For creating inline, on-the-spot functions without formal definition.
What is a move constructor in C++?
A constructor that moves resources from a given object to a new object.
What is a move assignment operator?
An operator that transfers resources from one object to another, without copying.
How does move semantics improve performance?
By eliminating unnecessary copying of temporary objects.
What are the benefits of using R-value references?
They enable efficient data transfer and resource management.
How do you use std::move in C++?
To convert an L-value to an R-value reference, enabling move semantics.
What is the difference between copy and move semantics?
Copy semantics duplicate objects, while move semantics transfer existing resources.
When should you use move semantics?
When dealing with temporary, non-reusable resources.
How do lambda expressions improve code readability?
By allowing the definition of functionality directly where it’s used.
What are iterators in C++, and how are they used?
Special variables that point to elements in a container, used for navigating through it.
How do you capture variables by value in a lambda?
By listing them in the capture list without the ‘&’ prefix.
How do you capture variables by reference in a lambda?
By listing them in the capture list with the ‘&’ prefix.
What is the significance of ‘const’ in lambda expressions?
It makes the lambda unable to modify captured variables if they are captured by value.
Why might you choose to capture a variable by reference in a lambda?
To allow the lambda to modify the captured variable.
What is a function template in C++?
A blueprint for creating a function that can work with any data type.
How do you define a function template in C++?
Using the ‘template’ keyword followed by a type parameter list and the function definition.
What is a class template in C++?
A blueprint for creating a class that can operate with generic types.
How do you define a class template in C++?
Using the ‘template’ keyword followed by a type parameter list and the class definition.
Why are templates used in C++?
For code reusability and flexibility with different data types.
How do you instantiate a template function?
By calling the function with a specific data type, which the compiler then uses to generate the function.
How do you instantiate a template class?
By declaring an object of the template class with specific data type(s).
What is template specialization in C++?
Defining a specific implementation of a template for a particular data type.
How do you declare a template specialization?
Using ‘template<>’ followed by the specialized function or class definition.
What are the benefits of using function templates?
They allow the same function to work with different data types, reducing code redundancy.
How does the compiler process template functions?
It generates a specific function for each type of data used with the template.
What is the difference between class templates and function templates?
Class templates define a blueprint for a class, while function templates define a blueprint for a function.
Can class templates have static members?
Yes, class templates can have static members.
How are class template objects instantiated?
By specifying the type in angle brackets when declaring the object.
What is a template parameter in C++, and how is it used?
A template parameter is a placeholder type or value used in a template definition.
Can templates have more than one type parameter in C++?
Yes, templates can have multiple type parameters.
What is a non-type template parameter?
A template parameter that represents a value rather than a type.
How do you pass a non-type template parameter?
By specifying the value in the template argument list.
What is a variadic template in C++?
A template that can take a variable number of arguments.
How do you declare a variadic template?
Using ‘…’ after the last template parameter.
What is the typename keyword used for in templates?
To specify that a template parameter is a type.
Can a function template call other function templates?
Yes, function templates can call other templates.
How can templates lead to more efficient code?
Templates can be optimized by the compiler, leading to more efficient code generation.
What is the role of templates in generic programming in C++?
Templates provide the ability to write code that can operate on any type, central to generic programming.
How do you explicitly specify a template argument?
By providing the type in angle brackets when calling the function or instantiating the class.
What is a container in C++?
A container is a class template that stores a collection of objects.
What are some examples of containers in C++?
Vector, queue, stack, and linked list are common examples.
What is the purpose of using containers?
To manage collections of objects in a structured way.
How are arrays considered as containers?
Arrays like int arr[3] are built-in containers used to store simple objects.
What are the advantages of using containers?
They offer easy insertion, removal, and management of elements.
What is an iterator in C++?
An object that provides a standard way to traverse through a container.
How do iterators function similarly to pointers?
Iterators can move through a container and select elements one at a time.
What is a smart pointer in the context of iterators?
A smart pointer is a type of iterator that manages memory and resource allocation.
How is an iterator different from a pointer?
Iterators are more abstract and can work with different container types, unlike pointers.
Why are iterators important in C++?
They provide a unified way to access elements in different types of containers.
What is a vector in C++?
A dynamic array that can resize itself automatically when elements are added or removed.
What is a queue in C++?
A FIFO (First In, First Out) data structure where elements are inserted from the back and removed from the front.
What is a stack in C++?
A LIFO (Last In, First Out) data structure where elements are added and removed from the top.
What is a linked list in C++?
A collection of nodes where each node contains data and a reference to the next node.
How do you access elements in a container using iterators?
Using methods like .begin() and .end() to get iterators to start and end points.
What are the benefits of using a linked list?
Efficient insertion and deletion of elements without reallocation.
How can you reverse the order of elements in a container?
Using algorithms like std::reverse with the container’s iterators.
How do you insert a new element into a container?
Containers have member functions like push_back or insert for adding elements.
What is the role of .begin() and .end() in container classes?
They return iterators to the beginning and past-the-end elements of the container.
How do you remove an element from a container?
Using member functions like erase or pop_back, depending on the container type.
What is the difference between .begin() and .end() in terms of iterators?
.begin() points to the first element, while .end() points to the position after the last element.
What are the uses of iterators in range-based loops?
They allow iterating over elements of a container efficiently.
How do you display the last element in a container?
By accessing the element before the container’s .end() iterator.
What is the significance of container flexibility in C++?
It allows handling collections of elements in a memory-efficient and organized way.
How do you sort elements in a container?
Using the sort function along with the container’s iterators.