Inheritance Basics Flashcards

1
Q

What is the main idea of inheritance?

A

Factor out the common features of similar objects into a “base class” and then inherit that base class into more specific “derived classes” that diverge from the base.

For example:

Cars, trucks, motorcycles, and busses are all motor vehicles – they have many traits in common (engines, wheels, speed, etc.) but then they start to diverge from one another. Vehicle would be the base class, which would then split into derived classes such as Car and Truck.

All motor vehicles have engines, speed, wheels, etc. They all get these types of data from the base class. But the Motorcycle class might have a member data boolean value called hasSidecar because that’s a trait unique to Motorcycles, but not all motorcycles have it.

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

In terms of Inheritance, what does the “is a” relationship mean?

A

A derived class is an example or type of the base class. If the base class is Sport, a Soccer object IS A sport.

(It’s worth noting that the inverse is never universally true. A sport is not necessarily tennis – it could be Soccer or Baseball.

So, you can ALWAYS say that “Tennis = Sport” regardless of the context, but you could not definitively say “Sport = Tennis” because that’s not ALWAYS the case. Sport might be Soccer or Baseball depending on the context.)

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

What is the syntax for declaring a derived class?

A

class derivedClassName : public baseClassname

“public” means that the protection levels in the derived class are the same as the base class (this can be changed by changing the keyword in the declaration: public, protected, private)

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

What is the difference between public, private, and protected with regard to classes?

A

public: Any member that is public can be accessed by name from anywhere

private: Any member that is private can be accessed directly only by the class in which it is declared.

protected: Any member that is protected can be accessed directly by the class in which it is declared, and by any classes that are derived from that class (but not from anywhere else).

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

What occurs when a derived object is created? For example, what happens in each of these lines:

Vehicle obj1; // Line 1 (Vehicle is base class)

Car obj2; // line 2 (Car derived from vehicle)

Honda obj3: // line 3 (Honda derived from Car)

A

Line 1: The constructor Vehicle() runs on obj1.

Line 2: The constructor Vehicle() runs on obj2, followed by the constructor Car().

Line 3: The constructor Vehicle() runs on obj3, followed by the constructor Car(), then followed by the constructor Honda().

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

When an object of a derived class goes out of scope, how do the destructors for that object run?

A

In reverse order.

Example:

A Honda object will have its destructors run in the following order:

~Honda(), ~Car(), ~Vehicle()

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

How are parameterized constructors handled with derived classes?

A

In the Initialization List. The constructor for the next-higher object is called explicitly in the initialization list to take the parameters relevant to it and leave the others for the current constructor to handle.

Example with Vehicle class and its derivatives:

Vehicle::Vehicle(int c, int id)
// this parent constructor uses the first two parameters
{
vehicleCategory = c;
idNumber = id;
}

Car::Car(int c, int id, int nd, char* cc) : Vehicle(c, id)
// this function passes the first two parameters up to Vehicle, and uses nd and cc
{
numDoors = nd;
strcpy(carColor,cc);
}

Honda::Honda(int c, int id, int nd, char* cc, char* mod) : Car(c, id, nd, cc)
// This function passes the first four parameters up to car, and uses mod
{
strcpy(model, mod);
}

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

What is an function override?

A

When a derived class inherits a function from another class, it can use that same function without changing the parameters. It essentially gets its own unique version.

Additionally, any parent class’ version of that function can still be called explicitly with the scope resolution operator (className::memberName). This is useful if you want to use the normal functionality of the parent class version and just add on to it. It can eliminate repetitive code.

// Student version
void Student::GradeReport()
{
… processing done by parent function …
}

// Grad version
void Grad::GradeReport()
{
Student::GradeReport(); // explicit call to parent function
// other processing specific to Grad’s version
}

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