Inheritance in C++ - Reading Assignment

Article 11.1

1. why do we need inheritance in C++?
inheritance is in everything in the real world just like in the programming world. Different programming languages inherit different properties front earlier programming languages. inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and than extending them or specializing them.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
An oak tree and a pine tree have both inherited the tree properties.

Article 11.2

1. What is a superclass and what is a subclass?
The class being inherited from is a super class and the class doing the inhereiting is the sub class.

2. How would you write a public inheritance in C++?
class baseballplayer : public person
{jhsddbkas
};

3. In what ways can a subclass extend the functionality of a superclass?
By inheriting from a base class, which allows us to use the information already assigned to that clas and apply it to the derived classes.

1 Like

Part 1

Why do we need inheritance in C++?

  • 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, all of our derived classes will automatically inherit the changes.

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

  • Banana and Apples inherit from Fruit

Part 2

What is a superclass and what is a subclass?

  • A superclass (also called parent class or base class) is the class being inherited from.
  • A subclass (also called the child class or derived class) is the class doing the inheriting.

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

  • After the class name write a colon and the word public and the name of the class we wish to inherit.
  • e.g. class BaseballPlayer : public Person

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

  • A subclass inherits all the variables and methods from a superclass and adds their own methods and variables thus extending the functionality of a superclass.
1 Like

Part 1:

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

    Inheritance allow the creation of complex objects by using all the features of an exhisting objext, but with the ability to add additional, specialized features.

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

    Cars - most features of previous models are included in the new versions.

Part 2:

  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.

  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?

    Inheriting from a superclass saves work by only requiring the addition of additional functions needed instead of rewriting the entire class. In addition, any updates to the superclass will automatically be inherited by the subclasses.

1 Like

PART 1

1. Why do we need inheritance in C++?
Inheritance is important because it is an efficient way of creating new objects that acquire attributes and behaviors from a more general and encompassing object. With inheritance, we don’t need to repeat code that has already been written to define these attributes and behaviors. New objects simply inherit them, and they can have their own more specialized attributes and behaviors.

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

Orchestral Musical Instruments (top-most object)
String Instruments | Percussions | Winds | Metals
Under Strings: Violin, Viola, Cellos, Bass
Under Percussions: Piano, Xylophone, Cymbals, Drums, etc.
Under Winds: Flute, Oboe, Clarinet, French Horn, Bassoon
Under Metals: Trumpet, Trombone, Tuba

PART 2

1. What is a superclass and what is a subclass?
A superclass (also known as parent class or base class) is the class from which other other classes will inherit attributes (properties) and behaviors (functions) from.
A subclass (also known as child class, derived class, or subclass) is the one that inherits the attributes and behaviors from the superclass.

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

class StringInstrument : public OrchestralInstrument

3. In what ways can a subclass extend the functionality of a superclass?
The subclass adds specialized properties (member variables) and behaviors (member functions) that don’t have to be determined when first creating the superclass. This is efficient because programmers don’t have to think of every possible property or behavior or child classes beforehand.

1 Like
  1. It allows classes to be created more simply by building on attribute of parent classes

  2. A sport car is a type of car, a car is a type of vehicle.

  3. A super class is the parent or base class that give its attributes a subclass or or child class.

  4. We write the key word class followed by the name of the child class followed by : followed by the key word public and finally the name of the parent class

  5. The child class automatically inherits the variables and methods of the parent class.

1 Like

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

so that objects can inherit the properties of other objects without having to recreate from scratch.

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

trees and flowers inherit from plants

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

another name for a base class. the top in a hierarchy. subclass inherits properties from the top

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

class tree : public Plant
{
...
};

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

subclass gets all the functionality of the upper classes specific properties

1 Like

Hello @ivan and dear Community,


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

To abstract and demonstrate real-life conditions. Additionally, when using inheritance it’s possible to build a hierarchy of classes. Furthermore, code minimization is and code duplication is avoided due to inheritance.

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

Music types: There is electronic, rock, swing, … > electronic > techno > goa > psytrance > hitech.


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

The superclass is the parent of a subclass. A subclass is subordinated by the superclass and inherited all it’s members.

  1. How would you write a public inheritance in C++?
class Subclass : public Superclass {
    private:
    public:
    Subclass() {};
};
  1. In what ways can a subclass extend the functionality of a superclass?

The subclass can implement it’s own members and use other objects to interact with and delegate to it. The subclass can also override it’s inherited functions from the superclass.


Cheers
Kevin

Part One:

  1. We need inheritance in c++ because it is one of the two major ways to create objects, objects which acquires their properties from other objects.
  2. Humans, monkeys, sloths, cows, cats, and dogs all inherit properties from mammals.

Part Two:

  1. A superclass is the class being inherited from(the parent) in the answer about the superclass or parent will be mammels and a subclass is the class doing the inheriting(the child).
  1. I would write a public inheritance in C++ as:
    class Mammals {
    private:
    string humans;
    string dogs;
    };
    class Animals : public Mammals{
    };
  2. The subclass can extend the functionality of the superclass by inheriting all of its functionalities and defines its own new functionalities or overriding the inherited functions.

Why do we need inheritance in C++?
It enables us to create new objects by directly acquiring the attributes and the behaviors of other objects, thus minimizing redundancies and better program structure.

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

Fruit>Apple>Seed

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?
Subclass extends the functionality of a superclass with the addition of new member functions and member variables on top of the ones already inherited from the superclass.

1. Why do we need inheritance in C++?
Inheritance lets us create new objects while inheriting attributes and behaviors of another allowing us to reuse the code functionality and fast implementation time. Which makes it easier to create and maintain applications.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. Music>>Genres>>Artist/Band

1. What is a superclass and what is a subclass?
A superclass( base class, or parent class) is the class that is being inherited from. The subclass (derived class, or child class) is the class that is doing the inheriting.

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

// BaseballPlayer publicly inheriting Person
class BaseballPlayer : public Person

3. In what ways can a subclass extend the functionality of a superclass? It can add more member functions and member variables to those inherited from superclass
or overwrite them.

  1. Why do we need inheritance in C++?***
    We need “inheritance” C++ to define an “is – a” relationship between two objects***

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

Colors: Red, Green, Orange, Blue

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.

class ChildClass : public ParentClass {

};

  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.

  1. Why do we need inheritance in C++?
    It allows us to reuse generic object that is created more efficiently by inheriting the properties of another objects.

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

  3. What is a superclass and what is a subclass?
    Superclass is the object that is being inherited whereas subclass is the object inheriting from the Superclass

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

  5. In what ways can a subclass extend the functionality of a superclass?
    All the public & protected functions are inherited by the sub class.

  1. In C++ inheritance can be vital using old software with new, hence it creates and environment for backwards compatibility.
    2.In Hierarchies like Family trees, Dogs: Where dogs of different type have similar characteristics like far, eyes, nose, tail, 4 legs, barking.

Second part

  1. The super class can also be called the parent class or base class. This is where the characteristics are inherited from and the subclass is the one doing the inheriting

class Person: public University

  1. how a subclass can extend the functionality of a super class.
    Through the utilization of inheritance chains and then simply add the additional functions or member variables as desired.

Pt 1

  1. Why do we need inheritance in C++? It allows going down to more specifics while new objects still inherit more general properties. This allows for less work along the way when changes to the base class occur.
  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. When preparing a database or record of students in a University, one can group different types of students based on gender, department, major and class. Or even keep records of alumni together with current students under the same database.

Pt 2

  1. What is a superclass and what is a subclass? The superclass is the base class from which subclasses can derive members before even declaring new ones.
  2. How would you write a public inheritance in C++? After you declare the new class, you follow by a colon and then write public + the parent class designation, i.e. class ChildClassName: public ParentClassName
  3. In what ways can a subclass extend the functionality of a superclass? New members specific to a subclass can be added, thus expanding the functionality of the whole system.
  1. Why do we need inheritance in C++?

    • inheritance allows for the creation of new objects with interherited attributes and properties of other objects. Provides better reuse of code and avoids duplication.
  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.

    • a family tree
  3. What is a superclass and what is a subclass?

    • a superclass is the high level which the subclass inherites from
  4. How would you write a public inheritance in C++?

    • class son : public father {};
  5. In what ways can a subclass extend the functionality of a superclass?

    • the subclass can be extended by adding its own variables or functions to the inherited structure
  1. Why do we need inheritance in C++?
  • Inheritance allows us to create objects that inherit the properties of another object
  1. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
  • Animals -> felines -> cats
  • Animals -> mammals -> horses
  1. What is a superclass and what is a subclass?
  • A superclass (aka parent class and base class) is the class being inherited from.
  • A subclass (aka child class an derived class) is the class doing the inheriting
  1. How would you write a public inheritance in C++?
  • class TypeOfClass : public ClassBeingInherited
  1. In what ways can a subclass extend the functionality of a superclass?
  • Within a subclass, you can add new members, add new methods and override existing methods
  1. Why do we need inheritance in C++?
    It allows us to create objects that inherit properties of another object.I allows us to build “is-a” relation between objects.

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

  3. Currency
    1.1 Fiat Currency
    1.1.1 Euro
    1.1.2 Dollar
    1.1.3 Scam
    1.2 Crypto Currency
    1.2.1 Bitcoin
    1.2.2 ETH
    1.2.3 BITCONNEECTT

  4. What is a superclass and what is a subclass?
    A superclass is the class being inherited from, the Parent class
    A subclass is the class that inherints something from a Superclass

  5. How would you write a public inheritance in C++?
    class Veggy {
    private:
    string color;
    int weight;

  6. In what ways can a subclass extend the functionality of a superclass?
    A subclass grabs the methods of the parents class and adds additional member functions and variables on top of these.

Why do we need inheritance in C++?

So we can create new object that inherent attributes from another object.

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

For example humans we inherent genes from and on top of that do we have our own specific attributes.

What is a superclass and what is a subclass?

Superclass is the class that is being inhereted from while subclass inheret from the superclass.

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

Class random : public random2

{

};

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

If a class inherit the sublass, it’s also connected to the superclass.

1. Why do we need inheritance in C++?
So that we can create complex instanced classes (object) through the creation of a new object which acquires the properties of another object straight away. By contrast, another way to do it is through composition, by combining pre-existing objects.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Any hierarchy would fit that purpose. For example, the biological hierarchy that categorizes living being:
Domain -> Kingdom -> Phylum -> Class -> Order -> Family -> Genus -> Species

1. What is a superclass and what is a subclass?
Superclass = a class from which the inheritance comes
Subclass = the class that inherits the members

2. How would you write a public inheritance in C++?
class Superclass {
public:
int X;
void toDo(){ //yaddayadda }
};

class Subclass : public Superclass
{ public:
int Y
void moreToDo(){ //yaddayadda }
};

3. In what ways can a subclass extend the functionality of a superclass?
A subclass contains ALL of the members of a superclass, as well as its own members. This way we can use the functionalities of a superclass in a separate (sub)class, without having to redefine each member again.

Why do we need inheritance in C++?
Allows object and class deinitions to be reused and enhanced

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

Vehicle
Car Motorbike
VW Volvo Ducati BMW

Think about the following questions while reading:

What is a superclass and what is a subclass?
Superclass is a Class which is inherited from by a Subclass

How would you write a public inheritance in C++?
class BaseballPlayer : public Person

In what ways can a subclass extend the functionality of a superclass?
It can define members and functions of its own