Inheritance in C++ - Reading Assignment

Part 1

  1. While object compositions can model a “has-a” relation, inheritance allows us to model an “is-a” relationship between two objects. Attributes and behaviours of other objects are inherited and can then be extended, i.e. they are similar to existing objects, but with special functionality and/or data.
  2. Examples of objects using inheritance in the real world:
    Base class: Vehicle; Inherited classes: Car, Bicycle, Moped
    Base class: InputDevice; Inherited classes: Mouse, Keyboard, Joystick

Part 2

  1. A superclass, also called base class or parent class, is the class being inherited from, whereas a subclass, also called derived class or child class is the class doing the inheriting.
  2. class Subclass : public Superclass { ... }
  3. A subclass can extend the functionality of a superclass by adding both new methods and new member variables.
1 Like

Part 1

  1. Why do we need inheritance in C++?
    Inheritance is one the most import thing of Object Oriented Programming. It allow us create smarter system with less code.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Vehicle class as superclass that has name and max speed as variables. Car class as subclass that has number of doors as variables.

Part 2

  1. What is a superclass and what is a subclass?
    Superclass has the attributes and methods more generic that are inherited by subclass. Whereas the subclass has scope more specific.

  2. How would you write a public inheritance in C++?

    class Car : public Vehicle
    {
    }
1 Like
  1. inheritance makes it easier to define properties and functions that are used in multiple objects.

  2. examples, Family could have derived classes of mother and daughter

  3. superclass is the main class that the other classes are inheriting from.

  4. Class mother: public family

  5. subclass can extend functionality by inheriting functionalities and then more can be added.

1 Like

Part 1

  1. In C++, we need inheritance because some related objects may need to inherit traits from a parent object. This is an easier way to group and categorize your classes.
  2. A Pet parent class can be used so that a Dog Pet and a Cat Pet class inherit its general traits. The Pet parent class can contain owner name, type of pet, and DOB. The child classes can have specific traits about them and also contain the Pet traits.

Part 2

  1. A superclass is the ‘general’ class that will be doing the parent, or passing traits down. A subclass is the class that does the inhereting, which means getting traits from its superclass.
  2. class Dog: Public Pet
    {
         Public:
             string breed;
             int shotsNumber;
             Dog(string b, int a){
                breed = b;
                shotsNumber = a;
             }
             string getBreed(){
                return breed;
             }
      }
    
  3. By inheriting the parent class. Th subclass gets access to the parent’s member variables and functions.
1 Like

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

We need an inheritance in C++ because it enables to create new objects by directly acquiring the attributes and the behaviors of other objects, thus minimizing redundancies and better program structure.

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

An example of an object in a real world scenario is of the following:

At the top of the hierarchy is vehicle.
Following with vehicle are bikes and cars.
In the bicycles category, we can then subdivided into bicycles and motorcycles.
Within the bicycle group, it can be divided into mountain bikes and tandem bikes.
Meanwhile, in the car category, we can categorize into sports cars, limousine, and transporter cars. Example of the sports cars category, we can have the McLaren and Ferrari, while in limousine we can have the option of Rolls Royce, and lastly, for transporter we divide it into school bus, pick-up truck, and sedans.

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

A superclass is the class being inherited meanwhile the subclass is the class inheriting.

2. How would you write a public inheritance in C++?

class superClass : public childClassName

3. In what ways can a subclass extend the functionality of a superclass?

Ways that a subclass can extend the functionality of a superclass are:

  • Does not have to redefine the information of the base class.
  • Add an additional functions or member variables.
  • Automatically receive member functions and member variables.
1 Like

PART I

1. Why do we need inheritance in C++?
Inheritance is a way to construct complex classes. Inheritance models an “is-a” relationship between two objects. It involves creating new objects by directly acquiring the attributes and behaviours of other objects and then extending or specializing them. Inheritance is everywhere in real life.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Consider apples and bananas. Although apples and bananas are different fruits, both have in common that they are fruits. Simple logic tells us that anything that is true of fruits is also true of apples and bananas. We can say that apples and bananas inherit (acquire) all of the properties of fruit because they are fruit. Because apples and bananas are fruit, we also know that apples and bananas will inherit the behavior of ripening.

1 Like

PART II

1. What is a superclass and what is a subclass?
Inheritance in C++ takes place between classes. In an inheritance (is-a) relationship, the class being inherited from is called the parent class, base class, or superclass, and the class doing the inheriting is called the child class, derived class, or subclass.

2. How would you write a public inheritance in C++?
By using : public after the name of the subclass and before the name of the superclass we want to inherit from.

To have an Employee class inherit from a Person class:
after the class Employee declaration, we use a colon, the word “public”, and the name of the class we wish to inherit from, in this example Person.

class Employee: public Person
{
public:
    double m_hourlySalary;
    long m_employeeID;

    Employee(double hourlySalary = 0.0, long employeeID = 0)
    {
        m_hourlySalary = hourlySalary;
        m_employeeID = employeeID;
    }
}

3. In what ways can a subclass extend the functionality of a superclass?
Inheriting from a superclass means we don’t have to redefine the information from the superclass in our subclasses. We automatically receive the member functions and member variables of the base class through inheritance, and then simply add the additional functions or member variables we want.

1 Like
  1. Why do we need inheritance in C++?
    We need inheritance because is easy to reuse more bigger classes and get the members of that class and create a new class that inherit all those members and add more specific ones in our new class.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    example could be a superclass Person that then a child class of Person could be Employee and this child class of course inherit all the members from its parent class Person and we can add new members to the child more specific.

  3. What is a superclass and what is a subclass?
    A superclass is the parent class from wher the child or subclass inherit all the members from.

  4. How would you write a public inheritance in C++?
    example :
    class MyClass : public MyParentClass
    {
    -specific members from MyClass

         };
    
  5. In what ways can a subclass extend the functionality of a superclass?
    A subclass can add its own variables and methods more specific for its own.These are added to the variables and methods(public) of the parent class.

  1. To save us a lot of work redefining classes and writing a lot of code again and again
  2. General term Vehicles could be inherited by cars, trucks or bicycles, each inheriting the vehicles general data and function, and adding each its own specific
  3. Superclass (parent class) is a class, that is inherited from - being the general one. Subclass (child class) is the class, that is inheriting all the data from superclass, and adding some own specific data
  4. class Subclass : public Superclass {}
  5. It can create its own member variables and member functions - specific to that subclass
1 Like

Article 1

  1. Inheritance allows us to model objects with an “is-a” relation to another object. This provides the class we are defining with all of the members and methods of the class it inherits from. This lends immensely to code reuse and maintainability.

  2. You could easily use inheritance to model different kinds of vehicles. You could start with the basic functionality of a vehicle like a name, maker, topSpeed and methods to move and so on. A car would have a number of doors and seats and would inherit its other properties from the vehicle class. An electric car would inherit from the car class its members and methods and then add things like batteryCharge members and recharge methods.

Article 2

  1. A superclass is the class whose members and methods are being inherited, and the class that is inheriting is the subclass.

  2. Public inheritance is achieved by following the class name with a colon followed by the public keyword and the name of the class it is inheriting from in its definition signature.

class Truck : public Vehicle {
    public:
        int m_bedVolume;

    Truck(int bedvol) : m_bedVolume(bedvol){}
};
  1. A subclass can include new members and methods in addition to being able use or overload ( redefine ) members and methods of the superclass it inherits from.
1 Like

1. Why do we need inheritance in C++?
Inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them

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

Black Fleece Sheep | White Fleece Sheep
Awassi | Balwen Welsh Mountain
Badger Face Welsh Mountain | Zwartbles
Racka | Bluefaced Leicester

This can be further broken down into Country of origin an purpose.

1. What is a superclass and what is a subclass?
The class being inherited from is called the superclass

2. How would you write a public inheritance in C++?
class Employee: public Person
{
public:
double m_hourlySalary;
long m_employeeID;

Employee(double hourlySalary = 0.0, long employeeID = 0)
    : m_hourlySalary(hourlySalary), m_employeeID(employeeID)
{
}

void printNameAndSalary() const
{
    std::cout << m_name << ": " << m_hourlySalary << '\n';
}

};
3. In what ways can a subclass extend the functionality of a superclass?
I cannot see how this benefits the superclass, on the otherhand the subclass benefits from the superclass eg
If we ever update or modify the base class (e.g. add new functions, or fix a bug), all of our derived classes will automatically inherit the changes!

2 Likes
  1. Inheritance allows us higher level object composition. Instead of creating a class for every object we need, we can make a hierarchy of classes. Classes at the top of hierarchy are more generic, and classes lower down the hierarchy tree are more specific. The main use-case of inheritance is that child classes inherit everything from their parent classes.

  2. Example: Operating Systems: iOS, Linux, Windows,…

  3. Superclass is a class from which properties are inherited by subclasses. Subclasses inherit properties from superclasses.

  4. class SubClass : public SuperClass

  5. Subclass inherits all behaviours (functions) and properties (variables) from superclass. On top of that we can add any new functions or variables that extend functionality in the way we want.

1 Like

First part:

Why do we need inheritance in C++?
Since C++ was developed from C, inheritance it’s a feature that comes from its origin.
Also, it provides a way to optimize code because you avoid repetitions.

Give a simple example of objects in the real world that could be modelled in C++ using inheritance
A database of health insurance buyers. You will have some basic features, like age, gender, previous diseases, type of coverage. You can built on top of this adding new types of coverages of updates on premiums.

class Person
{
public:
string m_name;
string m_gender;
int m_age;
bollean m_smoker;
}

class basicInsurance : public Person
{
public:
int m_initialDayCoverage;
int m_initialMonthCoverage;
int m_initialYearCoverage;
double m_monthlyPremium;
boolean m_dentalCoverage;
double m_riskSmoker;
}

Second part:

What is a superclass and what is a subclass?
A super class is a class that has child classes.
A subclass is a class that is derived from another class (its
parent class). All the data types and methods present in the parent class are available in the subclass.

How would you write a public inheritance in C++?

class childClass: public parentClass{

};

In what ways can a subclass extend the functionality of a superclass?
The subclass can have new methods and data types, that will not change
those of the superclass. If for instance, you got something wrong in the subclass it won’t affect the superclass.

1 Like
  • Why do we need inheritance in C++?
    Inheritance let us create new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them.

  • Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
    Consider apples and bananas. Although apples and bananas are different fruits, both have in common that they are fruits. And because apples and bananas are fruits, simple logic tells us that anything that is true of fruits is also true of apples and bananas. For example, all fruits have a name, a color, and a size. Therefore, apples and bananas also have a name, a color, and a size. We can say that apples and bananas inherit (acquire) these all of the properties of fruit because they are fruit. We also know that fruit undergoes a ripening process, by which it becomes edible. Because apples and bananas are fruit, we also know that apples and bananas will inherit the behavior of ripening.

1. What is a superclass and what is a subclass?
A superclass is a parent class. The one being inherited by the child class also called the subclass.

2. How would you write a public inheritance in C++?

class Banana : public Fruit {

}

3. In what ways can a subclass extend the functionality of a superclass?
Inheriting from a superclass means we don’t have to redefine the information from the superclass in our subclasses. We automatically receive the member functions and member variables of the superclass through inheritance, and then simply add the additional functions or member variables we want. This not only saves work, but also means that if we ever update or modify the superclass (e.g. add new functions, or fix a bug), all of our derived classes will automatically inherit the changes!

Source: Learn C++

1 Like
  1. Inheritance allows us to take properties from other classes. so that we do not have to recreate the whole class.

  2. orange juice and apple juice inherit from juice.


  1. A super class is the class that is giving inheritance to other classes. and a subclass is the class taking the inheritance from a superclass.

  2. class Parent : public Childclass{

    }

  3. A subclass gets all the functionality from a superclass plus its own functionalities or the functionalities defined in itself.

1 Like

Why do we need inheritance in C++?

  • make use of the reusablility
    Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    1. vehicle 1.1 car 1.1.1 BMW 1.2 plane 1.2.1 Airbus 1.3 Ship 1.3.1 ferry

What is a superclass and what is a subclass?

  • superclass - the parent class
  • subclass the child class
    How would you write a public inheritance in C++?
  • class Child: public Parent
    In what ways can a subclass extend the functionality of a superclass?
  • automatically inherit the variables and method from the superclass
1 Like

PART ONE

  1. To easily fetch characteristics from a “parent class” without defining them again, optimizing the code.
  2. In a company database, if we have several classes for different kinds of workers and we already have a “Person class” that contains the basic information, we can define it as a parent to all the others to avoid defining for each subclass names, age, etc.

PART TWO

  1. “Superclass” is the parent, and “subclass” is the child or inheritor.
  2. Like this: class MySubclass : public MySuperclass {…};
  3. A subclass can use all the available functions of a superclass plus the new defined ones.

1. Why do we need inheritance in C++?
to build complex classes
to create new objects by acquiring attributes and behaviors of other objects and then expanding or specializing them

2. Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
hierarchy of animals, inhereting genes from parents

1 Like

1. What is a superclass and what is a subclass?
a superclass is a class being inherited from;
the class doing the inheriting is called the subclass;

2. How would you write a public inheritance in C++?
class Employee: public Person {
public:

};
3. In what ways can a subclass extend the functionality of a superclass?
can add new members, method and overide previous methods

1 Like
  1. To build complex object structures, minimize code repetition and to improve code or program structures
  2. vehicle - car - electric car
  1. A superclass is the class that is inherited in a subclass.
  2. class MinigolfPlayer : public Person{…};
  3. A subclass extends functionality of the inherited superclass, by adding its own variables and functions.
1 Like