Inheritance in C++ - Reading Assignment

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.

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.

  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.

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.

Part I

  1. In order to avoid to reinvent the wheel. More precisely all men and women are humans so instead of building three classes from scratch we can build a parent class human and then use inheritance for classes man and woman. Inheritance allows us to write less code by reusing properties defined in the parent class
  2. A car is a sub-class of a vehicule

Part II

  1. A superclass or a parent class is a class above a subclass like your parents were born previously than you and you inherit some of their genetics
  2. class Man : public Human
  3. By adding more specifications. For instance in the subclass Man we could add facialHairStyle which wouldn’t be relevant for a Woman subclass so it wouldn’t appear in the superclass Human

1 - we use inheritance in C++ in order to create specific classes and use more generic attributes from other more general class, without having to rewrite these attributes, which allows to focus on the more specific attributes of the class.
2 - you could model the specific class fruit inheriting from the more general class food.

1 - superclasses are classes that have one or more classes inheriting from them. on the other hand, subclasses are classes that inherit characteristics from one or more classes.
2 - After the class declaration, we use a colon, the word “public”, and the name of the class we wish to inherit.
3 - a subclass, as it is more specific, can have extra functionalities that apply to definite cases in the superclass, making it more specialized.

Part 1

  1. For reusing code and not replicate
  2. Muscle, Biceps, Triceps, Quadriceps, Hamstrings,

Part 2

  1. A superclass is a parent class, is being inherited by a child class. A subclass is a child class inheriting a parent class.

  2. class : public {

    }

  3. Add new members
    Add new methods
    Override existing methods

#letsgetthiscrpyto