OOP Basics Flashcards
What is a constructor?
A constructor is a special method that runs when a new instance of the class is created.
Can a constructor recieve parameters?
Yes, it can
Is this the correct syntax for a constructor?
ClassName ( )
{
}
Yes. A constructor is created using the same name as the name of the class it belongs to.
In c++, do classes contain a default constructor?
Yes
Can a single c++ class contain more than one constructor?
Yes, as long as they have different signatures (parameters in this case, since all constructors of a class have the same name)
What is a destructor?
Is a method that is called when an instance of a class gets out of scope.
What is the purpose of a destructor?
A destructor is used to clean and release memory used by the instance.
Is this the correct syntax for a destructor?
ClassName ( )
{
}
No, but the syntax shown is missing just a ~ before the name of the class.
What can you do using class inheritance?
Inheritance allows us to create a class with common functionality and then create subclasses that share the parent’s attributes.
What happens when there is code duplication in a program?
The same code exists in various places and provides the same functionality. This makes code harder to maintain.
In c++, how many subclasses can inherit a single base class?
As many as you create.
Can a subclass override methods that already exist in the base class?
Yes, it can.
How can we declare a class as a child of another class?
Using the syntax:
class NewClass : public BaseClass
What attribute access modifiers are inherited from the base class?
Access modifiers “public” and “protected”
What are virtual functions used for?
To override the behaviour of an inherited method.