OOP Basics Flashcards

1
Q

What is a constructor?

A

A constructor is a special method that runs when a new instance of the class is created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can a constructor recieve parameters?

A

Yes, it can

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is this the correct syntax for a constructor?

ClassName ( )
{

}

A

Yes. A constructor is created using the same name as the name of the class it belongs to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In c++, do classes contain a default constructor?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can a single c++ class contain more than one constructor?

A

Yes, as long as they have different signatures (parameters in this case, since all constructors of a class have the same name)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a destructor?

A

Is a method that is called when an instance of a class gets out of scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of a destructor?

A

A destructor is used to clean and release memory used by the instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is this the correct syntax for a destructor?

ClassName ( )
{

}

A

No, but the syntax shown is missing just a ~ before the name of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What can you do using class inheritance?

A

Inheritance allows us to create a class with common functionality and then create subclasses that share the parent’s attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens when there is code duplication in a program?

A

The same code exists in various places and provides the same functionality. This makes code harder to maintain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In c++, how many subclasses can inherit a single base class?

A

As many as you create.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can a subclass override methods that already exist in the base class?

A

Yes, it can.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we declare a class as a child of another class?

A

Using the syntax:

class NewClass : public BaseClass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What attribute access modifiers are inherited from the base class?

A

Access modifiers “public” and “protected”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are virtual functions used for?

A

To override the behaviour of an inherited method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you declare a method as virtual?

A

By using the ‘virtual’ keyword on the original method. You can also add the ‘override’ keyword to the other method

17
Q

Do virtual functions have a negative impact on performance?

A

Yes, the additional memory used to store the vtable and the call to the function.