Inheritance in C++ - Reading Assignment

Part 1
1. Why do we need inheritance in C++?
We need inheritance in C++ so we can directly acquire attributes and extend upon them. This reduces our need to replicate and allows us to reuse code.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Vegetable

  • Broccoli
  • Garlic

Part 2
1. What is a superclass and what is a subclass?
A superclass is a parent class, and a subclass is a child class
2. How would you write a public inheritance in C++?
class broccoli : public vegetables
{

};
3. In what ways can a subclass extend the functionality of a superclass?
A subclass can add new members, methods or override existing methods in the superclass.

1 Like

First Reading

  1. Why do we need inheritance in C++?
    We need inheritance in C++ because it allows us to create new objects that inherits properties from another (or other) objects. This creates efficiency, resuability.

  2. Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
    A real world example would be:
    Athlete --> Football --> Offense --> Quarterback --> Tom Brady

Second Reading

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

A superclass is also known as the “parent class,” or “base class.” This is the original class that will be drawn upon mostly for its generic attributes that can be used for more specific/detailed SUBCLASSES. These subclasses have more member variables of their own that work alongside the base class members, but the base class (aka superclass) cannot access these other member variables inside the subclass.

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

To write a public inheritance, you simple write, for example:
class BaseballPlayer : public Person

where BaseballPlayer is the subclass (child class) and Person is the parent class being used to inherit attributes to add into BaseballPlayer class.

  1. In what ways can a subclass extend the functionality of a superclass?
    You extend the functionality much like I explained in question 1. The subclass inherits the attributes of the parent class, and can thus call upon its variables/functions without having to create or repeat writing the same code again.
1 Like
  1. Why do we need inheritance in C++?*
    Inheritance has similar attributes to previous objects so you don’t have to create an entirely new object

  2. Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
    The iPhone 8 has similar features to the Iphone 7 and the apps and all the information from the Iphone 7 can be used with the new Iphones.

  3. What is a superclass and what is a subclass?
    Superclass has the components that subclasses end up using.

  4. How would you write a public inheritance in C++?
    class Food : public Ingredient {};

  5. In what ways can a subclass extend the functionality of a superclass?
    It can use the members in the superclass without having to create them again.

1 Like
  1. We need inheritance so we can associate “is-a” relationships with classes, allow new objects to inherit the attributes (variables) and behaviors (functions) of other objects before it and make it easier to code and be more specific and detailed when explaining our objects/variables.

  2. A simple example of objects in the real world that could be model in C++ using inheritance is vehicles being the parent object and planes, trains and automobile being the inheriting objects. Another example would be living spaces as the parent object and houses, apartments, basement suites and condos being the inheriting objects.

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

  4. How would you write a public inheritance in C++

     Class Employee:  public Person {
                                       .......  "variables of Employee"
                                     }
    
  5. The member functions and member variables of the superclass are automatically received through inheritance and the subclass can extend the superclass by adding new methods, new variables or override existing methods.

1 Like
  1. Why do we need inheritance in C++?
    Inheritance allows you to add member variable from a different class. This is great and convenient because it can save time because you are reusing the code that already has member variables that you may need.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    You can model for instance a Person and a Student where the person is personal information like name, age, sex and student info is student ID, major, grades, etc.

  3. What is a superclass and what is a subclass?
    A super class in made up of itself and an inherited class which is then called a subclass.

  4. How would you write a public inheritance in C++?
    class (super class name) : public (subclass name)

  5. In what ways can a subclass extend the functionality of a superclass?
    It can extend the superclass by adding additional member variables from the subclass.
    // Here are some examples of how to extend functionality of superclass by using inheritance from a subclass

#include <iostream>
#include <string>

using namespace std;

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; }

};

class BaseballPlayer : public Person
{
// In this example, we're making our members public for simplicity
public:
    double m_battingAverage{};
    int m_homeRuns{};

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

    void printBallPlayerInfo() const  //add member function to print Baseball Player and Person info
    {
        cout<<"Printing Baseball Player info using member function. printBallPlayerInfo"<<endl;
        cout <<"The name of the baseball player is  "<< m_name<< endl;
        cout<<"The age of the baseball player is :  " <<m_age<<"  years"<<endl;
        cout<<"The batting average of the baseball player is  "  <<m_battingAverage << " percent"<<endl;
        cout<<"The number of home runs of the baseball player is:  " <<m_homeRuns<<  "  homers"<<endl;

        cout<<endl;
};

};

// Add Employee class to also publicly inherit from Person
class Employee: public Person
{
public:
    double m_hourlySalary{};
    long m_employeeID{};

    Employee(double hourlySalary = 0.0, long employeeID = 0)
        : m_hourlySalary{hourlySalary}, m_employeeID{employeeID}
    {
    }

    void printEmployeeInfo() const  //add member function to print information of Employee and Person
    {
        cout<<"Print Employee info based on member function, PrintEmployeeInfo "<<endl;
        cout <<"The name of the employee is  "<< m_name << ": " << endl;
        cout<<"The hourly Salary of the employee is $"  <<m_hourlySalary << " bucks per hour"<<endl;
        cout<<"The employee ID number is:  " <<m_employeeID<<endl;
        cout<<"The age of the employee is :  " <<m_age<<endl;
        cout<<endl;
};
};

int main()
{
    cout << "Hello world!" << endl;
    cout<<endl;
    cout<<endl;

 // Create a new BaseballPlayer object
    BaseballPlayer joe{};

    // Assign it a name (we can do this directly because m_name is public)
    joe.m_name = "Joe";
    joe.m_age = 35;
    joe.m_battingAverage = 350;
    joe.m_homeRuns = 57;

    joe.printBallPlayerInfo();// can print info using member function for joe object

    // Print out the name and info independently of function
    cout<<"Print out Baseball Player info separately using member variable names"<<endl;
    std::cout <<"The name of the baseball player is  "<< joe.getName() << '\n'; // use the getName() function we've acquired from the Person base class
    cout<<"His age is  "<<joe.m_age<<endl;
    cout<<"His batting average is  "<<joe.m_battingAverage<<endl;
    cout<<"His number of home runs is  "<<joe.m_homeRuns<<endl;
    cout<<endl;

Employee janet{};  //create object "janet" and initialize

janet.m_name = "Janet";
janet.m_age = 45;
janet.m_employeeID = 345555;
janet.m_hourlySalary = 120.00;

cout<<endl;
cout<<endl;

janet.printEmployeeInfo();//print info for Employee and person using member function

cout<<endl;

//Print Employee info individually using member variables
cout<<"Print out Employee info separately using member variable names"<<endl;
cout<<"The name of the Employee is  "<<janet.m_name<<endl;
cout<<"The age of the employee is  "<<janet.m_age<<endl;
cout<<"The ID of the employee is   "<<janet.m_employeeID<<endl;
cout<<"The hourly salary of the employee is $"<<janet.m_hourlySalary<<"  dollars per hour" <<endl;
cout<<endl;

Employee imhotep{150.50,45789010}; //create Employee "Imhotep" an use list initialization

imhotep.m_name="Imhotep";  //initialize imhotep name
imhotep.m_age = 62;        //initalize imhotep age

imhotep.printEmployeeInfo(); //print Employee info using member function






return 0;

}
1 Like
  1. So that we are able to build on classes and create multilayer more specific data structures.

  2. The 5 mother sauces of french cooking are the parent class for all sauces in european cooking. Any sauce created will fall into a child class of the parent.

  3. A superclass is a parent class, the inheritor,
    and a subclass is a child, the inheritee.

  4. class Child: public: Parent {}

  5. They can contain more specific information while utilizing all of the base information that comes from the parent. This allows for less definitions in the superclass making the code a bit more clean.

1 Like
  1. 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. bananas and apples inherit from fruits! (or trees, idk)


1.the superclass is the class being inherited from the parent class, the subclass is the class doing the inheriting, the child class.

  1. class banana: public fruit {

    }

  2. The subclass can add new methods and new variables or override existing methods.

1 Like
  1. Inheritance involves creating new objects by directly acquiring the attributes and behaviours of other objects and then extending or specializing them.
  1. Species, Animals etc.
  2. Superclass is a parent class and subclass is a child class that inherits functions and variables from a superclass.
  3. class BaseballPlayer : public Person {}
    child class : parent class
  4. Subclass inherits functions and variables from a superclass.
1 Like
  1. For backwards compatibility and for creation of new objects using their
    attributes and behaviors

  2. granny smith --> apple —> fruit --> flora —> living being

  3. superclass is the class that an object inherits traits from, but a subclass is
    the class that inherits these traits

  4. write class NameofClass: public ClasstoInheritFrom

  5. A subclass can inherit traits from another subclass that is derived
    from a superclass, allowing us to automatically receieve the member
    functions and member variables of the base class

This lesson has lines of code that I haven’t seen before, can somebody help me to understand them?



what is the purpose of these objects included after the function and the colon?

1 Like

1. Why do we need inheritance in C++?
It’s great for “is a” relationships between classes. It also allows us to reuse classes and it saves coding.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Vehicle -> Car -> Volvo -> XC90
1. What is a superclass and what is a subclass?
The superclass is “above” the subclass in the hierarchy. The subclass inherits members and methods from the superclass.
2. How would you write a public inheritance in C++?
Class Car: public Volvo {...}
3. In what ways can a subclass extend the functionality of a superclass?
It has access to the functions/methods of the superclass. The subclass can reuse them, like: Subclass::superclassMethod();

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:

Here you defined a new class Car that inherits from Volvo. I think it should be the other way around. :wink:

  1. Why do we need inheritance in C++?
    Inheritance is a method that allows us to create model classes based on the specific traits that objects have. These traits are common among all of the object instances in that class.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Different vehicle types all have different traits but have similarities including fuel, weight, colour, milage etc.

  3. What is a superclass and what is a subclass?
    A subclass is a class that inherits properties from another class. A superclass is the class in which the properties are inherited from.

  4. How would you write a public inheritance in C++?
    class subclass : public superclass {
    public or private:
    subclass info {};
    };

  5. In what ways can a subclass extend the functionality of a superclass?
    In using subclasses we can reduce repeatability as we can use the characteristics of the superclass in all subclasses reducing the amount of code required to write. If additions or changes need to be made to superclass variables then we can alter the superclass and not all the subclasses.

1 Like

Reading 1

  1. Why do we need inheritance in C++?
    We need inheritance to model hierarchical relationships where one object derives its attributes from a more general object. Inheritance also facilitates the creation of complex objects through carrying all attributes from one object over to a newly created object. Inheritance also saves time and effort in maintaining and changing classes. All changes in a parent class will automatically be passed on to its child classes.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Example:
    Buildings — High-rise — Commercial … (etc.)
    — Residential … (etc.)
    — Low-rise — Commercial … (etc.)
    — Residential …(etc.)

Reading 2

  1. What is a superclass and what is a subclass?
    A superclass is a class from which another class inherits some variables and functions. The class which inherits the variables and functions is the subclass of the superclass.

  2. How would you write a public inheritance in C++?
    Public inheritance is declared by adding a colon and the name of the parent class after the declaration statement of the child class –
    class <child class> : public <parent class>

  3. In what ways can a subclass extend the functionality of a superclass?
    A subclass can extend the functionality of its superclass by adding new variables and functions in the subclass’s own declaration.

1 Like

Part 1:
1 - Inheritance is the creation of new objects that contain the attributes and behaviours of other objects, from here the inherited object is able to make use of these variables and functions and extend their use beyond the scope of the object from which they were derived.
2 - An example of where inheritance can be useful is when creating payrolls. Wage, Hours Worked, Tax to Pay, Bank Details etc could all inherit from the class Employee, as each employee for a company will all have their individual wage, hours, tax and bank details.

Part 2:
1 - The class that is being inherited from is called the superclass, and the class that is doing the inheriting is called the subclass.
2 -
Class Tesla:
public Cars
{
//Class elements
};

3 - In the example above, the Tesla class will inherit both member functions and member variables from the Cars class. This allows the Tesla class to both contain inherited member functions and variables as well its own native functions and variable.

1 Like

Part 1

  1. We need inheritance in order to automatically receive the member functions and member variables of the base class and then simply add the additional functions or member variables we want in our derived classes. This not only saves work, but also means that if we ever update or modify the base class all of our derived classes will automatically inherit the changes!

  2. For example we can have 3 classes of vehicles Car, Bass, Truck and use inheritance with a base class Vehicle Service, write three properties in it (check Oils, Breaks, Battery) and inherit the rest of the classes as common to them.

Part 2

  1. The class whose the member functions and variables are inherited by sub class is called Super class and
    the class that inherits properties from another class is called Sub class

  2. To write a public inheritance, after the class name declaration we use the word “public”, and the name of the class we wish to inherit. :
    Class schoolstudents : public Person

  3. A subclass can extend the functionality of a superclass by adding new methods and new member variables.

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. It is useful to build hierarchies which removes the need to redefine the attributes and behaviors in the class that inherited them.

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

Vehicules. Different vehicules range from cars to planes. They inherit the same basic caracteristics which is to carry people of object from one point to another.

----------------------------------------------------

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 the class ObjectName declaration, we use a colon, the word “public”, and the name of the class we wish to inherit.

3. In what ways can a subclass extend the functionality of a superclass?
A subclass extends the functionality of a superclass by acquiring the functions and variables of the superclass and adding the additional functions or member variables we want.

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

Inheritance is super useful to use of members of an already existing class. So if we create a class that can also make good use of members from an already existing class, but also has it’s own specific members, we can use inheritance.

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

F. e. We have a class called people. Here we will store and process all data that apply to all people, like name, age, height, nationality, and so on…
Then we can have an additional classes for profession and religion.
In this additional classes we maybe can store and process data that, besides name, age, height, nationality, also take into account peoples jobs, or peoples religious beliefs.

By inheriting the peoples class into the profession and religion class, we can save code, only add the specific type of information or processing that is needed and also lay a good foundation for making adjustments to our project with minimum effort.

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

A superclass, or base class or parent class, is the class we inherit data structure from.

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

class JobClass: public People //Inheriting the People Class to JobClass{
//DoStuffHere
}

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

With subclasses, we can add specific information or processes, that we only want to apply to specific data structure. For example: People class stores age, name, height,… Then in JobClass we inherit the People information and add more specific data like Profession, Education, Years in Profession, and so on…

1 Like

P1

  1. Inheritance enables being able to reuse properties of previous objects in a new one, not having to re-define everything.
  2. Vehicle>Truck

P2

  1. The superclass is the parent class and the subclass is the child one.
  2. In the child class definition, after its name, put “: parentClassName”.
  3. Adding more variables and functions.
1 Like

If you’re defining a public inheritance you must also specify it as public like : public parentClassName. :slight_smile: