Inheritance in C++ - Reading Assignment

Part 1:

  1. Using inheritance allows us to not have to redefine info that we’ve already defined inside another class. This saves time/work, and also allows for inheritance-chain wide updates to be made a lot easier.
  2. Superclass: Appliances, Subclasses: Refrigerator, Oven, Microwave, etc.

Part 2:

  1. A superclass is a parent class, it is the class that subclasses inherit from. A subclass is a child class, the class that does the inheriting.
  2. class Refrigerator : public Appliance
  3. A subclass inherits the built in functionality of its superclass, but it can also have its own unique data members and member functions, thus extending the capabilities of the parent class.
1 Like
  1. Why do we need inheritance in C++?
    Inheritance allows us to directly acquire the attributes and behaviors of other objects and then extending or specializing them.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    CEO, manager, janitor, can be inheritance of workers at a given company.

1 Like
  1. What is a superclass and what is a subclass?
    The class being inherited from.

  2. How would you write a public inheritance in C++?
    By declaring a class, the use of a colon, the world public and the name of the class we with to inherit.

  3. In what ways can a subclass extend the functionality of a superclass?
    Within the subclass, we can add additional member functions and member variables on top of the inherited from the superclass.

1 Like
  1. To create less code for related objects enabling faster implementation and changes
  2. Main company and its subsidiaries
  3. Superclass is the class being inherited from, subclass is the class doing the inheriting
  4. class Subclass : public Superclass
  5. When new variables and functions are defined within the subclass
1 Like

1a. Inheritance allows us to give objects an ‘is-a’ relationship to create more complex classes. This can help us be more efficient by using inheritance for a new object that has many identical properties as another object, without having to copy all of those properties. Also, if we want to change something about a parent class, the child classes will automatically inherit the changes, so we only have to update one block of code for program wide changes.

2a. One example of object inheritance could be company data where there is an employee class with the subclasses developer, HR and marketing. Within the developer subclass there could be further subclasses like frontend and backend.

1b. A superclass is the class from which another class inherits variables or functionalities. A subclass is the ‘child’ of the ‘parent’ class, and is the inheritor of variable or functionalities.

2b. To have a class inherit another class’ properties, we write our class declaration like this:
class Subclass: public Superclass {
3b. A subclass that inherits from a superclass has access to the superclass’ variable and methods. It can also pass this access to further subclasses.

1 Like

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

It lets you use objects as a base for new objects without rewriting code (or if you can’t access it).

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

Vehicle (Base object)
Car (inherited)
Plane (inherited)
Boat (inherited)

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

A subclass (child) is derived from the SuperClass (Parent)

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

class Car : public Vehicle {
// put your code here
}

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

You can add new functions (methods) - You can add properties (data types, values).

1 Like

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. for us to reuse the same values multiple times.
  2. vehicle would be a class and under it would be a car or bike eta.

1)superclass is from subclass inherits.
2)class vehicle: public car {
} ;
3) by creating a new var within a subclass

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. To me, this means not having to “reinvent the wheel” and using pre-existing class attributes and extending them to a new class structure. Also, if you update a something in the super class, that information will be updated in the sub class as well.

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

My body inherited my genes from my parents, so it is safe to say that all of the genes I received came from my parents.

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

The class being inherited from is called the superclass, and the class doing the inheriting is called the subclass.

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

class ChildClass : public ParentClass

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

A subclass can inherit the superclass’s methods and variables and include more methods and variables in order to go beyond what went before.

1 Like

You’re creating a vehicle class that inherits from car. :slight_smile:

  1. Why do we need inheritance in C++?
  • Allows the programmer a more simple way to make complex classes by re-using information from another class.
  1. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
  • You could have an Italian Chef as a sub class of an all around chef that makes some of the same foods and the all around chef would be considered the super class.

  1. What is a superclass and what is a subclass?
  • A super class is when information is inherited from this class into a sub class.
  1. How would you write a public inheritance in C++?
  • class Italian chef :
    Public:
    string dishes ( );
  1. In what ways can a subclass extend the functionality of a superclass?
  • They inherit some of the super class as well as have their own stuff on top of that.
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. Plants, Fruits, Persons… everything could be modelled in c++ or almost

1. In an inheritance relationship, the class being inherited from is called the parent, base or superclass and the class doing the inheriting is called the child, derived or subclass.
2. it would be something like this:

class Person{
public:
   string pName;
   int pAge;
   }
};
class baseballPlayer : public Person{
public:
   string bName;
   int bHomeruns;
   }
};

3. because we don’t have to redefine the information from the base class in out derived classes. we automatically receive the member functions and member variables of the superclass and we add additional functions or variables. So we are saving time and work, if we modify the superclass then we modify every subclass derived from it (adding new functions or fixing bugs).

1 Like

-minimize code repetition/duplication and improve code and program structure

  • m&m’s and skittles inheritance of candy
  • the superclass is the class being inherited from or the parent and the subclass is the class inheriting or the child.
  • class Skiing : public Sport
    {

}

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

Part 1

  1. Inheritance is useful because one object can directly acquire attributes (properties) from another object (usually its parent).

  2. Computer (display, OS, cpu)

    • Microsoft Surface (touch screen, windows, intel)
    • Levono (both, windows, intel)
    • Apple Mac (retina display, MacOS, Apple Silicon)

Part 2

  1. Superclass is the parent class. Subclass is the child class
  2. Example
    class Surface: public Computer
  3. Subclass inherits both methods and properties from the superclass.
1 Like
  1. Why do we need inheritance in C++? it lets us take a class and break it down to all the related objects in the class.
  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. Trees oak ash maple spruce fir pine softwoods hardwoods size color leaf needle… ect.
    part 2
  3. What is a superclass and what is a subclass? trees would be a superclass and species would be a subclass.
  4. How would you write a public inheritance in C++? public Trees{…}
  5. In what ways can a subclass extend the functionality of a superclass?
    subclass adds another branch of methods to our superclass which gives us new variable and functions to change methods or call them.
1 Like

Part1.

  1. It allows the creation of objects which can inherit properties from other objects.

  2. Apples & pears.

Part 2.

  1. Superclass is the main class in which the subclass inherits inherits data from.

  2. class Flat 1: public Building { };

  3. By adding new members or methods.

1 Like

Intro to Inheritance

  1. Inheritance allows us to create new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them.
  2. Let’s use an example of apples and bananas. Although apples and bananas are different fruits, both have in common that they are fruits. And because apples and bananas are fruits, simple logic tells us that anything that is true of fruits is also true of apples and bananas. For example, all fruits have a name, a color, and a size. Therefore, apples and bananas also have a name, a color, and a size. We can say that apples and bananas inherit (acquire) all of the properties of fruit because they are fruit.

Basic Inheritance

  1. 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 <string>
 
class Person
{
// In this example of public inheritance
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; }
 
};
  1. When a subclass extends a superclass, it gets all the functionalities of the parent class and can be added with more specific purposes of the child clases.
1 Like

You didn’t define a class that does the inheriting. :slight_smile:

You only defined a class but didn’t inherit from any base class. :slight_smile:

1 Like

Thank you for the correction