Inheritance in C++ - Reading Assignment

1. Why do we need inheritance in C++?
It allows to build classes built upon existing classes, to reuse code of the parent class and extend on the existing methods and members.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Parent class: Vehicle
Child class: car or truck
Car and truck can have different members and methods since they have different purposes (person vs cargo transfer) but they both inherit from vehicle.

1. What is a superclass and what is a subclass?
A subclass or child class intherits from a superclass or parent class
2. How would you write a public inheritance in C++?
After class declaration, use colon, the word public followed by the name of the class to inherit from
class ChildClass : public ParentClass
3. In what ways can a subclass extend the functionality of a superclass?
A subclass inherits all methods and members of the superclass, it can then add methods and members specific to itself, thus making it an extension of the superclass

1 Like
  1. Inheritance allows us to simplify our code and make it easier to maintain by reducing redundancies in objects.

  2. Hats - All hats are worn on your head, but have many different styles.

========================

  1. A superclass is a class that is the parent or the more generic object. Subclasses inherit from superclasses and add on to them for specialization.

  2. class Architect : public Person

  3. A subclass could add additional variables to describe the object more specifically than the superclass. The subclass could also use specialized methods that would not be appropriate to put into the the superclass.

1 Like

PART 1

  1. We need inheritance in C++ when we want to create a new class that, in addition to its own unique
    members, inherits also the members of an already existent class.

  2. LivingBeing <- Animal <- Horse
    <- Dog
    <- Plant <- Pine
    <- Oak

PART 2

  1. A superclass is the already existent class from which the new derived class inherits the members.

  2. class SubclassName: public SuperclassName

  3. A subclass, in addition to inheriting the members of its superclass, can also have its own members.
    Therefore the subclass can extend the functionality of a superclass by adding new variables and
    functions to those inherited from the superclass.

1 Like

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

  • With inheritance we can define new classes in terms of other classes. We don’t have to rewrite functionalities we already have because we can reuse members and functions of existing classes.

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

  • Covid 19 and its mutations

Second Part

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

  • A superclass is the class a subclass is derived from.

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

class ClassName:public SuperClassName{
};

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

  • A subclass inherits all members and functions from its superclass but also adds its own members and functionality .
2 Likes

1. Why do we need inheritance in C++?
this enables us to write more complex code, avoid duplication which in turn leads to cleaner and clearer code

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

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

class Mammal{

private:

protected:
    string species ;
    string name;

public:

    Mammal(string s, string n ){
        cout << "Construction object mammal" << endl;
        species = s;
        name = n;
    };

void printInfo(){
     cout << "Species: " << species << endl;
     cout << "Name of the animal: " << name << endl;

     };

};

class Dog : public Mammal{

public:
    string passion;

    Dog(string s, string n) : Mammal(s, n){
        passion = "get sticks";
    };

void printDogsInfo(){
    cout << "The passion of the dog is: " << passion << endl;
    }

};

int main()
{
Dog wuffi = Dog(“Dog”, “Wuffi”);
wuffi.printInfo();
wuffi.printDogsInfo();

return 0;

}


#include

using namespace std;

class Mammal{

private:
string species;
bool herbivores;
bool carnivores;

public:

Mammal(string s, bool h, bool c){
 cout << "Constructer object: " << endl;
    species = s;
    herbivores = h;
    carnivores = c;
};

void printMammalInfo(){
cout << "Species: " << species << endl;
cout << "Herbivores: " << herbivores << endl;
cout << "Carnivores: " << carnivores << endl;
};
};

class Lion : public Mammal{

public:
Lion(string s, bool h, bool c) : Mammal(s, h, c){
};

};

int main()
{
Lion lion = Lion(“Lion”, false, true);
lion.printMammalInfo();

return 0;

}

  1. What is a superclass and what is a subclass?
    Superclass (Base Class ) & Subclass (Derived Class ): In OOP, we could organize classes in hierarchy to avoid redundancy. We can extend a subclass (or derived class ) from a superclass (or base class ). The subclass inherits the members of the superclass , known as inheritance

  2. In what ways can a subclass extend the functionality of a superclass?
    The subclass inherits members and methods of the superclass and can additionally initiate own methods and members

1 Like

1. Why do we need inheritance in C++?
Whenever something has a “is a” relationship it can be useful to use Inheritance because it allows you to save work not having to redefine variables or functions in the derived class. ex: a person IS An employee, a Doctor IS A person.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Person --> Employee --> Supervisor. a supervisor could inherit a function from employee.

1. What is a superclass and what is a subclass?
The Class Being inherited from is the Superclass.
The class doing the inheriting is the subclass

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 could add new variables or functions, using variables from the superclass and the subclass.

1 Like

1. Why do we need inheritance in C++? Inheritance is important type of object composition allowing us to build “is-a” relationships between objects.
It enables us to improve reusability and maintainability
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
NBA Teams
BULLS, CAVS, KNICKS, BLAZERS, LAKERS,

1. What is a superclass and what is a subclass? superclass is the class which is closest to the root OR be called the parent class. The subclass in contrast is the offspring of the superclass.
2. How would you write a public inheritance in C++?
class superlassName : public childClassName
3. In what ways can a subclass extend the functionality of a superclass?
It doesn’t have to redefine the information from the base class.
• automatically receives the member functions and member variables.
• can add additional functions or member variables
• the superclass is updated, all its children will be updated automatically.

1 Like
  1. Inheritance allows new classes to inherit properties of already existing classes. This allows for for relationships between classes that allow for more sustainable and easier to maintain code.

  2. Lets say you are asking users to fill in a profile, if they select student that could be a class then primary, secondary, graduate students could all be classes that inherit properties form the student class.

  3. A super class is the original or parent class in which all classes below it(sub classes) inherit the properties of.

  4. class newClass: public oldClass{};
    3.Subclasses allow for additional data to be added using the members of the superclass. The extend the functionality of super classes because now any change in the members or methods of the super class will exhibit those changes accordingly to all sub classes below it functioning similair to that of using a master key to make changes to all classes below it.

1 Like

1. Why do we need inheritance in C++?
efficiency, organization

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
vehicle -> car -> brand -> model -> trim

1. What is a superclass and what is a subclass?
superclass is a parent class while subclass is it’s child class

2. How would you write a public inheritance in C++?
class SubClassName: public ParentClassName

3. In what ways can a subclass extend the functionality of a superclass?
by adding its owns members and functions in addition to what it inherits

1 Like

Section 1:
1.Inheritance is when you create a new object from attributes of other objects.
2. A real world example of inheritance is that all fruits are inherited a color, size, and are grown. Apples and oranges inherit from fruits.
Section 2:

  1. The class that is doing the inheriting is the subclass and the class being inherited from is the superclass.
  2. class Rockstar: public Musician {
    };
  3. A subclass can extend a superclass by overriding exisitng methods and adding new methods and members.
1 Like
  1. inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them
    2.For example, the Intel Pentium processor inherited many of the features defined by the Intel 486 processor, which itself inherited features from earlier processors. C++ inherited many features from C, the language upon which it is based, and C inherited many of its features from the programming languages that came before it.

  2. 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 cat: public animal{
    };
    3.it get all the functionality form the class it is referred to.

1 Like

1.1 If we want properties from 1 object in an other.
1.2. Birds and Cats inherits animals

2.1. A superclass is the parent class;
A subclass is the child class of the parent class.
2.2. class Birds: public: Animals{
}
2.3. It can provide more details and new functions for the superclass.

1 Like

Inheritance introduction

  1. We need inheritance to make coding modular and build reusable code. It allows us to access member of higher classes without having to rewrite code for similar members.
  2. Vehicles -> cars, planes, trains -> vehicle models e.g. Cesnar, Toyota etc.

Inheritance basics

  1. A superclass is a parent class whereas a sub-class is a child class of the parent class
  2. class SubClassName: public ParentClassName {};
  3. A subclass has access to members of a superclass and can add its own member functions.
1 Like
  1. Inheritance allows the creation of new objects by directly acquiring the attributes and behaviors of other objects and then extending and specializing them.

  2. Sports as the parent class then hockey, basketball, golf, etc as sub classes.


  1. A superclass is a class passing inheritance to its subclasses. A subclass receives inheritance from its superclass.

  2. class SubClass: public SuperClass {
    }

  3. It can add its own members and functions in addition to the ones it has already inherited previously.

1 Like
  1. For creating new objects who acquired the attributes and behaviors of other objects and then extending or specializing them.

  2. scorpion, fly, cockroach, bee can be the inheritance of insect.

  3. A superclass is the the class who give the inheritance to another class and subclass is the inheriting class.
    2.Class scorpion { Public:
    string specie;
    int legs;
    string sex; };
    3.It can do it by making a public statement and writing in it the name of the class we want to inherit.

1 Like
  1. Inheritance allows us to make similar but differentiated classes without remaking the same code.
  2. Plants (oaks and maples are deciduous, pines are coniferous, while both kinds are trees).

Next Section

  1. A superclass is the class that a subclass inherits from. It is the more general, while the subclass is more specific.
  2. After declaring the subclass, type a colon, the word public, and the name of the superclass we wish to inherit from.
  3. A subclass can extend functionality by adding new variables and new functions/methods.
1 Like
  1. Inheritance makes it easier to define relationships between objects.
  2. Car > BMW > M3 or Animal > Primate > Chimp

  1. A superclass enables a subclass to inherit its properties and methods.
  2. Inheritance works like: class Superclass : public Subclass
  3. A subclass can extend functionality by adding its own properties and methods.
1 Like

Does anyone know how to enable autocomplete in Code::Blocks? Right now, I can’t get the editor to help me complete variable names or enter properties at all. I have to type everything out. A common solution online is to check the option Disable Smartsense which I found under Settings > Editor Settings > Code completion. This option is greyed out for me though.

I’m not familiar with codeblocks unfortunately so I can’t help you. You can try a different editor or check online why the option is greyed out. :slight_smile:

1 Like

Part I

  1. We need inheritance in C++ because:
  • it helps in reusability of the code
  • a lot of time and efforts are being saved
  • it improves the program structure which can be readable
  • the program structure is short and concise which is more reliable
  1. Trucks, Cars, Buses can inherit from Vehicles.

Part II

  1. Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
    Super Class: The class whose properties are inherited by sub class is called Base Class or Super class.

// base class
class Vehicle {
  public:
    Vehicle()
    {cout << "This is a Vehicle" << endl;}
};
// sub class derived from a single base classes
class Car: public Vehicle{
};
  1. A subclass inherits all the functions and variables from the superclass. It can then also define its own functions and variables.
1 Like