Inheritance in C++ - Reading Assignment

  1. it allows us to extend or attribute different behaviors or characteristics of object to subsequent objects.
  2. Canine
    wolf /////domestic dog
    grey wolf///////pitbull
    ///////husky

1.superclass is the object that is giving the inheritance
subclass is the object receiving the inheritance.
2. class: husky public: Canine
3. a sub class can add new members or methods. it can also extend existing methods.

1 Like

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

We need inheritance because is easy to reuse more bigger classes and get the members of that class and create a new class that inherit all those members and add more specific ones in our new class.

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

Different types of fruit, animals or plants could be modelled using inheritance.

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 superlassName : public childClassName
3. In what ways can a subclass extend the functionality of a superclass?
When a subclass extends a superclass, she gets all the functionalities of the parent class that are public or protected. The subclass redefines the methods of its superclass. using super() it keeps the parent definition

1 Like

Part I:
1. Why do we need inheritance in C++?

  • Inheritance is useful for code reusability. You can reuse attributes and methods of an existing class when you create a new class.

  • Inheritance makes it possible to inherit attributes and methods from one class to another. It 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 modeled in C++ using inheritance.

  • We group the ā€œinheritance conceptā€ into two categories:
  • derived class (child) - the class that inherits from another class
  • base class (parent) - the class being inherited from

House, Techno, Electronica, Disco, Synth-pop, could all inherit from the base class Music as they all share various elements of sound like Melody, Harmony, Rhythm, Texture, Expression, etc…

Part II:
1. What is a superclass and what is a subclass?

  • The class doing the inheriting is called the subclass .
  • The class being inherited from is called the superclass.

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

  • public inheritance makes public members of the base class public in the derived class, and the protected members of the base class remain protected in the derived class.
class Base {
.... ... ....
};

class Derived : public Base {
.... ... ....
};

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

  • When a subclass is formed, it derives all the properties from the superclass. Therefore, the existing code is reused. The subclass inherits all the members of the superclass. The subclass defines its own constructor(s) and can define additional members (data or functions).

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

  • If we ever update or modify the base class by adding new functions or fix bugs, all of our derived classes will automatically inherit the changes.

1 Like

— First Article —

Inheritance involves creating new objects by acquiring the attributes and behaviors of other objects. Also, we find it in real world.

inheritance in real world: Electrodomestics - refrigerator - ice maker - ice cubes.

— Second Article —

the subclass inherits from a superclass or also known as parent class. A subclass inherits methods and variables from the superclass.

class baseballPlayer : public Person { … };
" After the class BaseballPlayer declaration, we use a colon, the word ā€œpublicā€, and the name of the class we wish to inherit."

when a subclass acquires new member functions and variables besides the ones already inherited from the superclass.

1 Like

Part 1

Q: Why do we need inheritance in C++?

A: Because inheritance involves a new object being created by acquiring the properties of another object and extending or specializing them.

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

A: Fruits can be modelled as objects. Apples and bananas are fruits so everything that is true of fruits will be true also for bananas and apples because they are fruits. They have a name, color, shape, size. They inherit the properties from being fruits.

Part 2

Q: What is a superclass and what is a subclass?

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

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

A: class Shrimp : public Seafood {

…

} ;

Q: In what ways can a subclass extend the functionality of a superclass?

A: Because they inherit the subclass functionalities but they are able to include more functionalities and therefore extend the functionalities of the superclass they inherit from.

1 Like
  1. We need inheritance so that we do not have to create new data-types for every new object. This means that one object can have an ā€œis-anā€ relationship with other objects and code can be reused.

  2. An object named Pets could have subclasses of dog, cat, fish, bird.

  3. A superclass is the higher level object that the subclasses inherit data from.

  4. class Pets{
    public: string dogs{}

  5. A superclass can have many subclasses that add progressively more specific details at each level of inheritance. This extends the functionality of the superclass because any alterations to the superclass are inherited by the subclasses.

1 Like
  1. Why do we need inheritance in C++?
  2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.

1: 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. This prevents code duplication.

2: money<-----crypto<-------Ethereum

  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?

1: a Super(or Parent)class is a class that inherits its member functions and variables to the child(or sub)class.

2: like this: class sub: public super{};

3: A subclass can take a superclass, say Money, and extend the functionality to a Crypto class. So a Crypto is a Money class and goes beyond, or extends, the functionality of just the Money class.

1 Like

You only defined a new class without inheriting from any other class. :slight_smile:

Thanks for the reply. I am not sure I am completely understanding the syntax of inheritance and how this is different from a nested object.

PART ONE:
1: Inheritance allows us to give objects properties of another object so you don’t have to do the redundant coding.

2: burger and pizza are inherited from food.

PART TWO:
1: a subclass inherits its properties from the superclass.

2:class pizza : public Food { //code to be used }

3: They get the benefits from public-protected functions or other data types.

1 Like

When you inherit a class you inherit its properties. To publicly inherit a class you use the expression class Subclass: public Baseclass {...}.
What you did was define public members inside a new class. :slight_smile:

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. It allows us to reuse code functionality and it gives us an "is-a" relationship between two objects compared to a "has-a" relationship with new objects.

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

For example, apples and oranges are fruits. They are different fruits, but they both have fruits as a naming convention. Different fruits such as apples and oranges have similar traits; they have a name, colour and size which is "inherited" (acquired) through the properties of fruit because they ARE fruit.

What is a superclass and what is a subclass?

SUPERCLASS:
Superclass is the class being inherited from. It is also known as the parent class or base class.
SUBCLASS:
Subclass is the class doing the inheriting. It is also known as the child class or derived class.

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?

A subclass can use the properties and methods of a superclass through inheritance. It means if the base class ever gets modified or updated, all the subclasses will inherit the changes too.

2 Likes
1. Why do we need inheritance in C++? Inheritance allows you to reuse classes and members. Saves a lot of time, organizes the branches of information from the parent class down to the children classes, and also provides low maintenance when a new functions or variables are added to any classes. 
2. Give a simple example of objects in the real world that could be modeled in C++ using inheritance. Mammal > Animal > dog > breed > legs > color > age

   1.What is a superclass and what is a subclass? A super class is the top of hierarchy also called parent and base class. A subclass is everything under the super class also called child and derived class. 
   2.How would you write a public inheritance in C++? class Person: public Human{members};
   3.In what ways can a subclass extend the functionality of a superclass? By adding new methods or members.
2 Likes

Part 1

  1. To inherit information from the predecessor. Its better to inherit and recycle information than always starting from scratch.

  2. Mean of Transport >> Car ; Ship ; Airplane >> Brand >> Type

Part 2

A superclass (or Baseclass or Parentclass) is the original class; the subclass (or childclass or derived class) is the class derived from the original.

class BaseballPlayer : public Person
(important is the " : public" part )
3)
A subclass inherits all variables + functions from the parent class. If any changes are made to the parentclass, the childclass will adapt the changes right away- therefor you have low maintenance if you have to change something

2 Likes

1. Why do we need inheritance in C++?
For backwards compatibility, and to write less code.

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

I would say cultural rules would be a real world example of inheritance. Also work cultural rules would be a real world example of inheritance because everyone follows them in society or work respectively. There are different cultures in different parts of the world, but everyone believes his or her culture is correct even though it is 100% based on inheritance.

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

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++?
#include

class Person
{
// In this example, we’re making our members public for simplicity
public:
std::string m_name{};
int m_age{};

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

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

};

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.

2 Likes
  1. Why do we need inheritance in C++?
    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. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
    Different types of fruit, animals or plants could be modelled using inheritance.

What is a superclass and what is a subclass?

A superclass, or base class or parent class, is the class we inherit data structure from. A subclass is the class doing the inheriting.

2. How would you write a public inheritance in C++?
class memberSpecificationName : public BaseClassName

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

A subclass can utilize any function or variable from its base class through inheritance and can still add specific functions and member variables for its own definitions.

2 Likes
  1. Inheritance basically saves work and preserves order by allowing member variables to pass from one class to a derived class.

  2. A real world example of inheritance would be just like the example in the reading, Person, Employee, Manager.

**** Second Reading****

  1. A superclass is the Passing class or the one doing the passing, the Subclass is the inheriting class or the class that inherits from the Superclass.

  2. A public inheritance might look like this:

class Superheros: public Marvel
{
public: //Enter Member Variable Here

};

  3.A subclass can utilize any function or variable from its base class through inheritance and can still add specific functions and member variables for its own definitions.
1 Like
  1. Inheritance lets us create new objects by acquiring their attributes and behaviors and extending them.

  2. Dog and Pig are both subclasses of class Animal. The Animal class could contain methods like run(), eat() etc. and attributes such as age, weight, gender etc. which can be inherited by all of its subclasses. We can then extend the subclasses to contain methods and attributes specific to Dog or Pig. For example, Dog could have a bark() method and Pig can have an oink() method.

  3. The class being inherited from can be called the superclass, and the class inheriting is the subclass.

  4. Declare the class, then insert a colon, the word ā€œpublicā€ and the name of the class you wish to inherit from.

  5. We automatically get member functions and member variables of the superclass via inheritance, and then add on any additional functions or member variables.

1 Like
  1. Why do we need inheritance in C++? will give us the origin of an object

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

  3. 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 the subclass .

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

  5. In what ways can a subclass extend the functionality of a superclass? By inheriting all the functionality from the superclass and then defining their own specific (new) functions or override inherited (old) functions.

1 Like

1.Inheritance connect objects through relationship to be extended and specialized , we don’t have to build new object from scratch.
2. FORD T………to ………TESLA

1.Superclass is the inherited parent
2. ….

image

image

3.Subclass is the inheriting processing accumulator

1 Like