Inheritance in C++ - Reading Assignment

1.We need inheritance because involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them.
2.Boat
-1. fishing boat
-1.1 middle console fishing boat
-1.2 dual console fishing boat
-2. luxury boat
-2.1 express luxury boat
-2.2 cruiser luxury boat

  1. 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. “class SubClass : public SuperClass”
  3. When a subClass extends from a superClass, gets all the functionalities of the superClass that are public or protected and can add more functionalities.
1 Like

Part one

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

To save time and effort. Make things more efficient. Minimize unnecessary repitition.

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

A family: Grandparents (string familyName, string countryOfOrigin, int age) -> Parents (string spouse, string occupation) -> Children (string gender, string school)

** Part two**

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

A superclass is the class that passes on its features (members and methods) to the subclass. A subclass inherits all the features from the superclass.

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

Class Subclass : public Superclass {
…Subclass’ stuff…
};

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

It can add more specific features thus specifing the object more.

1 Like

Think about the following questions while reading:

  1. Why do we need inheritance in C++?
    One of the most important concepts in object-oriented programming is that of inheritance . Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    You can define a base class called “Pen”, which has a colour as attribute and a method “write”. But there are different kinds of pens, for example a pencil, a ballpen and a fountain pen. So we can inherit the colour and the writing method from the base class “Pen”. But we also have to define the characteristics of each different kind of pen in its according subclass. A pencil’s lead has a hardness and a length, which will be member variables in the class pencil. And we will have the method “sharpen”, which will slightly reduce the length member variable of our pencil object. The class “ballpen” will have a member variable inkRemaining and a method called refill. The class “fountainPen” will have a member variable “lineThickness” and a method “inkRefill”. And we could also add the method changeColour, which may be suitable for ballpens and fountain pens.

Think about the following questions while reading:

  1. What is a superclass and what is a subclass?
    A superclass is a parent class and a subclass is a child class. Therefore the superclass will inherit its member variables and member functions to a subclass. The relation between this two classes is “is-a”.

  2. How would you write a public inheritance in C++?
    The class definition of a subclass is followed by a semicolon, the keyword “public” and the name of the base class:
    class SubClass : public BaseClass

  3. In what ways can a subclass extend the functionality of a superclass?
    You can define additional member variables and additional member functions in a subclass making it more specific than the superclass. If a method derived from the superclass is not suitable for the subclass, you can override it, e.g. to add some more functionality. However inheritance is all about code reusability and if you will have to override many methods of the superclass it might be a good idea to refrain from using inheritance.

1 Like

1. Why do we need inheritance in C++?
This way it’s easier to define different classes with common features

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
A bicycle’s features could be inherited by an MTB and a granny bike

1. What is a superclass and what is a subclass?
The Superclass has the shared features that a subclass inherits

2. How would you write a public inheritance in C++?
(After the subclass definition write) : public Superclassname

3. In what ways can a subclass extend the functionality of a superclass?
[/quote]
It can be an inherited class
It can be an inheritance chain of classes

1 Like

Inheritance allows us to create new objects by directly acquiring the attributes and behaviours of other objects, so we can avoid repeating code.

The class of drinks can be a parent class, which then can have child classes of alcoholic drinks, and soft drinks. These child classes can be expanded further into their own child classes, hence they could become parent classes themselves.

A subclass inherits variables and functions from the superclass.

class Subclass : public Superclass
{
};

It can add more variables, functions to it, that is more specific to the subclass.

1 Like
  1. Inheritance allows us to create new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them. Inheritance models an “is-a” relationship and takes place between classes.

  2. Web 2.0 inherited attributes and features from Web 1.0, and ultimately improved upon.

PART 2

  1. a superclass is the class from which a subclass inherits the properties from

  2. public inheritance written as;

class Apple: public Fruit
{
… … …
};

3.a subclass can extend the functionality of a superclass by expanding the functionality it inherits, by adding new variables, methods, or overriding existing methods.

1 Like

Reading Assignment: Inheritance in C++

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

To not duplicat e of same code several times, less risk for bugs.

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

Persons, which could inherit properties like name , weight, gender, lenght etc…

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

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++?

After a class declaration, we can create a public inheritance by adding a colon followed by the public keyword and the name of the class we want to inherit from

class Person // a class declaration

class Employee: public Person // public inheritance

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

By defining new variables and member functions a subclass can extend the functionality of its superclass.

1 Like

Why do we need inheritance in C++?
An inheritance is a relationship between a parent and child class. The members and functions of the parent and child can be shared. This allows for less redundancy in code.

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

For example, if the human class has the person’s name, the hobby class can utilize that name from the human class, and the hiking club can utilize the info from both making a pyramid structure.

1 Like
  1. Inheritance allows us to easily and quickly make changes to our classes and also reuse them without having to rewrite code.

  2. For example, car --> luxury cars --> ferrari


  1. A superclass (or base class or parent class) is the class from which another class (the derived class or subclass or child class) derives behaviors (that is the functions) and the properties (which are the variables/members)

  2. class ChildClassName : public ParentClassName {…}

  3. the child class not only inherits the functionality of a superclass (its functions and variable members) but can also have its own specific methods and fields.

1 Like

1. Why do we need inheritance in C++?
Inheritance allow us to create objects that directly inherits properties of another objects. And so, we don’t have to redefine some properties in our derived class. The child is more precise than the parent class.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
car and truck are children of automotive
cats and dogs inherits from animals.

1. What is a superclass and what is a subclass?
The superclass is the class being inherited from (the parent class). The subclass is the class doing the inheriting (the child class).

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

using single semi-colomn (:)      child:parent
class BaseballPlayer : public Person
{
......
};

3. In what ways can a subclass extend the functionality of a superclass?
When a subclass extends a superclass, she gets all the functionalities of the parent class that are public or protected. The subclass redefines the methods of its superclass. using super() it keeps the parent definition

1 Like
  1. With inheritance we can create more complex classes from simpler classes.

  2. Animals -> fish -> whales is an example of real-world inheritance that we can model with C++

  3. 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.

  4. Public inheritance example: class BaseballPlayer : public Person

  5. When BaseballPlayer inherits from Person, BaseballPlayer acquires the member functions and variables from Person. Additionally, BaseballPlayer defines two members of its own: m_battingAverage and m_homeRuns.

1 Like

We need inheritance in C++ in order to create variables based upon the traits of other (parent) variables. From there, these new (child) variables can acquire their own traits as well, different from the original variable traits.

A real world example of hierarchical inheritance would be musical instruments.

From instruments come wind instruments and string instruments (among others, I’ll stick to two here).

From wind instruments come saxophones, flutes, and trumpets, etc.

From string instruments come pianos, cellos, and guitars, etc.

From guitars comes acoustic, electric, and bass guitars, etc.

From acoustic guitars comes 6 string vs 12 string. Etc etc etc.

A superclass is an original class that is not derived from a parent class. Subclass is derived from a superclass.

Class Whatever

{

public:


};

A subclass can extend the functionality of a superclass by taking its member variables & functions and adding further variables and functions to work with. A subclass can access the data within the superclass.
1 Like

You can inherit classes, not variables :slight_smile:

You only defined a class, but didn’t inherit it from any other class.

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

To make programming more efficient as features from previous models can be contained when creating new or updated models without having to start from scratch

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

Subclasses Potatos and carrots are inherited from parent class vegetables

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

The subclass inherits features/attributes from the superclass

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

After the class name is declared, we add a colon, then the word public and the name of the class we want to inherit

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

By allowing superclasses to be reused in future programming rather than be discarded after the initial use

1 Like

PART i

  1. Inheritance in C++ Allows us to build upon, not only that we can use other object built upon and inherit then and new objects.

  2. Father and Son, The Son inherits the Fathers good looks.

PART ii

  1. Superclass is the class being inherited. Top of the Tree. Sub class would be the branch off of that tree

  2. class Sound { // DO STUFF HERE public: string m_chord};

  3. When a subclass extends a superclass, she gets all the functionalities of the parent class that are public or protected.

1 Like

You only defined the class, but didn’t inherit from any other class. :slight_smile:

1 Like
  1. Why do we need inheritance in C++?
    – Inheritance allows us to acquire the attributes and behavior of previous objects and as a result avoids rewriting code and it allows backward compatibility as well as new features

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    – Mammal, canine, dog

  3. What is a superclass and what is a subclass?
    – The class being inherited from is called a parent class, base class, or super class. For example a human parent. And the class receiving the attributes and behavior from the parent class is called the subclass, for example a human child.

  4. How would you write a public inheritance in C++?
    class ChildClass : public ParentClass {
    public:
    int var1:

    void doSomething() {
    }
    };

  5. In what ways can a subclass extend the functionality of a superclass?
    – A subclass inherently receives all the public or protected functionality of a superclass and in turn defines its own functionality.

1 Like

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

Parent class with different animals and child classes of Cat, Dog, Mouse…
Farm animals: > Cat, Dog, Mouse.
Aquatic animals: > Dolphin, Shark, Octopus.


In an inheritance relationship, the class being inherited from is called the parent class, or superclass.
The class doing the inheriting is called the child class, or subclass.

class ChildClassName : public SuperClassName {
…
};

Add new methods and variables or override existing methods.

1 Like

You swithced the order, this would be regarded as if the Super class inherited the Child class. :slight_smile:

1 Like

I edited the order :smile: thanks for the reply :+1: