Inheritance in C++ - Reading Assignment

  1. Inheritance allows us to construct new classes in a “easy, intuitive and low maintenance way!” We save time because we don’t have to redefine classes, and also save time when we want to update the code, as classes will inherit all their updates.

Source: https://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/

  1. Baseball is a ball sport is a sport.

  2. A superclass ( parent class ) is the class being inherited from.
    A subclass ( child class **)**is the inheriting class.

  3. Writing a public inheritance in C++ is very simple!
    class BaseballPlayer : public Person

  4. Subclasses can extend the functionality of existing superclassess by adding new members and new methods; expanding the capabilities of the original (super)class.

1 Like
  1. Why do we need inheritance in C++?
    So that we can use code we already have written. Only having to add new unique code to new classes.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Products in a webshop: having a Product class with a name and price property.
    Then a child class DVD inheriting the name and price of Product, but adding a duration property.
    And another child class Book inheriting name and price of Product but also numberOfPages.

  3. What is a superclass and what is a subclass?
    A superclass is the class that is being inherited, a subclass is the class that inherits.

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

  5. In what ways can a subclass extend the functionality of a superclass?
    New members and methods can be added to a subclass. Making use of both the parent and the child members and methods.

1 Like
  1. Inheritance allows us an “is-a” relation model. Also allows efficient reuse of code.
  2. A resource booking system where an example of a resource is a holiday home. An actual web system I implemented in java a long time ago.

Part 2

  1. The class being inherited from is the super/base/parent class. The class that is inheriting from the super class is the subclass.
  2. class BaseballPlayer : public Person{…};
  3. A subclass can add new members and methods specific to that child or subclass.
1 Like
  1. to pass traits from one object to another
  2. apples and bananas would have trait inherited from fruit

  1. a classes super class is the class that gives inheritance, a sub class is the class that receives an inheritance
  2. class Apple : public Fruit
  3. inheriting functionality from super classes allow sub classes to pick up where the super class left off by adding more functionality
1 Like

1. Why do we need inheritance in C++?
Our objects may have some equal properties and some different properties. To avoid rewriting the equal ones, we can describe a group with the equal properties, then make groups in the group and then each different object.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
It could be the company example in previous articles. Let’s say we have to describe several companies and each company has a CEO and HR, but the further structure of employees is different. We could define the common structure for all and then the different positions for each separately.
1. What is a superclass and what is a subclass?
Superclass (or parent class, base class) is the class being inherited.
Subclass (or child class, derived class) is the class that is inheriting Superclass.
2. How would you write a public inheritance in C++?
If we want all employees to contain the information about their company, we could write like this:

class Employee: public Company
{
public:
(and here add specific members for Employee)

};

Then, if we need to define a manager, he is also an employee with properties of employee:

class Manager: public Employee
{
public:
(and here we would specify new members for Manager)

};

3. In what ways can a subclass extend the functionality of a superclass?
By adding new variables and functions.
Let’s say we have class A, which takes data and executes functions a1 and a2 on this data, and that is good for most occasions. On some exceptions, though, we also need to execute another function, so we can define class B which inherits class A, and then adds its own function b1.

1 Like

1)It makes it easier to create and maintain an application.
2)The life cycle of a butterfly.
egg>catterpillar>chrysails>butterfly

1)Superclass is also known as parent class. Subclass is also known as child class. Let me give u an example explaining the relation between superclass and subclass. Parents(superclass) inherit their genes to their children (subclass).
2)FootballPlayer(){
amountOfGoals

}
3)By adding new varaiibles and function.

1 Like

This is not really the best example imo. What would an egg inherit from a caterpillar for example? A better use of these would be to inherit the caterpillar from the class insect. :slight_smile:

Here you actually just defined a new class without inheritance. You would want to inherit the FootballPlayer from a class like for example Person:

class FootballPlayer: public Person {
  public:
    int amountOfGoals;
}
2 Likes

Part I

  1. Inheritance allows us to reuse classes by having other classes inherit their members.

  2. Inheritance example:

  3. Beverages:

a) alcoholic

  • beer
  • cider
  • vodka
  • whiskey

b) non-alcoholic

  • soda
  • ginger ale
  • cola
  • juice

Part II

  1. A subclass is the class being inherited from in an inheritance relationship , and the class doing the inheriting is called subclass.

  2. class Beverages : public Alcoholic {};

  3. A subclass extends the functionality of a superclass by the ability to add new members, methods and override the existing methods.

1 Like

The name of the class you are defining comes first, then you list the inherited class (or more):
class Alcoholic: piblic Beverages { ... } :slight_smile:

1 Like

Got it! Thank you very much!

  1. It let’s us create new objects by directly acquiring the attributes of other objects, thereby reducing redundancies and better programme structure.
  2. Fruit>Apple>Seed
  3. A superclass is being inherited from a parent, subclass is the class doing the inheriting.
  4. class ChildClass : public ParentClass {
    };
  5. All public and protected functions are inherited by the sub class.

Why would you inherit Seed form an Apple? :slight_smile:

1 Like

once the apple decay we’ll have seeds?

True, but this is the property of an apple :slight_smile: so you would for example define the object seeds inside an Apple class.
Extending the class Seed from an Apple wouldn’t make sense since seeds don’t inherit any properties from an apple :slight_smile:

1 Like
  1. Why do we need inheritance in C++?
    IInheritance allows us to reuse classes by having other classes inherit their members.
  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Fruit - Name, Color, Weight

  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++?
class parent_class {
public:
    //Body of parent class
};
class child_class : public parent_class {
public:
   //Body of child class
};
  1. 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. 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
  1. Why do we need inheritance in C++?
    In order to organize classes by having general proprieties and functions that can be inherited by more specific classes.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Furniture could be a superclass and couch and table could inherit their proprieties (e.g. price, material, color).

  3. What is a superclass and what is a subclass?
    Superclass is a foundation class that has no parents and another class (subclass) inherits proprieties and functions.

  4. How would you write a public inheritance in C++?
    class Subclass : public Superclass{
    //member variables and functions
    };

  5. In what ways can a subclass extend the functionality of a superclass?
    A subclass inherits the functionalities of a superclass and can have their own specific functionalities.

1 Like

1. Why do we need inheritance in C++? in this way we can create general classes above and then create more specific classes underneath which exploits the function members and members of the general classes

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. tomato and potato inherit from vegetables

1. What is a superclass and what is a subclass? superclass is the higher more common class and subclass is the lower and more specific
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?
it inherit both members and functions from the superclass but it can add its own functions and members

1 Like

P A R T I

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

Object composition allows us to build “has-a” relationships with the constituent parts. However, object composition is just one of the two major ways that C++ enables us to construct classes. The second way is through inheritance, which allows us to build “is-a” relationships. For example, 386, 486 and Pentium are all computer processors. The 386 is-a computer processor and the 486 is-a computer processor too. We need inheritance because when the above is applied in practical terms, it allows us to inherit code from other classes that have this is-a relationship. This helps us avoid code duplication.

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

Fruit – a peach is-a fruit and a banana is-a fruit.

P A R T II

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

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.

2. How would you write a public inheritance in C++?
The example below is where we want BaseballPlayer to inherit details (e.g. name and age) from the Person class. It’s better to use inheritance here rather than just enter name and age again into BaseballPlayer because if we use inheritance, any updates made to the Person class will automatically be reflected in the BaseballPlayer class too. This works because a baseball player is-a person. To do this, we add a colon after BaseballPlayer followed by “public Person”.

Example:

class BaseballPlayer : public Person{
public:

// Your code here

};

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

A subclass inherits all the functions and variables from the superclass. It can then also define its own functions and variables too.

1 Like

A)
1. Why do we need inheritance in C++ ?
*so we can easily copy attributes and objects from other classes without having to reset all functions
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
*Class smartphones
A)Apple
B)Samsung
C)Huawei
4)Others

B)

1. What is a superclass and what is a subclass?
It is a parent class or base class from other classes, child classes, inherit.
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?
you dont have to redefine the info from the base class
It automatically receives the member functions and member variables.can add additional functions or member variables, if the superclass is update so are the children classes

1 Like

Why do we need inheritance in C++?

Some of the classes we need to construct will share member variables and functions. Without inheritance we would need to define the same function or variable many times over for each class that requires it. Inheritance allows us to structure our classes in a way that all shared members are inherited from a ‘common ancestor’ class. This makes code more concise and keeps large programs organised.

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

A zoo may need to keep track of all its animals and ensure the animals diverse requirements are met. All animals will require food, shelter and vetinary service but they will differ in the types of such. This could be modelled in C++ using inheritance.

What is a superclass and what is a subclass?

In an inheritance relationship the superclass has all of its members inherited by the subclass.
How would you write a public inheritance in C++?

class Subclass : public Superclass
{
}

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

A subclass can extend the functionality of a superclass by ‘repurposing’ it for new data types that build on top of the preexisting superclass.

1 Like