Inheritance in C++ - Reading Assignment

Part I

  1. Why do we need inheritance in C++?
    To create new objects by acquiring the attributes and behavior of other objects and then expand them and build more complex classes.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    There are many. For example, a vehicle is a car and a car is also a vehicle, and a motorbike is a vehicle and a vehicle is also a motorbike. But a motorbike is not a car and a car is not a motorbike. These relationships are important for a program to distinguish between and also make code reusable.

Part II

  1. What is a superclass and what is a subclass?
    a subclass is the class inheriting from a superclass.

  2. How would you write a public inheritance in C++?
    Motorbike is a Vehicle
    class Motorbike : public Vehicle {…};

  3. In what ways can a subclass extend the functionality of a superclass?
    A subclass can extend the implementation of a superclass with additional fields and functions, to create a more specific object.

Answer:

    1. Superclass is a class whose members are inherited by the subclass.
    1. class Superclass : public Subclass
    1. A subclass can create a specialty for narrower use of a superclass.
1 Like
  1. Why do we need inheritance in C++?
    Because it allows us to create new objects derived from existing ones (acquiring their attributes and behaviors and extending them). Inheritance also allows to construct abstractions resembling the real world.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    A vehicle and a motorbike (which is a special class of vehicle).


  1. What is a superclass and what is a subclass?
    A class from which other classes (a subclass) will derive from.

  2. How would you write a public inheritance in C++?
    Using : and the word public after the class declaration:

class DerivedClass : public BaseClass
  1. In what ways can a subclass extend the functionality of a superclass?
    Adding new members, creating new methods and overriding existing methods.
1 Like

1. 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?

Hello sir, did you forgot to type the 2nd part of the questions??

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

Article 1:

  1. Inheritance in importance in C++ because allows for “is-a” relationships between two objects. Inheritance means that objects can directly acquire attributes and behaviors of other objects, and then “extending or specializing” them.

  2. Objects in the real world that could be modeled using C++ inheritance could be a line of cars from a single manufacturer, and then all of the subsequent models of that same line which have come out after the original. All of them would share the fact that they are the same line of car and thus they have similarities like shape, maybe the amount of cupholders and seats, and the type of gasoline that they take, etc (which they would have inherited from previous cars in that line). The newer cars have upgraded features (more robust paneling on the outside of the car, cupholders that hold the cup better, nice seats, etc.) though they are still the same line of car and made by the same company.

Article 2:

  1. A super class (also called the parent or base class) is the class that is being inherited. A subclass (also called the child or derived class) is the class which is doing the inheriting.

  2. A public inheritance in C++ can be written as

class ClassName {

public:

      // members

};

  1. When functionality is inherited from the superclass, those inherited features can then be overwritten for something better (hopefully) or they can be defined in different ways that better suit the new functionality of the subclass.
1 Like

1. Why do we need inheritance in C++?
Inheritance makes it possible for certain classes to derive properties from other classes. Therefore we don’t need to define the same properties (member variables, methods) again in the derived class

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

molecule class can be derived form element class

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

A superclass is never derived form another class. All memeber variables and methods are defined in the superclass directly. A subclass is inherited form a superclass.

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

class SubClass :: public SuperClass{
// declare member variables SubClass
// declare methods specific to SubClass
};

3. In what ways can a subclass extend the functionality of a superclass?
A subclass can introduce unique methods and variables that cannot be accessed by other subclasses that are derived form the same superclass.
A superclass can contain general properties while the subclasses can include specific properties.

1 Like

1 - Why do we need inheritance in C++
Inheritance is a powerful feature which allows us to create sub-classes of existing classes. In this way, we can have general classes and more specific subclasses.

2 - Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
We could have a class Vehicles with a subclass Cars or Trucks, each of which would contain members of the Vehicles class but then have their own specific, additional members.

Part 2

1 - What is a superclass and what is a subclass?
A superclass is the class from which a subclass is created. So Vehicles would be a superclass and Cars and would be a subclass, and then Volvos could be a subclass of that.

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

class Cars : public Vehicles{

//code……

}

3 - In what ways can a subclass extend the functionality of a superclass?
When we change member variables or functions in a superclass, it can then make all of those changes available to all subclasses.

1 Like

I have posted them separately. Thank you for your attention.

1 Like

Ohhh yes, i see it now, sorry sir, my bad! thank you for noticing it!

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

  1. It’s easier to maintain an application. In that way, instead of creating new variables
    which will take up more storage, you can reuse the functionality with new
    values.

  2. Blockchain 101, Ethereum 101, Defi 101 and C++ programming can all
    be inherited from “class Courses”.

  3. A class that has been extended by another class. It is the class that is
    being inherited from. Also called parent class or base class.
    The subclass inherits the parent/base/super class.

  4. class Courses public Categories {

    };

  5. You can add new functions to the subclass and also overwrite the values of
    the variables or add new methods/overwrite existing methods

(btw, learncpp seems to be in maintenance but I hope I got it right anyway)

1 Like
  1. Inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them without the need to repeat what was already defined in the parent object.

  2. Elements of the periodic table and molecules composed of different elements through inheritance.

  1. In an inheritance relationship, the class being inherited from is called the superclass, and the class doing the inheriting is called the subclass.

  2. Right after the definition of the subclass, we would use a colon, the word public and the name of the superclass.

class Molecules : public Elements
  1. The subclass can add additional members and methods.
1 Like
  1. Why do we need inheritance in C++?
    It allows for a base class on which you can build and override to more specific objects.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Base type could be a building as the superclass , and house or apartment complex as subclasses.

  3. What is a superclass and what is a subclass?
    A superclass is the parent, and subclasses are derived from the base or superclass

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

class home: public building{
  public: 
  int size_sqft;
  int doors;
  string color;
...
}
  1. In what ways can a subclass extend the functionality of a superclass?
    Subclasses can start with the properties and methods of the base, but can add many more methods and properties that would be more specific to the subclass.
1 Like

Part1:

  1. To describe “is a “ relationships.
  2. Car > Manufacturer > Car Part Category > Specific Car Part

Part 2:

  1. A superclass is the parent class. A subclass is the child class.
  2. Class : public {}
  3. The subclass can be used to overwrite and add new variables to the superclass.
1 Like
  1. We need inheritance in order to increase programming efficiency: the specifications made in a superclass do not have to be repeated in a pertinent subclass, and hence the program size is reduced and the programmer has less code to write.

  2. All monkeys (subclass) are mammals (superclass) but not all mammals are monkeys. Consequently, any variable or function that is descriptive of a mammal in the superclass is also descriptive of a monkey, but there are additional variables or functions that are descriptive of a monkey in the subclass and that do not apply to mammals in general.

  3. The entities that are described by the variables and functions in the subclass are also described by the variables and functions in the superclass, but not (necessarily) vice versa: there can be entities that belong to the superclass but not to the subclass.

  4. I would use the following syntax:

    class supercl_name
    {

    class supercl_name
    {
    private:
    supercl_private_type_1 supercl_private_var_1;

    supercl_private_type_p supercl_private_var_p;
    protected:
    supercl_protected_type_1 supercl_protected_var_1;

    supercl_protected_type_r supercl_protected_var_r;
    public:
    supercl_f_type_1 supercl_function_1(supercl_type_1_1 supercl_var_1_1, …)
    {
    instructions for function supercl_function_1
    };

    supercl_f_type_q supercl_function_q(supercl_type_q_1 supercl_var_q_1, …)
    {
    instructions for function supercl_function_q
    };
    };
    class subcl_name: public supercl_name
    {
    private:
    subcl_type_1 m_subcl_variable_1;

    subcl_type_i m_subcl_variable_i;
    public:
    subcl_f_type_1 subcl_function_1(subcl_type_1_1 subcl_variable_1_1, …)
    {
    instructions for function subcl_function_1
    };

    subcl_f_type_j subcl_function_j(subcl_type_j_1 subcl_variable_j_1, …)
    {
    instructions for function subcl_function_j
    };
    };

  5. A subclass can extend the functionality of a pertinent superclass by adding further descriptive variables and functions to those contained in the superclass.

1 Like

Part I

  1. We need inheritance to describe hierarchies or to describe inheritance of properties of one object to another.

  2. rock and jazz inherits from music genres

Part II

  1. Superclass is the class being inherited from while subclass is the class being inherited to.

// Rock publicly inheriting musicGenre
class rock : public musicGenre
{
public:
    double ....;
    int ...; etc. 
};
  1. Apart from inheriting functionalities from the superclass, subclass then can add special functionalities specific to the subclass.
2 Likes
  1. Inheritance acquires attributes from another objects.

  2. Evolution of human race

  3. Superclass - the class whose properties are inherited by sub class
    Subclass- the class that inherits properties from another class

  4. class SubClass: public SuperClass

  5. New member functions, member variables

1 Like

Excellent answer sir! really well documented! keep it like that please! :muscle:

Carlos Z.

1 Like
  1. Inheritance allows reusing properties of methods, this helps to reuse code and less line code.
  2. Apple and banana inherit from fruits.
  3. The superclass is the class being inherited from and the subclass is the class doing the inheriting.
  4. class Car: public Mustang {};
  5. The class inherits all the functionalities of the parent class and we can update that functionality for different results in our child class.
1 Like

1. Why do we need inheritance in C++?
For programming efficiency. When attributes of the objects are acquired, less code needs to be used.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
All techno music (subclass) is electronic music (superclass), but not all electronic music is techno.

1. What is a superclass and what is a subclass?
A superclass is never derived form another class. All memeber variables and methods are defined in the superclass directly. A subclass is inherited form a superclass.
2. How would you write a public inheritance in C++?
class ClassName : public SuperClass
{

};
3. In what ways can a subclass extend the functionality of a superclass?
Subclasses can add many more descriptive variables and functions that would be specific to the subclass and extend its functionality.

2 Likes

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

Inheritance helps us to evaluate a class in terms of another class, making it easier to build and manage an application. It also offers an ability to reuse the features of the code and to apply it quickly.

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

I am a big fan of Apple computers then I will base my example on Apple computers. A new “updated” version of the MacBook Pro inherited looks, features of the previous version but on a top of it got some new features like stronger camera or touch bar.

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

The derived class (a class that is derived from another class) is called a subclass. The class from that which is derived is called the superclass.

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

I’m using an example provided from the text.

class Derived: public Base

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

A subclass inherits all of its members (fields, methods, and nested classes) from its superclass. Constructors are not members, because they are not inherited from subclasses, but the creator of the superclass may be invoked from the subclass.

1 Like