Inheritance Basics Flashcards
What is the main idea of inheritance?
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.
In terms of Inheritance, what does the “is a” relationship mean?
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.)
What is the syntax for declaring a derived class?
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)
What is the difference between public, private, and protected with regard to classes?
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).
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)
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().
When an object of a derived class goes out of scope, how do the destructors for that object run?
In reverse order.
Example:
A Honda object will have its destructors run in the following order:
~Honda(), ~Car(), ~Vehicle()
How are parameterized constructors handled with derived classes?
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);
}
What is an function override?
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
}