Inheritance in C++ - Reading Assignment

PARTI
1. Why do we need inheritance in C++ ?
To create new objects by acquiring the attributes and behaviours of other objects and then extending of specializing them.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
For example, a tablet, a cell phone and a PSP which inherit hardware and software features and functions of a PC.

PARTII

1. What is a superclass and what is a subclass?
It is a parent class or base class from other classes, child classes, inherit.
2. How would you write a public inheritance in C++?
class superlassName : public childClassName
3. In what ways can a subclass extend the functionality of a superclass?
• It doesn’t have to redefine the information from the base class.
• It automatically receives the member functions and member variables.
• It can add additional functions or member variables
• If the superclass is updated, all its children will be updated automatically.

1 Like
  1. Inheritance allows us to combine common data and behaviour in a common class and specific traits into child classes.

  2. Fruits as a parent, and apple and banana as child.


  1. In an inheritance (is-a) relationship, the class being inherited from is called the parent class, base class, or superclass.

  2. class BaseballPlayer : public Person{
    …
    }

  3. Within the subclass, you can add additional member functions and member variables on top of the ones already inherited from the superclass.

1. Why do we need inheritance in C++?
It makes it easier to reuse code and see the relationship between objects. It also helps in the design process for complex programs.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Lambo and Pulsar inherit from Car.

I. What is a superclass and what is a subclass?
Superclass is the parent class and the subclass is a child class that inherits properties and methods from it’s parent.
II. How would you write a public inheritance in C++?

class ChildClass : public ParentClass {
};

III. In what ways can a subclass extend the functionality of a superclass?
By defining its own custom properties and methods.

Part 1:

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

Inheritance in C++ allows for the creation of subclasses whose general properties come from more general classes. This allow to not redefine code by simply using inheritance.

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

Vehicles could be a class with properties such as size, colour, etc while subclasses inheriting those properties can be cars, places, boats…

Part 2:

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

A superclass is a class passing inheritance to its subclasses. A subclass receives inheritance from its superclass.

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

class SubClass: public SuperClass {

}

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

A subclass inherits all the functionalities of a superclass but also contain any new functionalities specific to this subclass and making use of the properties of the superclass.

Inheritance in C++

  • We need Inheritance to create new objects by directly acquiring the attributes and behavior of other objects (like cats inherit properties and behavior of animals

  • cats and dogs acquire properties and behaviors of animals. Programming language c++ inherits of C.,...

  • a subclass inherits all attributes and methods of its parent class (superclass) so you don't need to re-declare all variables and functions.

  • class Cats :public Animals {..};

  • You can add or override variables and functionality with public or private keywords

  1. Inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them.
  2. Example of objects using inheritance in the real world: - animal :lion: - bird :eagle: - chicken :rooster: - egg :hatching_chick: -
  1. A superclass or parent class is the above class in a hierarchy. Subclass or child class is the below class in a hierarchy. The child class inherits both behaviors (member functions) and properties (member variables) from the parent.
  2. Example of public inheritance: class BaseballPlayer : public Person { … };
    After the parent class declaration, we use a colon (:), the word “public”, and the name of the class we wish to inherit.
  3. Subclass extends the functionality of a superclass with the addition of new member functions and member variables on top of the ones already inherited from the superclass.
1 Like

1. Why do we need inheritance in C++?
So we do not have to duplicate code in an object that has same properties as some other class.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
men shoes inherit from shoes, car inherits from wehicle, slice of bread inherits from loaf, etc.

1. What is a superclass and what is a subclass?
Superclass is a class that is being extended by a sublcass.
2. How would you write a public inheritance in C++?
class Somthing() : public SomethingSuper();
3. In what ways can a subclass extend the functionality of a superclass?
It can add new methods and variables or override existing methods.

1 Like
  1. We need Inheritance in C++ because it allows us to create objects and further specialize their attributes and behaviors that we acquired from other objects.

  2. A real world example that could be modelled in C++ using inheritance: Men and women are humans

  3. 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++
    class Human
    {
    public:
    //variables
    //function() {}
    };

  5. A subclass extends the functionality of a superclass because it acquires the member functions and variables from the superclass.

Part 1

  1. Inheritance allows us to ensure backwards compatability by inheriting the traits of the parent class. It allows us to extend the functionality of the parent class.
  2. Fruits, such as apples and bananas, i.e. specific kinds of fruits may be modelled through C++ using inheritance.

Part 2

  1. A superclass is the class being inherited from. A subclass is the class doing the inheriting.
class ClassName: public ParentName
{
    public:
        //code goes here
};
  1. A subclass may utilize the basic methods in it’s superclass, and add more specific methods to the class, or change methods of the superclass.

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.
Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
General insurance products and Life insurance products both inherit member variables from insurance products
What is a superclass and what is a subclass?
The class being inherited from is called the superclass, and the class doing the inheriting is called subclass.
How would you write a public inheritance in C++?
class subclass: public superclass

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

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. This can not only save programmers time (no need to reinvent the wheel), but it also adds extra functionality around backwards compatibility, hierarchy, inheritance, shared behavior, etc.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Fruit. Consider apples and bananas. Although apples and bananas are different fruits, both have in common that they are fruits. And because apples and bananas are fruits, simple logic tells us that anything that is true of fruits is also true of apples and bananas. For example, all fruits have a name, a color, and a size. Therefore, apples and bananas also have a name, a color, and a size. We can say that apples and bananas inherit (acquire) these all of the properties of fruit because they are fruit. We also know that fruit undergoes a ripening process, by which it becomes edible. Because apples and bananas are fruit, we also know that apples and bananas will inherit the behavior of ripening.

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

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

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

Why do we need inheritance in C++?
to reuse code of the more general case of objects while specifying properties of more specific case of objects. Saves time because 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!

Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
car - bmw, toyota, ford
dwelling - house, condo, mansion, studio

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

How would you write a public inheritance in C++?
for the Employee class publically inheriting from the Person class:
class Employee: public Person

In what ways can a subclass extend the functionality of a superclass?
any way it wants… mostly adding methods and properties… or overriding superclass methods

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

Because a lot of object may have similar features and inheritance saves in typing space and memory

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

animals: all animals are alive, all animals have a heart, all animals have a brain, all animals need air.
animalsWith2Legs: chicken, duck, human
animalsWith4Legs: dog, cat, bear

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

superclass is a parent class
subclass is a more specific class that inherits from a superclass

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

define a class then add a colon (:slight_smile: and the name of the class you want to inherit from

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

by making it more specific

  1. Why do we need inheritance in C++?
    Inheritance creates objects which can inherit attributes and behaviors of other classes

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Object cars, bikes, trucks has a common object Vehicle from which it inherits some properties and characteristics.

  3. What is a superclass and what is a subclass?
    The class out of which other classes inherits are called superclass and the class who inherits from such class are called subclass

  4. How would you write a public inheritance in C++?
    Class Cars : public Vehicle

  5. In what ways can a subclass extend the functionality of a superclass?
    By adding its own members and functions in addition to the ones it has already inherited

PART 1

Why do we need inheritance in C++?
To easily create new objects that are similar as allready existing objects Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Vehicles <-- cars <-- bicycles <-- skateboards

PART 2

What is a superclass and what is a subclass?
superclass is a class where we inherited from, and a subclass is a class that inherited from de superclass

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

<subclass> : <superclass>

In what ways can a subclass extend the functionality of a superclass?
by adding more functions to the subclass or more variables

Classes may have many fields and/or methods in common. Inheritance allows us to reuse the common fields and/or methods in each class that shares them. For example, a Car and a Truck could both inherit many behaviors from a common LandVehicle class, such as accelerate(), brake(), and turn().

A subclass inherits from a superclass. A subclass can add new fields or methods to a superclass, or override a method of a superclass to change the details of what the method does.

class Subclass: public Superclass {

}

1 Like
  1. To extend the functionalities of predefined classes to the newly defined class.
  2. Fruits > Apple, Banana

Second part

  1. Superclass is which is inherited from and Subclass is which inherits.
  2. class Sub: public Super {
         //contents: additional functionality
    };
    
  3. By adding additional functions and variables.
1 Like

Why do we need inheritance in C++?
ease of passing feature of objects to new objects.
Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
shapes->quadrilaterals ->rectangle
What is a superclass and what is a subclass?
Parent class and Child class. The superclass is the on from which the subclass is derived
How would you write a public inheritance in C++?
class square : public Shapes
In what ways can a subclass extend the functionality of a superclass?
can add new vars and methods and even inherit from other classes.

1 Like

1. Why do we need inheritance in C++?
To reuse the same members or methods in derived class and to use higher lever of abstraction.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Fruit and Apple - base class might be a Fruit with Color member and derived class can be Apple with inherited member Color since an Apple.

1. What is a superclass and what is a subclass?
The superclass is in another words a base class or parent class - it is a class which other classes derive from. The subclass is in another words a derived class, child class - it is a class which has a superclass from which it derive.
2. How would you write a public inheritance in C++?
class Subclass : public Superclass
3. In what ways can a subclass extend the functionality of a superclass?
It can contain other members and methods or the existing methods can be overridden.

1 Like

Introduction to inheritance
1. Why do we need inheritance in C++?
Inheritance allows us to create objects based on the qualities another object has. These ‘child’ objects effectively inherit properties and behaviours belonging to their ‘parent’ object.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
One real life example could be a Gran Turismo Maserati, which is a Car, which is a Motor vehicle.
A Gran Turismo Maserati inherits properties from a Car, such as 4 wheels and a steering wheel, as well as what a Car inherits from a Motor vehicle, such as an engine.

Basic inheritance in C++
1. What is a superclass and what is a subclass?
A superclass is the class the contains the properties that will be inherited.
A subclass is the class that inherits the properties from the superclass.

2. How would you write a public inheritance in C++?
When defining the class, we type a colon after the class name, followed by the public keywords and the name of the superclass we will be inheriting from.
e.g. class Bird : public Animal {

3. In what ways can a subclass extend the functionality of a superclass?
While a subclass inherits all the functionality and properties of its superclass, it can also be given new properties and functionality on top of those. What’s more, if other properties and functionality is added to the superclass at a later time, they will automatically be inherited by the subclass.

1 Like