Inheritance in C++ - Reading Assignment

1.) Inheritance allows us to reuse classes by having other classes inherit its members.
2.) Meat would be the object modeled for inheritance. Meat would be the superclass and beef would be a subclass that is inheriting the traits of meat.
3.) If the object Meat contains the variables nonvegetarian, flesh, and food type and is the super class, then beef would be a subclass of meat bringing in the variables like roast, steak, tripe, lingua, heart, liver, etc.
4.) In order to write a public inheritance (in c++) , within your program, after your class place a colon, add the word public and then the name of the class you’d like to inherit the variables from the initial class.
5.) A subclass brings in its own variables, otherwise there would be no need to define it. By bringing in its own variables, it can add actions and function, adding to the functionality of a superclass.

1 Like

Part A

  1. Inheritance allow us to create objects that directly inherits properties of another objects. And so, we don’t have to redefine some properties in our derived class.
  2. cats and dogs inherits from animals

Part B

  1. The subclass is the class that inherits from the superclass.
  2. Class Food : public Rice{

…

}

  1. By inheriting all the functionality from the superclass and then defining their own specific (new) functions or override inherited (old) functions.
1 Like

You switched the order here, this would be read as if Food inherits from Rice. :slight_smile:

1 Like
  1. It allows us to create a new class derived class from an existing class, base class. The derived class inherits the features from the base class and can have additional features of its own.
    When a subclass is formed, it derives all the properties from the superclass. Therefore, the existing code is reused.
    There will not be any wastage of memory space because the same properties are inherited and not duplicated.
    This also leads to faster progression and development time.
    The code enhances maintenance and memory utilization.
  2. Gambling. You can play Blackjack, Mini Bacarrate, Spanish 21. All of them are games that involve buying in, placing your bets, and playing, but they all have different rules of play and different ways of handling cards.
    …
  3. A superclass is the parent class, the class being inherited from. A subclass is the child class, the class doing the inheriting.
  4. class Games
    { …
    };
    class Blackjack: public Games
    {…
    };
  5. The derived class inherits the members of the base class, on top of which it can add its own members.
1 Like
  1. Why do we need inheritance in C++?
    You can have classes that aren’t the same be categorized with a similarity.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    You have a general General class and you go more specific with the inheritance to show shared values.

I. Food
1.Grains
1A) Wheat
1A.a) Bread
1A.b) Pasta

1.What is a superclass and what is a subclass?
Superclass is the Major class while the subclass would be the minor class. The subclasses would have a relation of the superclass but not all subclasses would be the same.

  1. How would you write a public inheritance in C++?
    class Person {
    public:
    Person() {
    name = “”
    gender =
    age =

EDIT 2. How would you write a public inheritance in C++?
class Worker: public Person {
public:
Person() {
name = “”
gender =
age =
};
};
3. In what ways can a subclass extend the functionality of a superclass?
You can add more details in the subclass without changing the superclass

Here you defined a class with a public method and variables (you also forgot to define their type), but didn’t inherit from a class, a public inheritance is defined in the class definition:

class Worker: public Person {
  /* methods and variables */
}
2 Likes

1. Why do we need inheritance in C++?
To make code writhing more efficient and avoid repetition. Make programs backwards compatible.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Biological hierarchies like: domain, kingdom, phylum, class, order, family, genus, and species.


1. What is a superclass and what is a subclass?
The superclass will include the subclass. Or the subclass is originated from the superclass.
2. How would you write a public inheritance in C++?
Example:
class SubClass : public SuperClass
{
public:
xyz…

};
3. In what ways can a subclass extend the functionality of a superclass?
The subclass adds value to the superclass. Debugging is easier, since you use hierarchal orders of program modules.

1 Like
  1. It helps us to reuse code instead of repeating it and therefore keep our code DRY. Also it provides a way to build proper hierarchy between multiple classes and make sure all “children” got the correct logic from their “parents” which on it’s own opens up the polymorphism concept - when we know that every object we’re gonna receive is child of the class Person, we can conclude that they all contain a member function Breath.

  2. Food is a base class for Fruit and Vegetable - they are both eatable, have calories etc.

  3. Superclass is one that declares the members which every other class that inherits it will have. Subclass is one that inherits a superclass.

  4. class subclassName : public superclassName {

};

  1. By default every subclass inherits all members of the superclass and it can extend this functionality by simply adding more members (specific for the child class) or modifying/overriding the members received from the parent class.
1 Like
  1. Unlike object composition, which involves creating new objects by combining and connecting other objects, inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them. typically inheritance is everywhere in real world.

  2. iPhone 12 :wink: surely it got all the essentials of the previous ones

2nd part;

  1. the class being inherited from is called superclass and the class that it does the inheriting is called subclass.

a) food
a-1)starter
a-2 )salads
a-3)main dishes
a-4)desert

  1. subclass could add new methods, new variables (override methods too)
1 Like

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

It allows us to create a class with all of the same features and methods of a previous class, but we can add and remove bits as we see fit. This reduces the amount of code required.

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

Food can be broken down into dairy which can be broken down to cheese, milk, cream etc

2nd Article
1. What is a superclass and what is a subclass?

The class being inherited from is the superclass and the class inheriting is the subclass

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

class Cheese : public Dairy
{
}

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

The subclass can have it’s own methods and variables added on to those of the superclass

1 Like

<<Article 1>>

  1. Why do we need inheritance in C++?
    -> By having the inheritance, it will reduce the amount of code. And when you want to make a change in the variables or the function it will change all the places by changing one place.

  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    -> Car class with the attributes as the top and then have specific brands like Volvo which will have their own strength.

<<Article 2>>

  1. What is a superclass and what is a subclass?
    -> superclass is the parent of the subclass. Everthing defined in superclass will be used in subclass.

  2. How would you write a public inheritance in C++?
    => after the name of the class you want to declare you add : public and the name of the class you want to inherit.
    class Something :public Person{}

  3. In what ways can a subclass extend the functionality of a superclass?
    => By writing its own function in the subclass or overwriting the function determined in the superclass.

1 Like
  1. We need inheritance because it can reduce redundancy in our code. It also provides cleaner structure for complex objects and makes future modifications less time-consuming.
  2. One example might be mammals (superclass) and cats, dogs or hamsters (subclasses). All mammals have some common features that can be extracted to superclass, but hamsters differ quite a lot from dogs or cats, so each of those groups need separate subclasses.

Second part:

  1. The superclass is the class that is being inherited from whereas the subclass is the class that inherits features from the parent class.
  2. A public inheritance is done during declaration of the subclass by adding a colon along with public keyword and a superclass name, for example: class SomeSubclass: public SomeSuperclass.
  3. A subclass can have specific methods and fields independently from a superclass. At the same time the subclass has features from the parent class, so it extends the functionality of the base class.
1 Like
  1. Why do we need inheritance in C++?

It allows us to alter members fields and methods of child classes without altering the member fields and methods of the base class. This allows for reduced redundancy in code and greater efficiency when making adjustments to the source code.

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

Animals Kingdom

Ex: Basic Attributes > Sea Animals > Crustaceans/Fish/Mammals

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

The superclass is what provides the attributes to the subclass. In other words, the subclass inherits the properties of the superclass. Changes in the in the subclass do not affect the superclass, but changes in the superclass affect the subclass.

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

Use the (:slight_smile: then “public” keyword, followed by the class name

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

Subclasses build on top of the superclass. Methods can be added to the subclasses, as well as member values.

1 Like

Part 1
1 - To not repeat and replicate code and change it easily in the future if necessary;
2 - Monkeys and Humans both are animals. Some times both are irrational, sometimes only the Humans. :slight_smile:

Part 2
1 - superclass is the parent of the subclass. Everything defined in superclass will be used in subclass.
2 - class “SubclassName”: public “SuperclassName”
3 - A subclass can have specific methods and fields independently from a superclass. At the same time the subclass has features from the parent class, so it extends the functionality of the base class.

1 Like

1. Why do we need inheritance in C++?
It allows for the relationship of two objects to be related by 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.
Pastries, cakes, biscuits and bread are all baked goods using flour

1. What is a superclass and what is a subclass?
Superclass is being inherited from while subclass does the inheriting eg parent then child

2. How would you write a public inheritance in C++?
class Sport: public Basketball {};

3. In what ways can a subclass extend the functionality of a superclass?
By constructing an inheritance chain with the general information at the top we are then able to alter at each level as we progress

1 Like

1. Why do we need inheritance in C++?
Inheritance provides an efficient way to let us reuse code used to define one class in another class. It also allows an efficient mechanism by which changes or fixes to parent classes can be propagated to child classes.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
For example, if we want to create an Employee class and we already have a Person class, we can have the Employee class inherit from the Person class since an employee is also a person.

1. What is a superclass and what is a subclass?
A superclass is the class from which a subclass inherits. A subclass inherits from a superclass.
2. How would you write a public inheritance in C++?
class Employee: public Person …
3. In what ways can a subclass extend the functionality of a superclass?
A subclass can take a superclass, say Person, and extend the functionality to an Employee class. So an employee is a person and goes beyond, or extends, the functionality of just the Person class.

1 Like
  1. inheritance is a good way not to double information, also keep certain variable values, properties of objects, without need to redefine them later. it makes our lives (code) easier and more organized further down the road we go
  2. simple example would be “EmployeesOfCompanyXYZ”, which are inherited from the “ResidentsOfCountryZXY” class, which itself is inherited from “PeopleOfTheWorld” class
  3. a superclass is a class above the class using the information within it, a parent class, from which the lower classes can inherit/get/set variable values not in their own class
  4. by generating the class like so: class Subclass : public Superclass { stuff };
  5. by using new variables and appending them to ones in the superclass, or using them to modify results further down the line, but still using the functionality of the original parent class (those that are public or protected)
1 Like

Part One

  1. We need inheritance in C++ to create an “is-a” relationship between two objects. To do this, we simply take the attributes of other objects and extend them, sometimes adding new features. It’s similar to technological products receiving the same features from their earlier versions; however, new features are added to the newest versions.

  2. A child inheriting its genes and physical characteristics from its parents, yet possessing a unique personality

Part Two

  1. A superclass is the class being inherited from; it is also called a parent class or a base class. A subclass is the class that is performing the inheriting; it is also called the child class or derived class.

  2. I would write a public inheritance in C++ as something like this. It’s just my own example.

class animals :
public pets{
...
};
  1. A subclass can extend the functionality of a superclass. Through inheritance, we automatically receive the member functions and member variables of the base class. We therefore do not need to redefine the information repeatedly. Even changes that are made will be inherited.

Thank you!

1 Like

With inheritance we can build upon other similar objects. This helps us reduce amount of code and make the code easier to maintain.

  1. Drink
    a. Coffee
    b. Tea
    c. Water

A superclass is the parent class and the sub class is the child class. The subclass inherits from the superclass.

class Person : public Employee
{
…
}

The subclass extends the superclass by inheriting functionality from the superclass and by creating additional subclass-specific functionality.

1 Like

:thinking: Do you want to inherit animals from pets? I think it would make more sense to inherit pets from animals :slight_smile: