Inheritance in C++ - Reading Assignment

  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.
    Games: Developers, Publishers, Designers, Release

  3. What is a superclass and what is a subclass?
    A superclass is the class being inherited.
    A subclass is the class doing the inheriting

  4. How would you write a public inheritance in C++?
    class AgeToPlay : public Games
    {
    public:
    int m_age{};

    AgeToPlay(int age = 0)
    : m_age{age}
    {
    }
    };

  5. In what ways can a subclass extend the functionality of a superclass?
    because the subclass will inherit the superclass and add some kind of functionality.

1 Like
  1. Why do we need inheritance in C++?
    For efficiency and simplification , since all attributes and behaviours from superclases are inherited by child clases reusing the code.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Superclass: Car
    Subclass: Audi A5
    Subclass: Volvo

  3. What is a superclass and what is a subclass?
    A subclass is a more generic class where subclasses inherit the attributes and
    behaviours from , adding or specialising some of them.

  4. How would you write a public inheritance in C++?
    As my example above -> class Audi: public Car

  5. In what ways can a subclass extend the functionality of a superclass?
    By adding extra attributes or function that extends the functionality of the superclass

1 Like

Introduction to inheritance

  1. Complex classes are constructed from simpler classes and types. Inheritance models an “is-a” relationship between two objects.
  2. A simple class could be Animal, the class Mammal could inherit from this class.

Basic inheritance

  1. The class being inherited from is called the superclass, the class that is inheriting from it is called the subclass.
  2. class Subclass: public Superclass{};
  3. A subclass inherits all the methods and attributes from the superclass and in addition adds extra attributes and methods of their own.
1 Like

Q: Why do we need inheritance in C++?
A: It helps us avoid code duplication, which is something we want to avoid as much as possible. In other words, as much as possible, we want to reduce the amount of code we have.

Q: Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
A: One example is fruit . . . blackberries, strawberries, bananas, oranges, etc. would inherit from fruit.

Q: What is a superclass and what is a subclass?
A: Superclass is the class from subclass inherits all the information (variables, functions, etc.)

Q: How would you write a public inheritance in C++?
A: Example: class Produce : public Fruit { …
};

Q: In what ways can a subclass extend the functionality of a superclass?
A: A subclass can add new variables, new functions on top of the ones already inherited from the superclass.

1 Like

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

A1. Inheritance allow us to create (child) objects that inherit properties from other (parent) objects with the benefit of reducing code.

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

A2. Parent (Base) Class: Human, Child (Subclass(s): Male & Female

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

A3. A class being inherited from is called the parent, base or superclass and the inheriting class is called the child or subclass

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

A4. Code syntax:

class <ChildClass> : public <ParentClass> {

...

};

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

A5. A subclass can extend the functionality of it’s parent class by optionally adding new properties, new methods and also overriding existing (parent inherited) methods

1 Like

Inheritance is a way to create complex classes. Inheritance models an “is-a” relationship between two objects. There is less duplication of code.

Colors
-Blue
-Yellow

Part 2

A superclass is a class being inherited from and the subclass is doing the inheriting.

class PrimaryColors : public Colors

A subclass receives the member functions and member variables of the base class and is then able to add new functions and member variables.

1 Like

PART 1

  1. Inheritance is needed in C++ for a lot of reasons like the following:
    a)Avoid duplicating code that already exists in the parent class
    b)Need not to redefine the information from the base class or parent class

  2. Animals - Birds - Eagles

PART 2

  1. A super class (can also be called base class or parent class) is where the sub class (can also be called derived class or child class) inherits both behaviors (member functions) and properties (member variables).

  2. After the class declaration, we use a colon, the word “public”, and the name of the class we wish to inherit. For example:
    class BaseballPlayer : public Person

  3. A sub class extends the functionality of a super class by inheriting both behaviors (member functions) and properties (member variables) of the super class and then the sub class can have its own members (functions and variables).

1 Like
  1. Why do we need inheritance in C++?
    Because It allows user to create a new class (derived class) from an existing class(base class).

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance
    Colors
    Red, blue, black, white, green, yellow, brown

  3. What is a superclass and what is a subclass?
    It goes like this: 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++?

#include

class Base {
public:
Base() = default;
virtual ~Base() = default;
virtual int x() {
std::cout << “Base::x()\n”;
return 41;
}

protected:
virtual int y() {
std::cout << “Base::y()\n”;
return 42;
}
};

class Derived : public Base {
public:
int x() override {
std::cout << “Derived::x()\n”;
return Base::y();
}
};

int main() {
Base* p = new Derived();
std::cout << p->x() << std::endl;
}

  1. In what ways can a subclass extend the functionality of a superclass?
    By adding, deleting or modifying variables or methods without changing the superclass.
1 Like
1. Why do we need inheritance in C++?  allows programmers to create new classes from an existing class (base class). 
  2. Give a simple example of objects in the real world that could 	be modelled in C++ using inheritance. We inherit certain properties 	from the class 'Human' such as the ability to speak, breathe, eat, drink, etc. We can also take the example of cars. The class 'Car' inherits its properties from the class 'Automobiles' which inherits some of its properties from another class 'Vehicles'.
  1. What is a superclass and what is a subclass? Super Class:The class whose properties are inherited by sub class is called Base Class or Super class. Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.

  2. How would you write a public inheritance in C++?
    class Cat : public Animal
    {
    …
    };

  3. In what ways can a subclass extend the functionality of a superclass? When a subclass extends a superclass, it acquires all the functionalities of the parent class …
    Add new members
    Add new methods
    Override existing methods

1 Like
  1. Why do we need inheritance in C++?
    Ans: Inheritance enables us to model an “is-a” relationship between two objects. Inheritance
    involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them. In essence, inheritance allows us to reuse classes by having other classes inherit their members.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Ans: 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. Technological products (computers, cell phones, etc…) inherit features from their predecessors (often used for backwards compatibility). For example, the Intel Pentium processor inherited many of the features defined by the Intel 486 processor, which itself inherited features from earlier processors. C++ inherited many features from C, the language upon which it is based, and C inherited many of its features from the programming languages that came before it.

Part 2

  1. What is a superclass and what is a subclass?
    Ans: In an inheritance (is-a) relationship, the class being inherited from is called the superclass whereas the class doing the inheriting is called the subclass.

  2. How would you write a public inheritance in C++?
    Ans: Example: class BaseballPlayer : public Person //Class BaseballPlayer publicly inheriting the Person class. After the class BaseballPlayer declaration, we 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?
    Ans: A subclass can extend the functionality of the superclass by adding additional behaviors (member functions) and properties (member variables) that it wants. A subclass automatically receives the member functions and member variables of the superclass through inheritance.

1 Like

Part 1

  • Inheritance allows us to construct complex classes with a “is-a” relationship between the objects. New objects are created by directly acquiring the attributes and behaviors of other objects and expanding on them.

  • furniture ->table ->dining table, furniture ->table ->coffee table
    furniture ->couch ->loveseat, furniture ->couch ->sofa

Part 2

  • A superclass is the class being inherited from and a subclass is doing the inheriting.

  • A public inheritance in C++ is written as follows:
    class subClasss : public superClass {};

  • A subclass can extend the functionality of a superclass by receiving the member functions and member variables of the superclass through inheritance. We can then add the functions and variables that we want.

1 Like

Reading Assignment : Inheritance in C++ (Answers)

Pt1.

  1. Allows programmer to model an “is-a” relationship between objects.
  2. Automobiles: sedan, coup, SUV.

Pt2.

  1. Superclass - the class being inherited from.
    Subclass- the class that is inheriting from the superclass.
  2. Class Automobiles : public vehicle
  3. By allowing to add new variable and functions.
1 Like

The colon comes after the class name: Clas Automobiles : public Vehicle :slight_smile:

1 Like

A.

  1. Because inheritance allows us to create a new class that does have already an inherited attributes coming from the parent class. So this means it won’t be anymore time consuming to write a new class and then write again its member variables that are already declared on the parent class, thus it will not duplicate the code.

  2. A. Truck (Garbage truck, Dump truck, Cement truck)
    B. Asia, -> Southeast Asia, -> Philippines :philippines: :slight_smile:
    C. Ship (Cruise ship, Cargo Ship, Tanker ship, Container ship)
    D. Clothes (shirt, pants, underwear)
    E. Accommodation (Hotel, Motel, Inn, Hostel)

B.

  1. Superclass is the antecedent, it is the class where the subclass will inherit its attributes
    while the subclass is the heir descendant class that inherit the attributes of the superclass.

  2. syntax -> class subclassName : public superclassName{};
    ex:
    class BasketballPlayer : public Person
    {…};

  3. A subclass can have its own specific functionality independent from superclass by adding its own functions and member variables but at the same time the subclass automatically has a built in attributes that comes from the superclass, so this means even though a subclass has already its own specific features it can still extend the functionality of the superclass by means of inheritance.

1 Like

1.1 We need inheritance in order to construct more complex and specialized classes. Using inheritance we can branch out into different classes which themselves can branch out into other classes and thus make a complex system without using as many resources.(I think)

1.2 Sports => Basketball
_________=> Football => Men’s football
__________________=> Women’s football

2.1 A superclass(parent) is the main class where all the subclasses(children) branch from.

2.2 Class Child : Public Parent

2.3 Using a subclass, you can get all the functionalities of a superclass while being able to add new ones without modifying the superclass.

1 Like

What happened with the rest of your homework? :stuck_out_tongue:

1 Like

:)) Sorry teacher!

I accidentally pressed CTRL+Enter instead of enter then had to move away from the computer for a while. Its fixed now :smiley:

1 Like
  1. Inheritance allows us to avoid rewriting the same properties over and over for objects that share them. It’s also useful because you can modify something inside of a parent/superclass, and have these changes be reflected for all child objects that inherited those properties, instead of going one by one to each object and modifying them.
  2. Fruits - each individual fruit would inherit member variables and functions from a parent class called “fruit”.
  3. A superclass is the object from which properties are inherited by the subclass.
  4. you first write the new class you are declaring, and then a colon followed by public superclass.
  5. A subclass adds new attributes that make for a more specialized object, while still retaining the overarching hierarchical properties of the parent object.
1 Like
  1. Why do we need inheritance in C++?
    We need to create new objects by “inheriting” their essential characteristics. So for example apples and bananas ‘inherit’ the characteristics of fruit object.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    1 - Animals
    1.1 - Birds
    1.2 - Insects
    1.3 - Fishes
    1.4 - Rectiles
    1.5 - Mammals
    1.5.1 - Canidae
    1.5.1.1 Dogs
    2 - Vegetables

  3. What is a superclass and what is a subclass? A superclass is a class on superior level from which the subclasses will inherit; so in the first example “fruits” is the superclass and “banana” / “apple” are the subclasses

  4. How would you write a public inheritance in C++?
    class banana : public fruits
    {
    …
    };

  5. In what ways can a subclass extend the functionality of a superclass?
    A subclass will ‘inherit’ the functionalities of the superclass, even if it can also have its own variables and functions.

1 Like

11.1
1. Why do we need inheritance in C++?
We can use it to represent an “is-a” relationship between objects.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Car -> (Ford, BMW). Laptop -> (Dell,Apple,Asus)

11.2
1. What is a superclass and what is a subclass? The class being inherited from is the superclass, and the class doing the inheriting (the inheritator? :slightly_smiling_face:) is the subclass
2. How would you write a public inheritance in C++?
class Ford : public Car { …
3. In what ways can a subclass extend the functionality of a superclass?
The subclass acquires the member functions and variables from the superclass. You can also add new member functions to the subclass

1 Like