Inheritance in C++ - Reading Assignment

Missed it, thanks Aleš!

Part 1

  1. Why do we need inheritance in C++?
    it can save us from a lot of typing like duplicating the same code in many classes
  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
1. Computer parts
     1.For graphical display and processing tasks
        1.Graphic card
            1.Integrated 
            2. Dedicated
               1.Asus
               2.Msi
               3. Gigabyte
               etc...
        2.Processor
            1. Intel
                1. Pention
                2. i versions 
             2. Amd
                1. Ryzen
           
2. Storage
   1.Hard disk
   2. SSD
   3. M.2 SSD  

Part 2

  1. What is a superclass and what is a subclass?
    Superclass is the class being inheritated and it on the top of the tree. Subclass is branch of that tree
  2. How would you write a public inheritance in C++?
    class Wheels: public Car{ … };
  3. In what ways can a subclass extend the functionality of a superclass?
    subclass have inharits funcionality of a superclass and can also have its own specific methods and fields
1 Like

Wheels would more probably be a variable inside the Car class, not inherit from it. Volvo would be a better example of an inheritance form a Car. :slight_smile:

  1. Why do we need inheritance in C++? To inherit properties of superclasses, can reuse members and methods while adding new more specific ones to the subclass

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. — fruit —> apple, orange

  3. What is a superclass and what is a subclass? — superclass is the class inherited from, sub class is the the class that took inherited properties. Superclass is base or parent, subclass is derived or child.

  4. How would you write a public inheritance in C++? class derived: public base { }; or class janitor: public person {};

  5. In what ways can a subclass extend the functionality of a superclass? — add members and methods specific to the subclass, override existing methods, but still inherit public and protected members and method of superclass

1 Like
  1. inheritance is needed in C++ to create complex classes which is based on “is-a” relationship model. And such we create objects that inherits properties from other objects. Inheritance allows us to reuse classes by having other classes inherit their members.
  2. A parent and their offsprings.
  3. A superclass is the class being inherited from also known as a parent class and the class doing the inheriting is the subclass also known as the child class.
  4. I would write a public inheritance by declaring the class after the name of the class a colon followed by the word public and the name of the class we want to inherit as shown below;
    class Apple : public Fruit {
    //member variables and methods in here;
    };
    5 A subclass can extend the functionality of a superclass by having its own functions and member variables as well as those functionalities and member variables of the base class.
1 Like
  1. Why do we need inheritance in C++?
    For constructing complex classes.

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

                               football                                             gymnastics
                               male football                                    female gymnastics
    
  3. What is a superclass and what is a subclass?
    Subclass inherits methods and variables from the superclass(parent).

  4. How would you write a public inheritance in C++?
    class subClassName : public className

3.In what ways can a subclass extend the functionality of a superclass?
Additional functions can be added on top of the ones inherited already from the superclasses.

1 Like
  1. Let’s say we have a class Animal and a class Dog. There might be some methods and variables that you want to inherit. Something they share. This way you don’t need to redefine everything.
  2. dogs inherit animals.
  3. subclass inherits superclass

class Dog : public Animal
{
lalalalala
};

  1. They inherit superclass’ members and methods and have their own specific members and methods on top of that.
1 Like
  1. Inheritance allows us to create objects that directly inherit properties of other objects. With this, we can minimise repeating code, make our code more succinct and improve overall structure.

  2. insects> arachnids>spiders>widow spiders

  3. A super class is the inherited class that the subclass is derived from

  4. class arachnid : public insects {…}

  5. Classes can be added on top of the subclass. These subclasses will therefore have its own member variables and functions in addition to those inherited from the superclass.

1 Like
  1. Why do we need inheritance in C++? to use the attributes of other objects to create new, more complex objects.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. Transportation > Vehicle > Style > Make > Model > Series

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

  4. How would you write a public inheritance in C++? using “public:” and the class name

  5. In what ways can a subclass extend the functionality of a superclass? by adding specific variables and functions to the subclass that are in addition to the functionality of the superclass

1 Like
  1. We need inheritance in C++ because it make it easier for programmers to instead of making individual variables or functions for every object, we can pull from variables previously written for past objects if the child class has the “is-a” relationship to the parent class.

  2. Apples and Bananas inherit from fruits as a base class.

  3. A superclass is the object that has the base variables and functions that would be passsed on to the subclass that has the “is a” relationship to the superclass. The subclass is the inheritor and the superclass is the doner.

  4. After the class declaration, we use a colon, the word “public”, and the name of the class we wish to inherit

class BasketballPlayer : public Person {};

  1. Because a subclass will have it’s own set of variables and methods within it’s own class, after a public inheritance from a superclass, the subclass would now have the variables and functions of the superclass as well as it’s own variables and functions thus making it now an extension of the superclass.
1 Like

1. Why do we need inheritance in C++? We can use the attributes and behaviors (is-a relationship) of other objects, without having to re-invent them, but then use and enhance them with new special properties to make new objects.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. Vegetables. Root vegetables. Leafy vegetables. Color. Nutritional value.

1. What is a superclass and what is a subclass? The class being inherited from (passing from) vs the class doing the inheriting (passing to).

2. How would you write a public inheritance in C++?
class subclassClass : public superclassClass

3. In what ways can a subclass extend the functionality of a superclass? By writing its own functions. It already inherited all the functions from the superclass.

1 Like

A
1. Why do we need inheritance in C++?
To create an “is-a” relationship with other objects through the acquiring of attributes and behaviors from them.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Krautrock, Kraftwerk, 70s disco, electropop, dance, house, techno, synthwave… so on…

B
1. What is a superclass and what is a subclass?
Parent class or base class originates subclasses, it borrows attributes to its child, Child Class inherits functions and variables from the superclass.
2. How would you write a public inheritance in C++?
After the word class and the childClassName declaration, we use a colon, the word “public”, and the name of the class we wish to inherit.
class memberSpecificationName : public BaseClassName
3. In what ways can a subclass extend the functionality of a superclass?
A subclass can utilize any function or variable from its base class through inheritance and can still add specific functions and member variables for its own definitions.

1 Like

part 1

  1. Inheritance allows objects to be created that directly inherit features from another object which limits repetition of code.
  2. When a house is built, it acquires all of the attributes a house would have such as number of walls, square feet, doors, wall paint color etc.

part 2

  1. A superclass is a classes parents class while the class that inherits features from a parent class is known as a subclass
  2. class Player : public Person
    {

}
3. By inheriting the functionality of their superclass and creating or defining their own functions or overwriting the superclass’s functions

1 Like

Why do we need inheritance in C++?
It is the second major way to construct complex classes, which models an “is-a” relationships between two objects. Inheritance creates new objects by acquiring from and then extending the attributes (functions and variables) of other objects.

Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
Mammal -> Dog, Cat, Dolphin, Bear
They all inherit the attributes of mammal: have fur/hair, live birth, fed milk, warm-blooded, etc.

What is a superclass and what is a subclass?
Superclass (parent or base class) is the class being inherited from. Subclass (child or derived class) is the class doing the inheriting.

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 you wish to inherit.

In what ways can a subclass extend the functionality of a superclass?
Within the subclass, you can add additional member functions and member variables on top of the ones already inherited from the superclass.

1 Like

Introduction to Inheritance

1. Why do we need inheritance in C++?
Inheritance allows us to create new objects by directly acquiring the attributes and behaviors of other objects and then extending and specializing them.

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

We have fruits as a base/parent class, then we have apples, bananas, cherries which inherit all properties which have fruits (in parent class) class like color, shape, weight, etc…

Basic Inheritance

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?
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 Like
  1. Why do we need inheritance in C++?
  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Part 1.
  3. Inheritance models an “is-a” relationship between two objects, which minimizes code duplication and error checking
  4. Kitchen:appliance:stove:burner.

What is a superclass and what is a subclass?
2. How would you write a public inheritance in C++?
3. In what ways can a subclass extend the functionality of a superclass?

  1. A subclass inherits methods and variables from a superclass
  2. class Appliance(subclass) : public Kitchen(superclass)
  3. Subclasses contain other members and methods which can also be overridden as required. Properties and functions added to the superclass will automatically be inherited by the subclass.
1 Like

1 a. Inheritence is the use of the properties of an existing object. This minimizes code repetition.
2 a. for example, if we wanted to create a bee object, we could initialize a bee’s specific properties. Then we could use the pre-existing properties of the object to which the bee is a subclass of…which in this case would be insect.

1 b. In the above example (2.), insect is the superclass, bee is the subclass.
2 b. class bee : public insect {
};

3 b. If we change or add new properties to the superclass, its existing subclasses inherit the changes/additions without they themselves having to be changed.

1 Like
  1. to create relationships between and simply the coding

  2. Phone - Mobile phone

1 Superclass is where it is called from - Subclass is where it’s called to.

2 class MobilePhone ; public Phone
{



};

3 when its adding something new to it.

1 Like
  1. It allows us to take attributes from objects and add more to it to make a new variable
  2. A good example would be personalizing characters with the same base attributes of a character type.
    3.A superclass is a class that is being inherited and a subclass is a class that is doing the inheriting of data.
  3. class SubClass: public SuperClass{}
  4. A subclass can have methods in which call on variables from superclasses and change them to certain values.
1 Like

Part 1:

  1. Inheritance allows us to acquire traits of another object and expand on it without having to redefine everything over again.
  2. New Cellphone has features of previous models yet has new features as well.
    Part 2:
  3. A superclass is the base class. A subclass is directly connected to the superclass and incorporates the superclasses functionality and builds on top of it.
  4. Class secondLayer: Public firstLayer{…};
  5. A subclass can add extra functions to an existing superclass because it inherits all functions of the the superclass as well as adding its own functions.
1 Like