Inheritance in C++ - Reading Assignment

Part. 1

  1. Inheritance creates new data by taking objects and combining or connecting values to create new objects.
  2. Various parts used to build cars.

Part 2.

  1. The superclass is the class that is inherited from. And the subclass is the class doing the inheriting.
  2. class Manager : public Employee{}
  3. By inheriting member functions/variables it extends by creating derived classes.
2 Likes
  1. We need inheritance in C++ because it allows us to create hierarchy in our programs. This will allow us to create object that inherits info from other objects then allow us to customize them more specifically.

  2. An example of inheritance from the real world could consist of Animal–> Dog --> Yorkshire Terrier.

  1. A superclass is the parent class that any subclass derived from it will inherit all of it’s variables and functions. A subclass is a child class that will inherit all of the variables and functions of its parent class that it is derived from.

  2. An example of public inheritance could be: class Dog : public Animal

  3. A subclass can extend the functionality of a superclass by creating a more specific version of the superclass with added members and methods of a higher specificity.

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.

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

The first step level could be life on earth, the next levels could be plants (trees, flowers), animals (warm blooded and cold blooded) and humans (woman and man)

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

Superclass is the parent from with the subclass (child) inherits the information

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

Public inheritance is written by writing a keyword “class” and defining it, then comes the colon and keyword “public” and the class from which to inherit. As in the example below:

image

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

Subclass can extend the functionality of a superclass by having functionalities written after the subclass definition and inheritance line in the {} brackets, as in the example above. For example, m-battingAverage(); s a functionality of a subclass BaseballPlayer but is not accessible in Person.

2 Likes

a1. In order to make efficiently coded objects that can leverage previous objects’ features and add on their own
a2. Animal taxonomy

b1. A superclass is a class that is being inherited from, and a subclass is a class that is inheriting from, the implied perspective class.
b2. class Class : public Superclass
b3. It can add additional members and methods and increase the specificity and functionality of the superclass.

1 Like

Inheritance in C++

  1. We need Inheritance in C++ to create a new object, it lets you construct complex classes that models an “is-a” relationship between two objects.

  2. Lion, Cheetah, Tiger inherits from a class animal.

Thinking :speech_balloon:

  1. When implementing inheritance, the existing class from which the new classes are derived is the Superclass. When implementing inheritance, the class that inherits the properties and methods from the Superclass is the Subclass.

  2. class lion : public Animal
    {

    };

  3. Ways a subclass extend the functionality of a superclass by adding members, variables & functions.

:balloon:

1 Like
  1. Why do we need inheritance in C++?
    We don’t have to redefine the base class when creating a subclass; in other words we
    can reuse the code in the subclass that was already written in the superclass.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Car
    SUV Sedan
    | |
    2 door 2 door
    | |
    4 door 4 door


  1. What is a superclass and what is a subclass?
    The class which behaviors (member functions) and properties (member variables) are inherited by the subclass.

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

    public:    // This Supervisor can oversee a max of 5 employees
    long m_overseesIDs[5]{};
    

    };

  3. In what ways can a subclass extend the functionality of a superclass?
    It saves on work because the subclass can borrow the member functions and
    member variables.

1 Like

In the examples in the second article, the members declared do not contain anything in the function body but there is a line between the arguments and the empty function body that I am not familiar with. Can anyone advise what is going on here or direct me to an article where this is discusssed?

e.g

Person(const std::string& name = "", int age = 0)
        : m_name{ name }, m_age{ age }  //what is happening here?
    {
    }
  BaseballPlayer(double battingAverage = 0.0, int homeRuns = 0)
       : m_battingAverage{battingAverage}, m_homeRuns{homeRuns} //similarly what is happening here?
    {
    }

At a guess, it seems to be setting the class variable values but I’m not sure. The syntax is unfamiliar and am not sure where to look for an explanation. Any help would be great :slight_smile:

1 Like

Part I

  1. The capability of a class to derive properties and characteristics from another class is called inheritance. With inheritance we can reuse the fields and methods of the existing class. Facilitates reusability and is an important concept of OOPs.

  2. Oak, beech, elm inherit from trees.

Part II

  1. The class being inherited from is called the parent class, base class or superclass.
    The class doing the inheriting is called the child class, derived class or subclass.

  2. class Base {
    public :

    };

  3. By adding members, variables and methods.

1 Like

Its an initialization list for defining default values in a constructor.

For primitive datatypes like in the example this is the same as doing:

Person(const std::string& name = "", int age = 0)
    {
    m_name = name;
    m_age = age;
    }

A good explanation can be found here: https://www.cprogramming.com/tutorial/initialization-lists-c++.html :slight_smile:

1 Like

Think about the following questions while reading:

  1. Why do we need inheritance in C++?
    For code reusability. Inheritance allows us to reuse classes by having other classes inherit their members.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    In the real world, students and employees are persons.
    So in C++, Person would be the base class while Student and Employee the derived classes.

Think about the following questions while reading:

  1. What is a superclass and what is a subclass?
    A subclass inherits member functions and variables from its superclass.

  2. How would you write a public inheritance in C++?
    class Student : public Person
    {
    // Code
    }

  3. In what ways can a subclass extend the functionality of a superclass?
    By adding its own member variables and functions.

1 Like

Inheritance in C++ - Reading Assignment.

A.

  1. We need inheritance because it save us from doing more work. And if we ever need to update or modify the superclass (eg. add new function, or free a bug) all our subclass will automatically inherit the changes.

  2. An object in the real world that could be modelled in C++ is.
    A Fruit comprising of a mango and orange. The superclass is the fruit and the mango and orange are the subclass.

B.

  1. A superclass is the class being inherited from and subclass is the class doing the inheriting.

  2. A public inheritance is written in C++ by first declaring the class ( class declaration), after that, use the colon, the word “public” and the name of the class we wish to inherit.

e.g. // FootballPlayer publicly inheriting Person

 < class FootballPlayer : public Person

{

public:

  1. The way a subclass extends the functionality of a superclass is for the subclass to inherit both the behaviours (member functions) and properties (member variables from the superclass) - subject to some access restriction. The inheritance represent an is-a relationship.
1 Like

Thanks @Alko89 I suspected as much but wanted to be sure. Thank you for the article reference as well.

  1. It enables us to model an “is-a” relationship between objects as opposed to “has-a” relationships seen in object composition (complex classes constructed from simpler classes.
  2. Books could be modelled using inheritance with Book being at the top of the hierarchy, fiction and non-fiction could be the next ‘level’ with a myriad of authors under each of those and then the actual books would be grouped under their respective authors.
  3. In an inheritance relationship, a superclass is the class being inherited from while a subclass is a class that inherits or does the inheriting.
class SubclassName : public SuperclassName
{
public:
// public members and methods
private:
// private members and methods
}
  1. Subclasses enable additional functionality to superclasses by way of adding new subclass specific variables and functions/methods that are only necessary and relevant to that subclass.
1 Like
  1. Inheritance allows classes to inherit common properties from a larger class. It helps to prevent writing redundant code.
  2. Car and motorcycle would inherit from a class called Vehicle.
  3. A superclass provides inheritance to the subclass. The subclass inherits from the superclass.
  4. class Car: public Vehicle{
    //variables and methods here
    }
  5. A subclass can add their own variables and override any methods.
1 Like
  1. Inheritance models a is-a relationship between objects and saves us the time of writing redundant code.

  2. Apple and orange would inherit from a class called fruit.

  3. A superclass is a class that is being inherited from whilst a subclass is a class that is inheriting.

  4. class subclassname : public superclassname

  5. Subclassess can add their own variables and functions thereby extending functionality.

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

Inheritance allows us to reuse classes by having other classes inherit their members.

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

Person can be used as a superclass for groups of people to use such as employees, teams etc

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

Superclass is the base class and subclass is the inheritee.

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

class Supervisor: public Employee

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

Subclass can be itself be a parent class of other classes

1 Like

1. Why do we need inheritance in C++?
we don’t have to redefine the information from the base class. We can just use the inheritanced version and make copies without changing the original class/struct.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
lets say you have a restaurant, this restaurant has a waiter, chef and a manager.
the chef is part of the restaurant but he isn’t the restaurant itself. Same goes for the waiter etc. so the chef, waiter and manager are the inheritance instance from the restaurant.

second article.

1. What is a superclass and what is a subclass?
a superclass is an inheritance or so to speak (is a) relationship. And a subclass is a inheriting class from the superclass this is also called (has a) relationship. It isn’t the superclass but more like a copy from the superclass that you can program is all kinds of ways

2. How would you write a public inheritance in C++?
this is only the start from the class definition: class Myclass : public Superclass

3. In what ways can a subclass extend the functionality of a superclass?
they are able to inheret the superclasses capabilities and yet include more methods and variables in order to go beyond what went before.

1 Like

1 -> Because inheritance in classes decreases the amount of programming in classes, when one class (Mother) with code can be used in multiple other classes (Daughters), that same code can be copied/pasted in to the multiple classes (Daughters) or can be inherinted or in other words implemented with a redirection from the Mother class. example class Mother: public Daughter(s)
2 -> Cars -> volvo -> bmw -> mercedes
Computer -> processor -> harddrive -> keyboard

1 -> a sub class get the inheritance from a superclass
2 -> class superClass { public: };
3 -> class subClass: public superClass

1 Like

In this example Mother inherits from Daughters.

Public inheritance inherits a class as public, like in the example above. Writing public within a class would make properties of the class defined as public instead. :slight_smile:

1 Like

:slight_smile: i understood the question wrong, this makes it clear to me, thx Alko89