Inheritance in C++ - Reading Assignment

1. Why do we need inheritance in C++
With inheritance we can pass the attributes, properties of one object to another object, which inherit the variables of the predecessor and becomes then a extending or specialist version.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance
A mule is the offspring of a male donkey and a female horse. Donkeys and horses are two different species of Equine, with different attributes.
The mule inherit the different qualities of his mother like strength, high etc. and the attributes of his donkey father like fearlessness. This crossover create a new type of Equine which is (was) very useful for transportation or farm work.

Part II
1. What is a superclass and what is a subclass?
A Super class or also called Base Class is a class whose properties are inherited by a sub class.
Sub Class is the class that inherits properties from another class.

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

class PrivatBookLibary:
{
public:
int numberOfBooks{};
string nameOfBook{};
string writerName{};
int pageNumber{};

   PrivatBookLibary( int n, string b, string a, int p) {
                                  int numberOfBooks = n , 
                                  string nameOfBook =  b ,
                                  string writerName   =  a,    
                                  int pageNumber    =  p)
                               }
                       }

3. In what ways can a subclass extend the functionality of a superclass
The subclass inherit properties and methods from the superclass without redefining the information from parent class.

You defined a new class with public variables instead of inherited from an existing class. Using your current class you could now inherit a new one using
class BookLibrary : PrivateBookLibrary {...} :slight_smile:

  1. We need inheritance to be able to create objects that re use the code of other objects

  2. strawberry / orange/ banana could all inherit the class fruit. which could inherit the class food type etc

  3. a superclass is the overall large class of which the subclass inherits from

  4. class Fruit:
    public Strawberry{ };

  5. The subclass can inherit methods from the superclass and add new methods and variables, or overwrite existing ones from the superclass

1 Like

1.Why do we need inheritance in C++? Instead of writing the same code all over again we can inherit properties from a class to a nother

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

1.What is a superclass and what is a subclass? Superclass is the class from where the heritance derives and subclass is the class that gets there inheritage from

  1. How would you write a public inheritance in C++? class BaseballPlayer, public: int m_homeRuns

  2. In what ways can a subclass extend the functionality of a superclass? By adding additonal functions or members, inheritance

m_homeRuns could be a public variable inside BaseballPlayer, but not a class to be inherited. It would make more sense to inherit from a more generic class like Person:

class BaseballPlayer: public Person

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

It allows alternative way of object composition, where it follows a hierarchy

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

Development of phones from early line phones to smart phones.

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

The parent class, on top of other classes which is inherited. Subclass on the other hand is a child class which inherits.

  1. How would you write a public inheritance in C++?
class name : public inheritedclassname
  1. In what ways can a subclass extend the functionality of a superclass?

Subclass automatically inherits new features from superclass, and can specialize to its own purpose.

1 Like

ok thanks for the info!!!

  1. polymorphism, also code reusage

  2. vehicle -> car

  3. superclass is the parent class whilst the sub class is the child class (the one that inherits)

  4. class B : public class A {}

  5. you could add more class specific methods in addition to what it inherits or use override old one using super inside the method in addition to it’s own stuff.
    Note that a subclass inherits all the functionalities and properties from its superclass as long as those are public or protected.

1 Like

PART ONE
1. Why do we need inheritance in C++?
Inheritance allows the relationship between classes, that minimizes code repetition or duplication, while improving the code and program structure.The creates an easier way to maintain the application.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
We can have a class named Veicules (that can be cars, bike, trucks, etc)

class Veicules {
public:
    string typeOfVeicule;
    bool hasMotor;
}

and another one called Cars that inherits some

class Cars : public Veicule {
public:
    string brand;
    int horsePower;
}

PART TWO

1. What is a superclass and what is a subclass?
A superclass is the class that gets inherited (the parent class), the subclass is the class that ihnerits from the superclass.

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

class Dog  : Public Animal {
public:
string breed;
string name;
string owner;
int weight;
}

3. In what ways can a subclass extend the functionality of a superclass?
They are able to ihnerit the capabilities from the superclass, modify the public and protected instances of the superclass (but not from the private).

1 Like
  1. Inheritance can help create new objects with new properties than inherit properties from another pre-existing object.
  2. For example, species of plants oculd be modelled in C++ using inheritance.

1 . A superclass is the class being inherited from and the subclass is the class which inherits.
2. Using a colon: class subclass : public superclass
3. A subclass can add new variables and functions to a superclass.

1 Like

You did it right the first try :smiley: this class doesn’t inherit anything

Right, thanks for the warning :sweat_smile:
Going to fix it :slightly_smiling_face:

1. Why do we need inheritance in C++?
It allows users to create new objects by directly acquiring the attributes and behaviors of others then extending, or specializing them

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

1. What is a superclass and what is a subclass?
Super-classes are classes that are inherited from, and subclasses are classes that are inheriting

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

3. In what ways can a subclass extend the functionality of a superclass?
Sub-classes have all the functionality of a superclass, but you can add specializations to the sub-classes

  1. Why do we need inheritance in C++?
    Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them.
    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. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Cell phones use their pervious models (inherit) to add add and take away features to improve the service and useability of them.
    A puppy inherits genes from both parents. As well as physical attributes from both, but it also adds it’s own adorable personality and cute features.

  3. What is a superclass and what is a subclass?
    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.

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

  5. #include

  6. #include <string_view>

  7. class Person

  8. {

  9. // In this example, we’re making our members public for simplicity

  10. public:

  11. std::string m_name{};
    
  12. int m_age{};
    
  13. Person(std::string_view name = "", int age = 0)
    
  14.     : m_name{ name }, m_age{ age }
    
  15. {
    
  16. }
    
  17. const std::string& getName() const { return m_name; }
    
  18. int getAge() const { return m_age; }
    
  19. };

  20. In what ways can a subclass extend the functionality of a superclass?
    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!