Inheritance in C++ - Reading Assignment

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

Inheritance let’s you build complex classes, which builds an “is-a” relationship between two objects. It involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them. This avoids duplication and improves your code.

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

Furniture<Table<coffee table and also Furniture<closets<dressings

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

A superclass is a class being inherited, so in my example, it’s furniture, the subclass is then Table. Table is then the superclass to the coffee table sub class. Subclasses inherit the functions and properties of parent classes.

4. How would you write a public inheritance in C++?
class [INSERT NAME OF THE SUBCLASS YOU ARE CREATING] : public [INSERT NAME OF PARENT CLASS]
{
...
};
5. In what ways can a subclass extend the functionality of a superclass?

They do so by either adding variables or members, on top of the inheritance from the superclass.

1. Why do we need inheritance in C++?
It allows us to create objects which have all of the properties of other objects.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Icecream is a type of frozen food which is a type of food.

1. What is a superclass and what is a subclass?
A superclass is a parent class which another class inherits it’s properties from.
2. How would you write a public inheritance in C++?
class Subclass :public MainClass{
}
3. In what ways can a subclass extend the functionality of a superclass?
It adds all of the variables and functions of the superclass plus it’s own variables and functions.

  1. We need inheritance in C++ in order to prevent code duplication. If there was no such concept as inheritance in C++ we would have to duplicate the same properties and functions and ignore the real world relationships between object - this would be very inefficient.

  2. A Truck, Car, and Bus all inherit from a Vehicle.


  1. A superclass contains the base implementation consisting of variable and functions. Subclasses inherit the implementation from the superclass and have their own specific implementation.

  2. class Vehicle {
    // variables
    // e.g 4 wheels, engine, chassis

    // constructor

    // methods
    };

class Bus: public Vehicle {
// bus specific variables
// payment machine, number of deckers e.t.c

// bus methods
};

  1. A subclass automatically receives the variables and methods from superclass through extension. Subclass can add own specific variables and methods.

1. Why do we need inheritance in C++? this is a way to simplify our code, child class derivations can simply inherit the member variables of the parent class, and then have their own variables, which in turn, can be inherited by child classes of that class. All of this reduces the complexity of the code because each child class will already have a basic set of variables that it can access from the parent.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. We are all children of our parents. So, genetically, we inherit certain characteristics of our parents (dominant and recessive genes), however, we also have our own individual characteristics that are unique to ourselves.
1. What is a superclass and what is a subclass? A superclass is a parent class from which other child classes inherit from.
2. How would you write a public inheritance in C++? class child public: parent { }
3. In what ways can a subclass extend the functionality of a superclass? A subclass can extend the functionality of a superclass by having its own functions and methods that use the inherited variables and functions of the parent to perform yet more extended functionality.

Part I

  1. In order to avoid to reinvent the wheel. More precisely all men and women are humans so instead of building three classes from scratch we can build a parent class human and then use inheritance for classes man and woman. Inheritance allows us to write less code by reusing properties defined in the parent class
  2. A car is a sub-class of a vehicule

Part II

  1. A superclass or a parent class is a class above a subclass like your parents were born previously than you and you inherit some of their genetics
  2. class Man : public Human
  3. By adding more specifications. For instance in the subclass Man we could add facialHairStyle which wouldn’t be relevant for a Woman subclass so it wouldn’t appear in the superclass Human

1 - we use inheritance in C++ in order to create specific classes and use more generic attributes from other more general class, without having to rewrite these attributes, which allows to focus on the more specific attributes of the class.
2 - you could model the specific class fruit inheriting from the more general class food.

1 - superclasses are classes that have one or more classes inheriting from them. on the other hand, subclasses are classes that inherit characteristics from one or more classes.
2 - After the class declaration, we use a colon, the word “public”, and the name of the class we wish to inherit.
3 - a subclass, as it is more specific, can have extra functionalities that apply to definite cases in the superclass, making it more specialized.

Part 1

  1. For reusing code and not replicate
  2. Muscle, Biceps, Triceps, Quadriceps, Hamstrings,

Part 2

  1. A superclass is a parent class, is being inherited by a child class. A subclass is a child class inheriting a parent class.

  2. class : public {

    }

  3. Add new members
    Add new methods
    Override existing methods

#letsgetthiscrpyto

1. Why do we need inheritance in C++?
It allow us to create objects that will get properties from a superior object, sharing some properties in order to recycle code if its need it.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.

  1. PC Components:
    1.1 Motherboard
    1.2 CPU
    1.3 RAM
    1.4 PSU
    …So on…

1. What is a superclass and what is a subclass?
superclass is the main class with properties, subclass is the one who will inherit the properties from the superclass.
2. How would you write a public inheritance in C++?
class Polar : public Beers {
//add code here
}

3. In what ways can a subclass extend the functionality of a superclass?
adding, deleting or modifying variables or methods without changing the superclass, so u can tweak the same method or variable in order to do get derivated results.

1 Like

. Why do we need inheritance in C++?
inheritance involves creating new objects (“child class”) by directly acquiring the attributes and behaviors of other objects (“parent class”) 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.
Sportsmanlike: Footballer: Messi.

Part 2
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 Sportsmanlike : public Footballer{
//footballer members;
};
3. In what ways can a subclass extend the functionality of a superclass?
By adding its own specific subclass members (variables and/or methods).

Inheritance in C++ - Reading Assign

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

  • to avoid code duplication

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

  • Stones

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

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

class Stone {
...
};
class sediment : public Stone{
...
};

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

  1. Why do we need inheritance in C++?
    To build relationships between objects. And it helps to reuse code more easily.

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

  3. What is a superclass and what is a subclass?
    superclass is the parent class (Animals for example) and subclass is

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

  5. In what ways can a subclass extend the functionality of a superclass?
    We can add new fucntions or variables.

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

We need it to create new class which will use the same members without repeating the code and it enables to extend it or make it more specific.

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

Square is inheritance of rectangle.

Part II

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

The superclass is class upper in the hierarchy than subclass which inheritance all of the members from superclass.

  1. How would you write a public inheritance in C++?
class Supervisor: public Employee
{
public:
    // This Supervisor can oversee a max of 5 employees
    long m_overseesIDs[5];
 
    Supervisor()
    {
    }
 
};
  1. In what ways can a subclass extend the functionality of a superclass?

By adding new members variables and functions.

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

So that we don’t need to duplicate functions in every class where there is a is-a relationship

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

Specific occupations could inherit from a Person class

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

It is the class being inherited from (Parent class)

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

public Classname: public Superclass name

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

It can additional member variables and member functions

  1. Why do we need inheritance in C++?
    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.
    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.

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

  4. 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.

  5. In what ways can a subclass extend the functionality of a superclass?
    The subclass can add new methods and new variables or override existing methods.

  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.

  3. Vehicle
    1.1 Car
    1.1.1 Volvo
    1.1.2 Audi
    1.2 Motorcycle
    1.2.1 Suzuki
    1.2.2. Yamaha

  4. 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. Why do we need inheritance in C++?

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

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

A: Sports >> NASCAR >> Basketball >> Football ect…
Medical >> Doctor >> patient >> Insurance ect…
School >> student >> teacher >> subjects ect…

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

A: Superclass is the class giving the inhertance and subclass is the class that is doing that inheriting.

  1. How would you write a public inheritance in C++?
    A:

class BasketballPlayer : public Person

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

A : Subclasses are full-fledged classes, they can (of course) have their own members that are specific to that class.we can create a set of reusable classes that are very general (at the top) and become progressively more specific at each level of inheritance.

1. Why do we need inheritance in C++?
Normalization and de-duplicating of data through a hierarchy of objects.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Zoology with animal categories. Food with type.

  1. What is a superclass and what is a subclass?
    A superclass is an normalization of the characteristics of things that could potentially apply to other things. A subclass is something that is a member of a something that characterizes a group of two or more things.

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

  3. In what ways can a subclass extend the functionality of a superclass?
    Through inheritance a subclass inherits all that public or protected variables and functions of the parent class.

  1. Why do we need inheritance in C++?
    Inheritance allows us to acquire properties and methods from other objects and then extend them. This could be useful where you need classes that perform different tasks, but are similar. In this situation, it would make sense to inherit common properties and methods from a base class.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    SUV, Roadster, Truck, Coach, Digger and Motorcycle all inherit from Vehicle.

Reading assignment 8 part 2…

  1. What is a superclass and what is a subclass?
    The superclass is inherited from whereas the subclass is doing the inheriting.

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

  3. In what ways can a subclass extend the functionality of a superclass?
    A subclass will have its own properties and methods which are not available within the superclass. Once the subclass inherits from the superclass, it will have all the properties and methods of both with the option to override existing methods.

  1. To save time and don`t write code more than once. We can reuse the code from parent class to use in child class. If the parent class adds functionality in the future, the child classes that are beneath it will derive these new attributes from the parent class. In this manner we can choose which classes will be affected by the changes or not based from our class three structure.

  2. A parent class Snowboard with variables boots size, pants size, bindings.
    Child class could be Burton and Forum.

  3. The super class is the parent class which the subclass or child class derives/inherit the values from.

  4. class Burton : public Snowboard
    {

    };

  5. A child class can inherit the functionalty from the parent class and use this reference class to get the same functionality and data as the original class. This way we can use the same functionality without having to rewrite the code, we don`t need to start over, but we can change if public and build upon existing classes dawg!

Part 1.

  1. To simulate real life objects where properties are inherited as well.
  2. coins and crypto-currencies are inherited from currency.

Part 2.

  1. It’s like a parentclass and a child class, where the child inherit all the variables and member functions.
  2. class Supervisor: public Employee
    {
    public:
    long m_overseesIDs[5];
    Supervisor()
    {
    }

};
3. They are able to inherit the superclasses capabilities and yet include more methods or override existing methods and variables