Inheritance in C++ - Reading Assignment

  1. “Inheritance in C++ takes place between classes. Inheritance allows us to reuse classes by having other classes inherit their members. Inheritance involves creating new objects by directly acquiring the attributes and behaviours of other objects and then extending or specializing them.”

  2. Bicycles could be a set of real world objects that could be modelled using C++.

  3. “In an inheritance 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.”

#include <iostream>

using namespace std;

class Bicycle{

    private:

        string domain;
        string ageRange;

    public:

        Bicycle(string d,string a){
            domain=d;
            ageRange=a;
        }

        void printInfo(){
            cout<<"Bicycle's domain is "<<domain<<endl;
            cout<<"Bicycle's age range is "<<ageRange<<endl;
        }
};

class Specification: public Bicycle{

    private:

        int wheelSize;
        string colour;

    public:

        Specification(string d,string a,int w,string c):Bicycle(d,a){
            wheelSize=w;
            colour=c;
        }

        void printSpec(){
            cout<<"Bicycle's wheel size is "<<wheelSize<<" inch"<<endl;
            cout<<"Bicycle's colour is "<<colour<<endl;
        }
};

int main()
{
    Specification x=Specification("offroad","adult",29,"green");
    x.printInfo();
    x.printSpec();

    return 0;
}

  1. “Inheriting from a base class means we don’t have to redefine the information from the base class in our derived classes. 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. This not only saves work, but also means that 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!”
1 Like

Answers of the First Article:
1. Inheritance allows us to have a hierarchy of classes which relate to each other. This allow us to avoid code duplication.
2. Birds could be the base class, and the sub classes can be parrots and canaries.

Answers of the Second Article:
1. The subclass is the class that inherits from the superclass.
2. class parrot : public Bird
{
...
};
3. When a subclass extends a superclass, it gets all the functionalities of the parent class that are public or protected.

1 Like

Article 1

  1. Because it enables the programmer to create new objects with inherited attributes from previous objects and extend or specialize them.

  2. A car and a truck are both vehicles. Therefore, whatever universal vehicle attributes are also true about cars and trucks.

Adding to question number 1, we need inheritance because this way we don’t have to redefine information from base classes in the derived classes, which saves work and makes updating classes much easier.

Article 2

  1. A superclass is the parent class, and the subclass is the child class, or the class that derives the attributes from the parent class.

  2. E.g., class Banana : public Fruit {etc. etc. etc. };

  3. It can be used to add specific attributes that are not applicable to all members of the parent class. E.g., number of home runs is applicable to baseball players, but not to soccer players; home runs are not applicable to all athletes nor persons.

1 Like
  1. Inheritance allows us to organize classes into more generic “parent” ones and more specific “child” ones (also called subclasses) which (apart from their specific members) also gain automatically all of the properties of their parents.
    This is very useful when a class must have a “is a” relationship with another class.

  2. Creature, cat, siamese
    Creature, cat, bengal
    Creature, human
    They all inherit “creature” properties, the first two examples also inherit “cat” properties.


  1. Superclasses are parent classes, while subclasses are child classes which inherit all the properties (variables and methods) of their parents (and grandparents, and so on).
class MySubClass public : MySuperClass {
//members of the subclass
};
  1. Subclasses inherit all the functionalities of their parents, in addition they have their own specific functionalities.
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.

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

1. What is a superclass and what is a subclass?
The superclass is when you rock :slight_smile: no 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++?
class Cat : public Animal
{
...
};

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.

1 Like

Questions:
. Inheritance is needed so that new objects can take properties from existing objects.
. Examples of electronic applications include, televisions, computers and phones.

. The super class is the class inherited from the parent class, while the sub class is what carries out the inheritance.
. class person {
public:
};
. The functionality is reset to correspond to that of the new function which goes to the sub class.

1 Like

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

So that groups of related classes can have their related properties abstracted out into parent classes.
Inheritance allows child classes to inherit members from their parent classes, hence eliminating the need for re-implementing them.

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

A motor vehicle class could be the parent class from which a motorcycle, car , and truck class inherit certain properties.

The truck class could further have child classes SemiTruck and PickupTruck which not only inherit from their parent truck class but from its parent MotorVehicle class.

1. What is a superclass and what is a subclass?
A subclass is a class that inherits member variables and methods from its super class or parent class.
A parent class is the class from which the child or sub class inherits properties from.
There can be multiple levels in the class hierarchy with each super class having another super class and so on.

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

As follows…

class ChildClass : public ParentClass
{
// class body for ChildClass here
}

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

A subclass can add more specific properties via its own member variables and more specific behavior via its own methods.

For e.g. if a BirdClass inherits from a super class called Animal, the BirdClass could implement its own method called fly() which is a behavior specific to all birds.

1 Like
  1. It allows for the construction of complex classes, as well as acquiring specialized behaviors and attributes to allow for the creation of such classes within programs.
  2. ComputerOS:
  • MacOS
  • Linux
  • Windows
    PT.2
  1. Superclass is the class that another class is inheriting from, and the subclass is the class who is doing the inheriting from the superclass.

class subClass : public superClass {
……………….
……
………};
3. By allowing for access to classes through inheritance, it makes the practical functionality of the class easier to build because of past and new functions, members and variables working efficiently together.

1 Like

1. Why do we need inheritance in C++?
By inheriting we gain access to other properties of other objects and do not have to redefine base properties. Thus they are inherited.

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

1. What is a superclass and what is a subclass?
Superclass is parent class and sub class is child class which inherits from superclass.
2. How would you write a public inheritance in C++?
class Employee : public Person{
};
3. In what ways can a subclass extend the functionality of a superclass?
A subclass will inherit the functionality from the superclass then define its own or can declare new functions over old.

1 Like

Part 1

  1. Inheritance saves work by not having to redefine information. If we ever need to add new information to a superclass, it will automatically be added to all subclasses.

  2. Tools <- Saws <- Reciprocating saw, Circular saw, Table saw
    Tools <- Hammers <- Framing hammer, Ball peen hammer, Dead blow hammer

Part 2

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

class SubClass : public SuperClass{

};

  1. Subclasses extend the functionality of superclasses because they inherit all variables and methods from their superclass and also add additional variables and methods that make them even more specific.
1 Like

1. Why do we need inheritance in C++?
So we can create new objects using the attributes of other objects without writing a ton of new code.

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

1. What is a superclass and what is a subclass?
A subclass is a class that inherits variables and functions from another class, the superclass.

2. How would you write a public inheritance in C++?
class Subclass: public Superclass{
};

3. In what ways can a subclass extend the functionality of a superclass?
Automatically inherit variables and functions from the superclass and add new variables and functions of its own.

1 Like

Part One

1. Why do we need inheritance in C++?
Inheritance in C++ allows for classes to derive member functions and variables (depending on access restrictions) from parent classes and to pass these same or different members to their own child classes.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
German and Mongolian both inherit from the parent class Langauge.

Part Two

1. What is a superclass and what is a subclass?
Superclass is the class being inherited from, whose members pass down to subclasses which are inheriting.

2. How would you write a public inheritance in C++?
//Child class inherits from Parent class
class Child : public Parent

3. In what ways can a subclass extend the functionality of a superclass?
Because they inherist members from the superclass but can add their own members also, in turn becomming a superclass to another sub class creating an inheritcance chain which becomes increasingly specific and allows for reusability.

1 Like
  1. Inheritance allows the construction of is-a relationships, helps eliminate code redundancy, allows for hierarchical relationships and facilitates polymorphism.
  2. Boats > Cargo ship > Tanker > Container ship
    > Submarine
  1. A superclass provides the inheritance. A subclass receives the inheritance.
Class Submarine: public Boat {
public/private:
Submarine{details here};
}
  1. A subclass allows for the nuanced addition of variables and functions without having to alter the superclass.
1 Like

1. Why do we need inheritance in C++?
it allows the child class to acquire the properties (the data members) and functionality (the member functions) of parent class.
so this, will save a lot of work! with inheritance 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. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.

root: vehicle
A. 4 wheels:
1. truck
2. automobile
3. SUV
B. 2 wheels:
1. motorcycle
2. bicycle
3. scooter

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


class Truck : public Vehicle {
       public: 
                int numberOfWheels{};
                string manufacturer{};
                string engine{};
                Truck(int n, string m, string e) {
                      numberOfWheels = n;
                      manufacturer = m;
                      engine = e;
                }
}

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

a subclass cannot by any means EXTEND the functionality of a superclass. It is the subclass that takes advantage of the inheritance that it receives from the superclass to extend the functionality of the superclass, but it is the subclass that ends up with extended functionality

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.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
A vehicle can be the base class for a car a truck etc…

1. What is a superclass and what is a subclass?
A superclass is the class you are inheriting from and a subclass is the class that inhertits from the superclass. In the previous example vehicle is the superclass and car and truck are the subclasses
2. How would you write a public inheritance in C++?

// class ClassName: public SuperClassName
class Car: public Vehicle

3. In what ways can a subclass extend the functionality of a superclass?
It adds properties and methods specific to the subclass. For example a Vehicle superclass have generic info about a vehicle like number of wheels and Truck subclass might have info about the payload

1 Like
  1. Why do we need inheritance in C++?
    Inheritance involves creating new objects by directly acquiring attributes and behaviors from other objects and then extends and specialize them.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Brocolli and Celery inherits from Vegetables

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

Superclass is the class where the attributes were being inherited from (parent class) while a subclass is the class doing the inheriting ( child class).

  1. How would you write a public inheritance in C++?
    class Brocolli : public Vegetable
    {

    };

  2. In what ways can a subclass extend the functionality of a superclass?
    add new methods and variables

1 Like
  1. We need inheritance in C++ because it greatly simplifies code, reduces time to write said code, and allows for more diverse classes.

  2. A simple example could be cars. Have a superclass named “Car” and have subclasses all determining different characteristics of the cars like year, make, model, horsepower, suspension type, safety rating, etc.

  3. A super class is the main class with shared attributes, variables, code that can be accessed by all other subclasses. A subclass is a class with its own specific characteristics and the variables of the superclass.

  4. You would write a public inheritance like this: class horsepower : public car

  5. A subclass allows you to have a variety of different characteristics that add on to the variables of the superclass.

1 Like

Horsepower would be a property inside a class, not a new class in itself. A better example of an inherited class would be a car brand. :slight_smile: