Inheritance in C++ - Reading Assignment

1. Why do we need inheritance in C++?
Inheritance allows us to build upon what has already been established. You can add more specifics while using the same functionality.

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

Part 2:

1. What is a superclass and what is a subclass?
The superclass would be the parent and the subclass would be the child. The child inherits attributes from the parent.

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

class Person
{
public:
std::string m_name;
int m_age;

std::string getName() const { return m_name; }
int getAge() const { return m_age; }

Person(std::string name = "", int age = 0)
    : m_name(name), m_age(age)
{
}

};

// Employee publicly inherits from Person
class Employee: public Person
{
public:
double m_hourlySalary;
long m_employeeID;

Employee(double hourlySalary = 0.0, long employeeID = 0)
    : m_hourlySalary(hourlySalary), m_employeeID(employeeID)
{
}
void printNameAndSalary() const
{
    std::cout << m_name << ": " << m_hourlySalary << '\n';
}

};

int main()
{
Employee James(20.25, 12345);
James.m_name = “James”; // we can do this because m_name is public

James.printNameAndSalary();
return 0;

}

This Prints: James 20.25

3. In what ways can a subclass extend the functionality of a superclass?
The subclass can add more detailed and specific information.

1 Like
  1. We can build objects inheriting properties of other objects. This reduces a lot of coding necessary to make everything work.

  2. Apples and bananas are both fruits.

  • Superclass: the class being inherited from.
  • Subclass: the class doing the inheriting.
  1. Aftert the declaration, use a colon, the word “public”, and the name of the class we wish to inherit.

  2. A subclass can create new variables and methods, or can overwrite existing methods.

1 Like
  1. Inheritance allows us to create new objects by directly acquiring the attributes and behaviors of other objects.
  2. Building -> house -> flat -> room

SECOND PART:

  1. Superclass: parent class that inherits to the child class
    Subclass: child class inheriting from the parent class
  2. class Vehicles : public cars
    {

};

  1. By adding new methods and new variables
1 Like

Part 1

  1. Inheritance is used to pass down properties of an object allowing us to build upon previous objects with greater specificity.
  2. Food could be modeled using interitance. Each type of food would hold properties such as type of cuisine, preparation time, difficulty levels, and the ingredients it requires.

Part 2

  1. A superclass passes its properties onto the subclass, showing the directional flow of inheritance.
  2. class “superclass” : public “subclass”
  3. Subclasses inherit all the values and methods of their superclasses and can additionally hold more values and methods specific to their classes.
1 Like
  1. It allows for the creation of new object with inheritance attributes and properties of other objects. Providing the better reuse of code and avoids duplication.
  2. For example : Electronic goods -> computer -> laptop -> Macbook
  1. A superclass is a class being inherited or parent class. A subclass inherits the functions and properties from parent class.
  2. class Subclass : public Superclass{
    }
  3. It includes all variables and functions of the superclass and also its own variables, functions.
1 Like

1. Why do we need inheritance in C++?
So that we acquire properties from other objects. This way we don’t have to redefine properties that we will constantly reuse.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Java and Python inherit from programming languages.

1. What is a superclass and what is a subclass?
A subclass is the one who inherits from the superclass.

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 extends functionality by inheriting all its function and adding unique functions of its own.

1 Like

Why do we need inheritance in C++?

Inheritance is really useful in C++ because

  1. it can save us a lot of typing in some examples and instead of duplicating code in many classes child classes can inherit functions and variables from a parent class.
  2. It can allow us to start data structures in a really generalized form to start with and as we develop our data structure we can get really specific in subclasses.
  3. If we want to add extra functionality to our structure we can add it into a super class and all child classes automatically inherit the new feature without having to duplicate across multiple classes.

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

For my example i will use the science of taxonomy for an example of hierarchy;

    Kingdom ==> Phylum ==> Class ==> Order ==> Family ==> Genus ==> Species

What is a superclass and what is a subclass?

  1. A superclass is also known as a base class or parent class and is the originating class in a heirarchy.
  2. A subclass is a child class of a superclass or a child class of another child class.

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

class Person {
   public:     // variables and functions need to be public
   std::string m_name;
   getName(std::name = "name")
   m_name(name);
};
class hobby : public Person {    // use public Person to link inheritance.
   std::string m_hobby;
   getHobby(std::string hobby) {
   m_hobby = hobby
 }
};

In what ways can a subclass extend the functionality of a super class?

A sub class allows us to focus on a subset of data of a super class and we can become very specific about the data in that structure so if at some stage we need to add a new class of data which doesn’t actually fit correctly in a super class we have a mechanism to add the object easily and cleanly.

1 Like

Excellent answers sir, well documented! Please keep them like that :muscle:

Carlos Z.

1 Like

To reuse variables and functions in other classes.
Make the code clean and avoid repeating and create a better maintenance.

For me as a teacher
Wr can create a class Person which contains two variables name and age.

Then we can create two subclasses one for teachers and the other for students
Both subclasses will inherit the properties of the person class.

Superclass is the more general class or the basic class example: class person.
While subclass is the inherited class which is a child class from the superclass and it is more specialized.

A subclass has all member variables and functions of the superclass plus its own variables and funcrion.
Example: superclass person has name and age only.
While subclass student has name and age of superclass person plus for example student’s grade, school, gender…etc.

1 Like

1.Why do we need inheritance in C++?
It is not efficient to repeat code. Improves structure.

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

  1. Phone

    1. Android
      1. Galaxy
      2. Note
    2. IPhone
      1. Iphone 10
      2. Iphone 8
  2. What is a superclass and what is a subclass?
    A super class is also the parent class. A subclass is also the child class. The subclass inherits from the superclass.

  3. How would you write a public inheritance in C++?
    After declaring a class you would write “public” then the name of the class you want to inherit from.

class Employee: public Person

  1. In what ways can a subclass extend the functionality of a superclass?
    The child inherits the members and properties from the parent class. It can extend the functionality because it can get more specific. For example, every baseball player is a person and every pitcher is a baseball player, but there are things that a pitcher does that not every baseball player does.
1 Like

1. Why do we need inheritance in C++?
To reuse variables and functions in other classes.
Make the code clean and avoid repeating and create a better maintenance.

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

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 Person {
   public:
   std::string m_name;
   getName(std::name = "name")
   m_name(name);
};
class hobby : public Person { 
   std::string m_hobby;
   getHobby(std::string hobby) {
   m_hobby = hobby
 }
};

3. In what ways can a subclass extend the functionality of a superclass?
A subclass allows us to focus on a subset of data of a superclass and we can become very specific about the data in that structure so if at some stage we need to add a new class of data which doesn’t actually fit correctly in a super class we have a mechanism to add the object easily and cleanly.

1 Like

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

It is needed to increase efficiency in the development of higher abstraction layers

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

atom > molecule > compound > protein > cell > tissue > organ > object human > sentient human

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

a superclass is that which has the functions and properties of its members

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

I believe you declare the keyword ‘public’ ?

3. In what ways can a subclass extend the functionality of a superclass?
[/quote]

by creating additional functionality to that which it inherited?

1 Like

I think you switched that :slight_smile: subclass inherits members from the superclass. Like you inherit properties from your super parents :stuck_out_tongue:

1 Like

yeah, I get that mixed up. when I think ‘sub’ you think below but since you are inheriting properties from super parents you actually are ‘above’ them. still hasn’t sunk in, don’t like the naming system but hey…who am i

1 Like

Part 1:
1. Why do we need inheritance in C++?
Inheritance is a way for objects to acquire attributes and behaviours (variables and functions) of other objects without having to redefine each of the members. This means the programmer can reduce the amount of unnecessary or repetitive code.

2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
A piece of furniture is a good example of an object that could be modelled in C++ as just about any piece of furniture inherits some basic attributes, for example: type, colour, use. Methods could also be created in order to ‘get’ or ‘set’ the data within the class for use elsewhere. Depending on the type of furniture, further delineation of attributes could occur, for example if the furniture type was table; then shape, height, length and width could be added as basic attributes ‘of table’ (which is ‘of furniture’). You could then go further and have ‘Wooden’ as a class, which not only inherits the ‘type, colour and use’ attributes from the furniture class, but also the ‘shape, height, length and width’ attributes from the table class, then the Wooden class could apply a new set of members on top of all of that which could include the wood type and the stain or coating type and so on. This class ‘Wooden’ could even be used not just as a subclass for tables but as a superclass or subclass for other furniture types like chairs, wardrobes etc.

Part 2:
1. What is a superclass and what is a subclass?
A superclass is another name for a parent class, which is to say the class that is having its member variables and functions inherited by another class. A subclass, or child class, is the class that is inheriting the member variables and functions, to which it can add its own members. A class can be a superclass, for one or more other classes, whilst simultaneously being a subclass of another. This is what is referred to as an inheritance chain.

2. How would you write a public inheritance in C++?
To use public inheritance in C++, you first define the type ‘class’ followed by the class name, then you put a colon followed by the keyword public and then the name of the class you are wanting to inherit from.
For example:

class Table : public Furniture {   ....   };

3. In what ways can a subclass extend the functionality of a superclass?
A subclass can extend the functionality, of a superclass, by adding its own members (variables and functions).
For example, a class of Clothing might include the member variables: size and colour and methods of obtaining and/or returning or printing these variables, whereas the subclass of Pants, will inherit the size, colour and any functions defined in the Clothing class, but may also include: waist size, length and style variables as well as functionality to get or set these variables, etc.

1 Like

Part I
1. Why do we need inheritance in C++? - Inheritance allows the reuse of classes by having other classes inherit their members. This makes it so it is not necessary to redefine properties from a derived class.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance? - Football and Basketball inherits from Sports

Part II
1. What is a superclass and what is a subclass? - the superclass is the class being inherited and the subclass is doing the inheriting.
2. How would you write a public inheritance in C++? - class Football : public Sports
3. In what ways can a subclass extend functionality of a superclass? - The subclass can add new methods and new variables to override existing methods.

1 Like

1. Why do we need inheritance in C++?
Inheritance allows us to use the variables and functions of the base class without duplicating code

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

  shape
    polygon
        pentagon
        hexagon
    triangle
        isosceles triangle

1. What is a superclass and what is a subclass?
The superclass is the parent class from which a child class does inherit from.
The subclass is the child class which inherits from a parent class.
Therefore the subclass does inherit from the superclass, which means the superclass is the parent class to the subclass.

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

3. In what ways can a subclass extend the functionality of a superclass?
The subclass can add new members and new methods to the superclass as well as override existing methods in the superclass.

1 Like
  1. Why do we need inheritance in C++?
    To add variety using an “is-a” relationship between two objects.
    Allows direct acquisition of attributes/behaviors which can be further specialized.
    More comparable with relationships in reality.

  2. Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
    The biological hierarchy- domain, kingdom, phylum, class, order, genus, species.

  3. What is a super class and what is a subclass?
    Super class is also called Base or Parent class.
    Subclass is also called Inheriting or Child or Derived class.

  4. How would you write a public inheritance in C++?
    class Gamer : public Person //Gamer class is Subclass “:” of Super class Person.

  5. In what ways can a subclass extend the functionality of a super class?
    No need to redefine information from base class into derived class, received automatically.
    Also means updates and modifications to base class automatically pass to subclass’s.

1 Like
  1. Why do we need inheritance in C++? Inheritance in C++ allows the modeling of “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.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance. A student class can inherit from a person class. Specific attributes can then be added to the student class, specific to the student type.

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

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

class Student : public Person

{

public:

int studentId;

string major;

}

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

In addition to the members inherited from the superclass, the subclass can define members and methods of its own.

1 Like

Inheritance in C++

  1. Why do we need inheritance in C++?
    Inheritance allows us to define a class in terms of another class, which makes it
    easier to create and maintain an application. 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. One example of inhertance in the real world is the product development in smart phones. Smart phones today inherated the basic functions and applications that were developed from it’s early predecessors. Most of the basic functions progressed and are an improved design compare to earlier models.

  3. What is a super-class and what is a subclass?
    a.) Super-class(base class); The class whose properties are inherited by sub class is called base class or Super-class. Data is being inherited from the super-class function.

    b.) Subclass(derived class); The class that inherits properties from another class is called Sub class or Derived Class. The subclass is doing the inheriting.

  4. How would you write a public inheritance in C++?
    First write the class statement, enter a colon and then write public followed by
    the function declaring the inheritance.
    //Example;
    class className : public publicName

  5. In what ways can a subclass extend the functionality of a superclass?
    Using inheritance, we write the BaseClass properties only one time, instead of multiple times for other different class. This allows us to only maintain the properties from the BaseClass one time, and allow the subclass to adapt or take the new changes. This means all the subclass will automatically inherit the new changes.
    Inheritance allows us to reuse classes by having other classes inherit their members. This eliminates writing new class properties for other subclasses. Using the inheritance methods saves time when modifying or updating the superclass.

1 Like