Inheritance in C++ - Reading Assignment

Same you inherited Person from an Employee, but I guess an Employee could also be a Dog :smile:

1 Like

Dogs are absolutely employees. They are in fact more efficient than persons :stuck_out_tongue: .

Thanks for pointing out this obvious error though. :stuck_out_tongue: It’s getting late here, better relax and not mix up more basic stuff. :stuck_out_tongue:

1 Like

:trophy: :dog: :sweat_smile:

1 Like

1 We can create objects using a class, and by inheritance we can create a subclass where all objects must have the properyies difined by the class and some extra features.

2 If we create a class “Vertebrates” we can use inheritance to create the sub class “Mammals”.

1 Vertebrates is the superclass to Mammals and Mammals is a suclass to Vertebrates.

2 class Mammal : public Vertebrate{ // add features and/or methods }

3 The extension adds members to the object, perhaps even new metods. We also save a lot of code by recycling.

1 Like

1- Creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them. It allows us to reuse code and then expanding on them if needed.
2- Meat - Chicken / Beef/ Pork / Lamb

1 - A superclass is a class that has been extended by another class. A subclass inherit its state and behaviors from the superclass.

2 - class BaseballPlayer : public Person
{
public:
double m_battingAverage{};
int m_homeRuns{};

BaseballPlayer(double battingAverage = 0.0, int homeRuns = 0)
   : m_battingAverage{battingAverage}, m_homeRuns{homeRuns}
{
}

};
3 - They can inherit member functions and member variables from the superclass. They can also extend and add to it.

1 Like

PART 1

  1. Why do we need inheritance in C++?
    Inheritance allows us to create objects that inherit properties from other objects, this means there will be greater efficiency in the code that is written, with less duplication. Inheritance creates the “is-a” relationship between two objects.

  2. Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
    A dog is mammal but not all mammals are dogs. So the class Dog can inherit and be a subclass (or child class) of class Mammal, which would be the superclass (or parent class).

PART 2

  1. What is a superclass and what is a subclass?
    The superclass (or parent class) has the smaller set of attributes, that are inherited.
    The subclass (or child class) inherits the attributes of the superclass and then has additional attributes that extend or specialise them. So the superclass is a more generic classification and the subclass is a more specific classification.
    superclass Mammals, subclass Dogs
    Mammals are warm blooded, so class Dogs inherits that but Dogs all have a form of tail (I think, whether docked or left full), whereas not all Mammals have tails (eg humans).

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

class ChildClass: public ParentClass { 
//class properties and methods here
};
class Dog : public Mammal {
// class properties and methods here
};
  1. In what ways can a subclass extend the functionality of a superclass?

A subclass inherits all the properties and methods from its superclass, making them its own, but then a subclass can also have its own properties and methods. It can make it much more specific, rather than general.

1 Like

Part 1:

1. Why do we need inheritance in C++?
It allows us to create related objects to the main class which can have its own functions as well as inherit them. It gives the ability to create more focused and specific information in relation to a broader category.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
A food group pyramid would be a good example of this:
Food <
a) Bread/Cereals < a1) Oatmeal, a2) Whole Wheat
b) Fruits/Vegetables < b1) Apples, b2) Carrots
c) Meat/Dairy < c1)Beef, c2) Cheese
d) Fat/Oils < d1) Lard, Olive Oil

Part 2:

1. What is a superclass and what is a subclass?
The superclass is the a base class that has nothing inherited before it, and a subclass is a class that inherits a preceding class’ variables and methods.

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?
You can add more functionality and information to expand upon the inherited methods and variables.

1 Like
  1. We need inheritance in c++ to acquire member attributes and functions (or methods) from other objects in a direct manner. Once acquired, we can extend and specialize our new objects that descend from their parent classes.

  2. Musical Instruments -> Horns -> Saxophones -

Tenor Sax

  1. A superclass is also called a parent class and it’s the class that gets inherited from. The subclass (or child class) receives the inherited attributes and methods from the superclass.

  2. class Superhero : : public Batman
    {

    };

  3. A subclass extends on the functionality of it’s superclass by adding member variables and functions of its own. In this way, classes that extend from superclasses become more specific and specialized and incorporate an is-a relationship with the more general superclasses they descend and adhere from.

1 Like

You switched the order. This would be read as if Superhero inherits from Batman :slight_smile:

Oops!!! I knew that!

class Batman :: public Superhero
{

};

Also only one colon… my bad.

class Batman : public Superhero
{
···
};

1 Like

Ahhh, yes I didn’t notice that error :smiley:

1.) We need inheritance in C++ because it allows us to acquire information that was passed from previously objects rather than combining and connecting other objects. Inheritance allows us to create new objects with attributes and similarities of previous objects with extend capability of them by modifying and specializing their behaviors.
2.Aircraft are an example of an object that can be modelled in C++ using inheritance.

1.) Superclass is the class being inherited from in the inheritance relationship in c++. Superclass is synonymous with parent class and base class. A subclass in the class that inherits the specific attributes and is synonymous with child class and derived class.
2.)By declaring a class followed with Public for examle. class Bus : Public Automobile {…}
3.) The subclass extends the functionality of a superclass by inheriting attributes from the superclass and then adding additional functionality or member variables with the use of the subclass. This saves work and allows any update or modification to the baseclass to be simultaneously broadcasted to the derived classes in the event of a bug fix or simply modification.

1 Like

Why do we need inheritance in C++?

inheritance models an “is-a” relationship between two objects. inheritance involves creating 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 modelled in C++ using inheritance.

A childs genome sequence and behavior.


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 superclass, and the class doing the inheriting is called the subclass.

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

Here’s a simple class to represent a generic person:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include

class Person
{
// In this example, we’re making our members public for simplicity
public:
std::string m_name{};
int m_age{};

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

const std::string& getName() const { return m_name; }
int getAge() const { return m_age; }

};

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

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

You have only defined a class Person but didn’t inherit it from any class. :slight_smile:

1 Inheritance makes it possible to re-use code. You can make re-use of the properties of other classes higher up in the hierarchy.
2 fruits - apples -granny smith

1 Superclass is the class higher up in the hierarchy (base class). Subclass is a class that uses the properties of a class higher up in the hierarchy.
2 class BaseballPlayer : public Person {};
3 By inheriting all functionality from the superclass and then add members and methods

1 Like

Part 1:

Why do we need inheritance in C++?
So we can have one more type of relationships between data.

Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
In a task management tool you might have one type of task that could be inherited by different people who take the task.

Part 2:

What is a superclass and what is a subclass?
Superclass = The class closest to the root.
Subclass is the child of the superclass (which is the parent class)

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

  • class MyTask : public MySuperTask{ //This is at the start of the class definition.

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

  • They can inherit the superclasses capabilities and yet include more methods and variables in order to go beyond what went before.
1 Like

Why do we need inheritance in C++?
there are several reasons

  • To avoid repeat code in every class
  • To save time programming
  • To make programs more abstracts and easier to read
  • its very important to reuse code
  • If we find a bug in a class and fixed it all sub classes are benefited for this

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

general class = videogames consoles
subclass = xbox consoles
subclass = playstation consoles
subclass = Nintendo consoles

What is a superclass and what is a subclass?
A superclass is generally the baseclass in other words the more general class

How would you write a public inheritance in C++?
class Nameclass : public NameMoreGeneralClass
{
//variables, functions(methods) and constructor
}

In what ways can a subclass extend the functionality of a superclass?
we can use every variables, functions(methods) and constructor from the superclass to the subclass

1 Like

PART A:

  1. Inheritance is one of the two ways to construct complex classes.
  2. Fruit is an example of objects in the real world that could be modeled in C++ using inheritance.

PART B:

  1. " 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 ." - https://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/
  2. Class declaration followed by “Public:” then the name of the class to be inherited.
  3. Subclasses can define members of their own to be added on to the total members of the superclass.
1 Like

Part 1:

  1. Inheritance models an “is-a” relationship between two objects. Inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them. This helps reduce code derived from variable properties and makes code more efficient and maintainable in C++.

  2. Smart Phone>OS>Manufacturer>Screen Size>charge port type

Part 2:

  1. The class being inherited from is called a superclass. The class doing the inheriting is called the subclass.

  2. Class SmartPhone
    {
    Public:
    //inheritance variables and functions
    }

  3. Subclasses can inherit superclass attributes and behaviours and therefore, have subclasses attached to it.

1 Like