Inheritance in C++ - Reading Assignment

#101
1. Why do we need inheritance in C++?

Inheritance let’s you build complex classes, which builds an “is-a” relationship between two objects. It involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them. This avoids duplication and improves your code.

2. Give a simple example of objects in the real world that could be modeled in C++ using inheritance.

Furniture<Table<coffee table and also Furniture<closets<dressings

3. What is a superclass and what is a subclass?

A superclass is a class being inherited, so in my example, it’s furniture, the subclass is then Table. Table is then the superclass to the coffee table sub class. Subclasses inherit the functions and properties of parent classes.

4. How would you write a public inheritance in C++?
class [INSERT NAME OF THE SUBCLASS YOU ARE CREATING] : public [INSERT NAME OF PARENT CLASS]
{
...
};
5. In what ways can a subclass extend the functionality of a superclass?

They do so by either adding variables or members, on top of the inheritance from the superclass.

0 Likes

#102

1. Why do we need inheritance in C++?
It allows us to create objects which have all of the properties of other objects.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Icecream is a type of frozen food which is a type of food.

1. What is a superclass and what is a subclass?
A superclass is a parent class which another class inherits it’s properties from.
2. How would you write a public inheritance in C++?
class Subclass :public MainClass{
}
3. In what ways can a subclass extend the functionality of a superclass?
It adds all of the variables and functions of the superclass plus it’s own variables and functions.

0 Likes

#103
  1. We need inheritance in C++ in order to prevent code duplication. If there was no such concept as inheritance in C++ we would have to duplicate the same properties and functions and ignore the real world relationships between object - this would be very inefficient.

  2. A Truck, Car, and Bus all inherit from a Vehicle.


  1. A superclass contains the base implementation consisting of variable and functions. Subclasses inherit the implementation from the superclass and have their own specific implementation.

  2. class Vehicle {
    // variables
    // e.g 4 wheels, engine, chassis

    // constructor

    // methods
    };

class Bus: public Vehicle {
// bus specific variables
// payment machine, number of deckers e.t.c

// bus methods
};

  1. A subclass automatically receives the variables and methods from superclass through extension. Subclass can add own specific variables and methods.
0 Likes

#104

1. Why do we need inheritance in C++? this is a way to simplify our code, child class derivations can simply inherit the member variables of the parent class, and then have their own variables, which in turn, can be inherited by child classes of that class. All of this reduces the complexity of the code because each child class will already have a basic set of variables that it can access from the parent.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. We are all children of our parents. So, genetically, we inherit certain characteristics of our parents (dominant and recessive genes), however, we also have our own individual characteristics that are unique to ourselves.
1. What is a superclass and what is a subclass? A superclass is a parent class from which other child classes inherit from.
2. How would you write a public inheritance in C++? class child public: parent { }
3. In what ways can a subclass extend the functionality of a superclass? A subclass can extend the functionality of a superclass by having its own functions and methods that use the inherited variables and functions of the parent to perform yet more extended functionality.

0 Likes