C++ Flashcards
True or False?
A constructor has to return a value
False
A constructor has no return type
(not even void)
True or False?
A constructor must have the same name as the class
True
True or False?
A constructor can not take arguments
False
True or False?
A constructor can be overloaded
True
A class can have multiple constructors
Explain “constructor”
in 3 short statements
Constructor:
- Same name as the class
- No return type
- Initializes the member variables
True or False?
Abstract classes can only have pure virtual methods
False
Abstract classes must have at least one pure virtual method.
But it can have a mix of pure virtual, virtual and non-virtual methods
True or False?
You can not create objects from an abstract class
True
An abstract class can not be initilialized directly
What makes a class abstract?
It has at least one pure virtual method
Explain encapsulation
Encapsulation promotes modularity by allowing the internal details of a scope to be modified without affecting other parts of the program
Ex. Changes to the internal variables are isolated within the scope, and won’t be changed outside and variable created within a scope only exists within that scope.
True or false?
A class’ static methods can access private member variables of the class
False
Static methods of a class do not have access to the private member variables of the class directly
Explain private member
Private members are only accessible within the scope of the class itself
Explain protected member
A protected member is accessible within the class that declares it and by classes derived from it
However, it is not accessible from outside the class hierarchy
Explain public member
The public access specifier allows members of the class to be accessed from outside the class definition
Explain Pure virtual Methods
A pure virtual method is a virtual function that has no implementation in the base class and is meant to be overridden by derived classes
It is declared with the = 0 syntax at the end of its declaration.
Ex.
virtual void pureVirtualMethod() = 0;
Explain virtual Methods
A virtual method is a member function of a class that can be overridden in a derived class
When a method is declared as virtual in a base class, it allows a derived class to provide its own implementation of that method
Ex.
virtual void virtualMethod()
{
std::cout«“Hello”«std::endl;
}
Explain static methods
Static method:
- A member function of a class that is associated with the class itself rather than an instance of the class
- A static method can be called on the class itself, rather than on an object of the class
- Static methods are declared using the static keyword
Ex.
static void staticMethod()
{
std::cout «“This is a static method.”«std::endl;
}
Explain overriding
Overriding:
- Allows a derived class to provide a specific implementation for a method that is already defined in its base class
- The overridden method in the derived class must have the same signature (name, return type, and parameters) as the method in the base class
Ex.
void virtualMethod() const override
{
std::cout«“DerivedClass::virtualMethod()”«std::endl;
}
Explain function overloading
Function overloading allows you to define multiple functions with the same name but different parameter lists.
(The compiler determines which function to call based on the number, types, and order of the arguments passed during the function call.)
what concept does the following code demonstrate?
class A
{
void print (int i)
{
std :: cout «_space;“print: “ «_space;i endl;
}
};
class B : class A
{
void print (float f)
{
std :: cout «_space;“print again: “ «_space;f endl;
}
};
a) Overriding
b) Encapsulation
c) Overloading
d) Polymorphism
c) Overloading
You can still call print on an object of class B with an int as parameter, in that case the method in the base class will be called.
Ex.
B objB;
objB.print(42); // Calls A::print(int)
objB.print(3.14f); // Calls B::print(float)
Explain polymorphism
- Compile-time (Static) Polymorphism
- Runtime (Dynamic) Polymorphism
Polymorphism allows objects of different types to be treated as objects of a common base type
Compile-time (Static) Polymorphism:
- Also known as function overloading and operator overloading
- Occurs at compile time
- Multiple functions or operators with the same name but different parameter types or a different number of parameters
- The compiler determines which function or operator to call based on the arguments provided
Runtime (Dynamic) Polymorphism:
- Achieved through virtual functions and inheritance
- Occurs at runtime
- Allows a base class pointer or reference to refer to objects of derived classes
Explain composition
Composition represents a “has-a” relationship, indicating that an object is composed of other objects
Ex. A car has an engine and 4 wheels
Syntax for calling the superclass A method x() from subclass B
A::x();
Explain Struct
A struct is like a class, but the default access control is set to public
Fields and methods of a struct is by default:
a) Private
b) Public
c) Protected
d) Secured
b) Public
Fields and methods of a class is by default:
a) Private
b) Public
c) Protected
d) Secured
a) Private
int x = 5;
Create a reference variable to x.
int &ref = x;
What is the memory area called where dynamic allocations are stored?
Heap
What is the memory area called where static allocations are stored?
Stack