Inheritance in C++ - Reading Assignment

Part 1
1.Why do we need inheritance in C++?
We need inheritance in order to transfer properties between classes. As it is in the real world, we are unable to fit every single description of every object into one classification. Often, there are sub categories of objects that exist, where they might share similar properties, but still have their differences. We can use inheritance to express such a difference.

2.Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
One example could be the animal kingdom. While every living being on the plant is classified under “Life”. We then move down the taxonomic rank and go down to the kingdoms of animals, plants, fungis etc. The are further split down the line. In this case, objects are things like a lion and a tiger, would might be from the same animal kingdom, and from the cat family, but still have their differences.

Part 2
1.What is a superclass and what is a subclass?
A superclass is like a parent class, where it is a more general and overarching class of objects. Subclasses are derived from a superclass, where they inherit properties belonging to the superclass and might have additional unique properties.

2.How would you write a public inheritance in C++?
A public inheritance can be written by first declaring a new class, naming it, and then putting a colon, the word “public” and the superclass that we wish to inherit properties from. An example would be:

class Rectangle : public FourSidedShapes

3.In what ways can a subclass extend the functionality of a superclass?
A subclass is able to inherit whatever existing properties from the superclass, plus add more functions and variables of its own into the mix.

1 Like
  1. 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 (e.g. add new functions, or fix a bug), all of our derived classes will automatically inherit the changes!

  2. A coconut is a fruit.


  1. A superclass is what is being inherited from while a subclass is what is doing the inheriting.

  2. class Rectangle : public Shape
    { …

};

  1. If a new value or method is added to the superclass a subclass is able to utilize it.
1 Like

Why do we need inheritance in C++? We need inheritance in C++ so we dont need to rewrite code that are common between many classes. Thereby you can make use of the same member variables as a parent class without rewrite all the code.

Give a simple example of objects in the real world that could be modelled in C++ using inheritance. It could be fruit, shape, person. If you would like to make a class, student, a student are a person, and by inheritance you can get the attributes and variables from the person class and then you can add special features to the class student. Then if you want to add a class nurse, you again can inheritance the class person but then specialize the class for a nurse.

What is a superclass and what is a subclass? The class that is inherited is called a superclass or a parent, as for a subclass is the class that inheritance a class, could also be called a child class.

How would you write a public inheritance in C++? class BaseballPlayer : public Person
{all code…};

In what ways can a subclass extend the functionality of a superclass? It can create its own member variables and member functions - specific to that subclass, and it can inheritance and can access all public member variables and public functions in the superclass.

1 Like

part 1

  1. Why do we need inheritance in C++?
    minimize code duplication, and repetition as Inheritance involves creating new objects by directly acquiring 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.
    Genes inheritance from parents and adding of own personality on top.

part 2.

  1. What is a superclass and what is a subclass?
    Superclass is the class being inherited
    Subclass is the class doing the inheriting.

  2. How would you write a public inheritance in C++?
    class Asia : public Continent {};

3.In what ways can a subclass extend the functionality of a superclass?
Subclass can extend the functionality of a superclass either by adding new variables, new methods or overriding existing methods.

1 Like
  1. We need it to extend or specialize a class of a class. Let’s say object “Pear” which is a subclass (inheritant) of “Fruits”.

  2. Let’s say when a child is born it inherits both parents genes or when a seed is grown the plant inherits the genes of the plant which the seed came from.

Part two:

  1. When you inherit a class from another class you create a subclass. The class that you inheritated from is called a superclass.

  2. class Student : public Person {

    };

  3. In ways that you can use the functions(methods) of it, basically the functionalities (variables, functions). You can also add new methods and variables in the “game”.

1 Like

Part One:

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

So we can create new objects by inheriting attributes and behaviors of other objects, and then adding our own.

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

Cats and dogs.

Part Two:

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

A superclass is the parent class. A subclass is 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?

It allows us to reuse classes.

1 Like

Inheritance in C++ Part 1

1.inheritance gives the ability to access and reuse groups of variables and functions and even other classes. 2.cars and wheels. wheels could have a property tires,hubcaps and a methods rotate forward, rotate backward. cars could have a property make, model and as cars have wheels it make sense that cars have 4 wheel objects which inherit all the properties and methods of the wheels object.

Inheritance in C++ Part 2

1. A superclass is a the parent of 1 or more subclasses of child classes.
2. class ChildClassName : public ParentClassName.
3. A subclass can have its own variables and functions and have the parentclass variables and functions.
1 Like

1 To inhereit others objects ‘traits’ rather than creating them

2 Humans -> Torso - > Hearth


1 Supercless is the class that is being inherited (the bigger, more general class). Subclass is the class getting the inheritance.

2 class sportsPlayer : public Messi

3 Subclass can inherit the functionality of the superclass, and then extend on it further.

1 Like

PART 1
Why do we need inheritance in C++?
So we can create new objects by directly acquiring the attributes and behaviors of other objects and then extend or specialize them.

Give a simple example of objects in the real world that could be modelled in C++ using inheritance
When you were conceived, you inherited your parents genes, and acquired physical attributes from both of them – but then you added your own personality on top.

PART2
What is a superclass and what is a subclass?
Superclass: the class being inherited from
Subclass: the class doing the inheriting

How would you write a public inheritance in C++?
After the class declaration, we use a colon, the word “public”, and the name of the class we wish to inherit.

In what ways can a subclass extend the functionality of a superclass?
The subclass inherits the member functions and variables from the superclass, the subclass can add it’s own members.

1 Like
  1. we need inheritance as it assists in modelling out “is-a” relationships.

  2. a class called a “motor vehicle” which has subclasses called “car” , “bus”, “truck” etc

  3. the superclass is the parent class. a subclass inherits from the parent class

  4. class ChldClass : public ParentClass

  5. by adding new methods that are specific to the subclass behaviour but yet still needing the parent class core characteristics.

1 Like

PART I

Why do we need inheritance in C++?
We need inheritance so that we can have hierarchy between objects. This involves creating new objects by directly acquiring the attributes and behaviours of other objects and then extending or specialising them.

Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Vehicle > 4 wheel vehicles > Car

PART II

What is a superclass and what is a subclass?
A superclass is a class that other classes are going to inherit from. A subclass is a class that will inherit member variables and functions from the parent class.

How would you write a public inheritance in C++?
class Car : public Vehicle

In what ways can a subclass extend the functionality of a superclass?
It can extend it through its properties (member variables) and behaviours (member functions)

1 Like
  1. Inheritance allows us to create complex classes, by getting variables and functions of other objects and possibly passing them further.

  2. It can be for insance Animal as base (parent) class-> fish -> salmon
    -> bear ->grizzly (from general to specific)

  3. Superclass is the most general category of inheritance
    Subclass is the most specific category of inheritance

  4. class Fish : public Animal
    // class Fish is inheriting variables from the public class Animal

  5. Subclass inherits the functionality of superclass and it can add it’s own variables and functions

1 Like
  1. Inheritance is used to avoid repeating code through several classes sharing common variables and methods as well as structuring applications in a more scalable way.
  2. For example a house could inherit from a land, a land from a city, a city from a province and a province from a country…
  3. A superclass (Base class) is a class from which a subclass (Derived class) inherit from.
  4. To let a class inherit from an other, one just need to add a colon, the public keyword, and the name of the class it inherits from next to its own class name. like so : class Child : public Mother {…};
  5. A subclass can add methods (functions) to the class(es) it inherits from. By calling the subclass, one get the functions from the class it inherit from, plus the functions of the current subclass.
1 Like

http://www.learncpp.com/cpp-tutorial/111-introduction-to-inheritance/

  1. Why do we need inheritance in C++?
    Inheritance in C++ allows us to define a class in terms of another class thereby simplifying the development and maintenance of applications. Inheritance also facilitates code reuse and quicker implementation time as we can add additional functions and variables to a base class and all the derived classes automatically inherit the changes.

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

Eukarya  Domain
|
Animalia  Kingdom
|
Chordata  Phylum
|
Mammalia  Class
|
Primate  Order
|
Hominidae  Family
|
Homo  Genus
|
Homo Sapiens  Species

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

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

  2. How would you write a public inheritance in C++?
    After the class declaration (which consists of the word ‘class’ followed by ‘then name of the class’ add a colon followed by the word ‘public’ followed by the name of the base class to be inherited.

  3. In what ways can a subclass extend the functionality of a superclass?
    A subclass can add both new variables and methods that extend the more general superclass functionality so that it can effectively be applied to more specific situations and objects.

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

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    animals - cats, dogs, fish …

  3. What is a superclass and what is a subclass?
    the subclass (child) inherits from the super class (parent)

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

  5. class Subclass : public Superclass { ... }

  6. In what ways can a subclass extend the functionality of a superclass?
    we can add any new functions or variables that extend functionality

2 Likes
  1. It allows the construction of complex classes. It gives another option, instead of making something from scratch it can take properties from a previous class, for example, and then further be specialized from that point.

  2. A person inherits things from their parents but they are not a carbon copy – there are still differences. The same is true for apples and bananas. They are both fruits so they have similar properties but also a number of distinct differences.


  1. The superclass is the parent in the parent-child relationship. It would be fruit in the fruit-apple or banana scenario. The subclass is the child in the aforementioned examples. It would be triangle from shape, apple from fruit, etc.

  2. It is done by using a colon and then public and the name of the class that is going to give the inheritance. For example, class Player : public Person { } – the class Player will inherit from the Person class.

  3. With many different subclasses there is more specificity and control. In addition, by changing something in the base class you can change it for all the subclasses at one time. It gives more options to the user. It can create greater organization as well.

1 Like
  1. To create new objects that directly acquire the attributes and behaviors of other objects and then extending or specializing them.

  2. Apples and bananas that inherit the attributes of fruits.


  1. Superclass is the class being inherited from, subclass is the class doing the inheriting

  2. class SubClass : public SuperClass

  3. A subclass inherits member functions and member variables from the superclass and has their own members specific to that class that extend variables

2 Likes

Part 1

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

It is a method of constructing complex classes, A new object created can inherit properties of a previous one. This can help with future amendments, bug fixes and limits code repetition.

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

Fruits contain; apples, oranges and bananas, apples contain; golden delicious, granny smith pink lady etc. There is a hierarchy of items here which in c++ could be defined as objects.

Part 2

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

A superclass is also called a parent class and is the class that has its members inherited by a subclass or child class.

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

class Employee: public Person{ etc. };

Employee is the subclass and Person is the suprclass

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

A subclass can use its own members and functions as well as the ones from its superclass, and even superclasses that are above its direct superclass.
2 Likes
  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. toyota => cars => road vehicles

  3. 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. class child class: public parent class

  5. member variable and member functions, override parent methods

2 Likes
  1. Why do we need inheritance in C++? So we can add more diversity into a structure while still maintaining its main functionalities.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. Computing devices > Phones>> iPhone, Samsung, Nokia

  3. What is a superclass and what is a subclass? A subclass has the same master properties that are inherited by a superclass but with other more specialized member variables and methods.

  4. How would you write a public inheritance in C++? class Subclass: Superclass {…};

  5. In what ways can a subclass extend the functionality of a superclass? It introduces a new level of complexity to the superclass.

1 Like