Inheritance in C++ - Reading Assignment

Welcome to the discussion about the reading assignment about Inheritance.

Leave your answers to the questions below in this thread. If you have any questions or you want to discuss something connected to the assignment feel free to do it in this thread as well, but please everything to the topic.

Read this article (http://www.learncpp.com/cpp-tutorial/111-introduction-to-inheritance/).

Think about the following questions while reading:

1. Why do we need inheritance in C++?
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.

Read this article (http://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/).

Think about the following questions while reading:

1. What is a superclass and what is a subclass?
2. How would you write a public inheritance in C++?
3. In what ways can a subclass extend the functionality of a superclass?

17 Likes

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.

5 Likes

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

2
A “Motorcycle”- and a “Car”-class could both inherit member variables like “motorType”, “numberOfWheels” and “maxSpeed” from the superclass “Vehicle”. Then we could later add a “closeDoors()”-method to the “Car”-class and a “makeWheely()”-method to the “Motorcycle”-Class and so on.

1
The subclass is the class that inherits from the superclass.

2
class Motorcycle : public Vehicle {
//add members
};

3
By inheriting all the functionality from the superclass and then defining their own specific (new) functions or override inherited (old) functions.

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

    1. Vehicle
      1.1 Car
      1.1.1 Volvo
      1.1.2 Audi
      1.2 Motorcycle
      1.2.1 Suzuki
      1.2.2. Yamaha
  3. What is a superclass and what is a subclass?
    A superclass is a parent class, is being inherited by a child class
    A subclass is a child class inheriting a parent class

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

    }

  5. In what ways can a subclass extend the functionality of a superclass?
    Add new members
    Add new methods
    Override existing methods

1 Like

First part:
1- For reusing code and not replicate
2- Animals > Quadrupeds > Dogs > Pitbulls

Second part:
1- Superclass is the class from subclass inherits methods and properties
2- class Quadrupeds : public Animals {}
3- Two ways, is-a-class (object composition) and has-a-class (inheritance)

1 Like

Part I

1. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. It provides an opportunity to reuse the code functionality and allows faster implementation.

2. Earth, Mars, Saturn, Jupiter, etc. could all inherit from the class Planet because all of them have a size, weight, diameter, etc.

Part II

1. A subclass inherits methods and variables from a superclass.

2. class Earth : public Planet {};

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

1 Like

Part 1:

Why do we need inheritance in C++?
- It allows for an additional type of relationship between data, the ‘is-a’ relationship.

Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
- Workers have specific jobs yet inherit generals from Employees. Employees might inherit People.

Part 2:

What is a superclass and what is a subclass?
- A superclass is the class which is closest to the root. It could otherwise be called the parent class. The subclass in contrast is the offspring of the superclass.

How would you write a public inheritance in C++?
- class MyClass : public MySuperClass{ //This is at the start of the class definition.

In what ways can a subclass extend the functionality of a superclass?
- They are able to inheret the superclasses capabilities and yet include more methods and variables in order to go beyond what went before.

1 Like

1. Why do we need inheritance in C++?
to describe an “is a” relationships. Example Ivan is a Young Swedish God. This allows us to have members in the class Young Swedish God class that will be apart of defining the Ivan class. lol
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
The amount of slices in a pie would inherit the ingredients in a pie.

1. What is a superclass and what is a subclass?
Superclass is original foundational class while the subclass is a class that is directly correlated and cannot fully be described without (example Parent (superclass) to a child (subclass))
2. How would you write a public inheritance in C++?
class Blackbalancesheet : public Ivanacademy2018 { }
3. In what ways can a subclass extend the functionality of a superclass?
By already having the members and functions of the superclass a subclass extends by providing more details aka members/variables and functions. Example: Young Swedish Gods are 1. born in sweden 2. always learning 3. helping others learn. While Ivan ( the subclass) has an 1. height 2. sense of humor 3. nice hair which would extend past Young Swedish God hahaha “Smash the like button guys.”

  1. to model “is-a” relationships. it helps reusing code and makes the overall structure of our program more convenient.

  2. furniture - closet / table / chair. Closets, tables and chairs are all furnitures which could inherit properties like price, weight color etc…

  3. a superclass is the class being inherited from and a subclass is the class doing the inheritung. In the example above, furniture is the superclass and table and chairs are subclasses.

  4. class OurClassName : public superclassName { };

  5. we can create new variables and methods within the subclass or we can overwrite existing methods.

PART 1

1.Thanks to Elias:

  1. Example:
    Edibles
    Food,e.c.t
    Fruits, Vegtables, Poison, e.c.t
    Bannana, Apple, orange, e.c.t

Part 2

  1. Quote from article:

Inheritance in C++ takes place between classes. 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.

  1. Example: “class Supervisor: public Employee”.
Why do we need inheritance in C++?

Inheritance allows us to reuse member values and functions from previously written classes to build new classes.

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

You could define a class called clothing, that contains a bunch of members. You may need to write individual clothing pieces by class (shirt, jacket, jeans, etc.) that will be able to inherit properties from the clothing class (size, colour, etc.).

What is a superclass and what is a subclass?

A superclass aka parent class or base class is the class that is being inherited from. A subclass is the class that is doing the inheriting from the superclass.

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

Using my example above, I’d do it like this:

#include <iostream>
#include <string>


using namespace std;

class Clothing {
    public:
    string color;
    bool clean;

Clothing() {
    color = "not supplied";
    clean = false;
}

Clothing(string _color, bool _clean) {
    color = _color;
    clean = _clean;
}

void printStatus () {
    cout << "This item of clothing is "<<color<< endl;
    if (clean == true) {
        cout << "This item of clothing is clean" << endl;
    }
    else {
        cout << "This item of clothing is dirty" << endl;
    }
}
};

class Jeans : public Clothing {
    public:
    int length;
    int waistSize;

Jeans(int _length, int _waistSize) {
    length = _length;
    waistSize = _waistSize;
}

void printFullStatus() {
printStatus();
cout << "This item has a waist size of  "<<waistSize<< endl;
cout << "This item has a leg length of  "<<length<< endl;
}
};


int main()
{

Jeans firstPair(73, 32);
firstPair.color = "Blue";
firstPair.clean = true;

firstPair.printFullStatus();
return 0;
}

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

It can provide different and versatile ways of changing members within the superclass by using member functions that have been defined in the subclass.

2 Likes

1- inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them.

2- Iphone < Iphone II < Iphone III < …

1- 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 dog : public animal
{

};

3- They are able to inheret the superclasses capabilities and yet include more methods and variables in order to go beyond what went before.

Introduction to Inheritance

1. Why do we need inheritance in C++?
Inheritance allows us to create new objects by directly acquiring the attributes and behaviors of other objects and then extending and specializing them.

2. Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
We can say that both cats and lizards are cow. Therefore we could model classes Cat and Cow as subclasses of class Animal. The Animal class could contain methods like run(), eat() etc. and attributes such as age, weight, gender etc. which can be inherited by all of its subclasses. We can then extend the subclasses to contain methods and attributes specific to Cat or Cow. For example, Cow could have a moo() method and Cat can have a meow() method.

Basic Inheritance

1. What is a superclass and what is a subclass?
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. How would you write a public inheritance in C++?
After declaring the class, 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?
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.

  1. Why do we need inheritance in C++? Inheritance allows to define a class in terms of another class,
  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Building
    Streets
    Avenues

from a Class CityRoad

  1. What is a superclass and what is a subclass? subclass inherits from a superclass
  2. How would you write a public inheritance in C++? class Avenues : public CityRoad {};
  3. In what ways can a subclass extend the functionality of a superclass? subclass could add new methods, new variables (override methods too)

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

Inheritance allows programmers to associate "is-a" relationships between classes. This allows new objects to acquire the functionality of objects before it. It reduces the amount of code needed when describing a super class of objects that all have similar characteristics.

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

People can be modeled. The people super class would have attributes common to everyone like name, date of birth, place of birth ect… Different people live and work in different places and thus programs to keep track of these different people will keep track of different data types. If you are now programming a database to keep track of musicians you can inherit the people superclass as a base, a foundation to begin working on, instead of writing it from scratch. Since musician “is a” person you can focus on writing elements specific to musicians.

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

The class being inherited is called the parent class(super class, base class), and the class inheriting is the child class (subclass, derived class).

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

Public inheritance can be invoked by writing the workd class followed by the name of the new Child Class followed by a colon, then the word public and then the name of the Parent Class, the class that is to be inherited. For example;

class ChildClass : public ParentClass {  /*Class elements*/  };

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

A child class inherits both behaviors and properties from the parent. These variables and functions become members of the derived class.

1- Inheritance enables us to create complex classes in an easy way.inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them.
2- Shape,Rectangle,Square,Triangle. Rectangle ,Square and Triangle are all shapes and inherit
all of the properties of shape.

Part two :
1- 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- After the subclass declaration, we use a colon, the word “public”, and the name of the class we wish to inherit. This is called public inheritance. For example :
class Employee : public person
{

};
3- By defining its own member variables and member functions a subclass can extend the functionality of its superclass.

Part 1
1. Why do we need inheritance in C++?
Inheritance allows us to combine common data and behaviour in a common class and specific traits into child classes.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Vehicle as parent class then cars, trucks, bikes etc as child classes which can have their own further child classes.

Part 2
1. What is a superclass and what is a subclass?
Super or parent or base class is the one that is being inherited and the sub or derived or child class is the one that inherits.

2. How would you write a public inheritance in C++?
child class : public parent class
For example:
class Circle : public Shape

3. In what ways can a subclass extend the functionality of a superclass?
It has access to the parent class methods as well as variables, but anything specific to it can be added to make it more specific.

PART 1

1. Why do we need inheritance in C++? Through inheritance, we can model an “is-a” relationship between two objects. This is useful because it allows us to construct more specialised classes from more generic classes and types by inheriting the attributes and behaviours before extending them.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. Information about animals is something that can be modelled with inheritance. For instance, you can have classes such as bird and fish that inherit from an animal class, which contains properties and behaviours that are applicable to all animals. Then when it comes to more specific information like the colour of its feathers or the number of gills – these can go in the bird class and fish class respectively, where they make more sense.

PART 2

1. What is a superclass and what is a subclass? In an inheritance relationship, the class being inherited from is called the superclass and the class doing the inheriting is called the subclass.

2. How would you write a public inheritance in C++? After a class declaration, we can create a public inheritance by adding a colon followed by the public keyword and the name of the class we want to inherit from.

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

  1. It makes it easy for us to adopt behaviors and attributes and then expand on them for something else. Making both things related.

  2. A family tree could be used to model inheritance.

  3. The class of which is being inherited from is called the superclass and the class that is inheriting is the subclass.

  4. You add “: public ‘class name’ “ to the end of the class declaration that you want to have inherit.

  5. They make it easy because if you want to add functionality to all related classes, you can just add it to the superclass to make all subclasses of the superclass inherit the functions.

PART ONE

1 We can build more complex classes using relationship between two objects.
2.Car models can inherit some features from older cars ( engine features, equipment, etc)

PART TWO

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.
2.class audiA4 : public AUDI
{

};
3.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. T