1. Why do we need inheritance in C++?
So that we do not have to copy and paste code from parent classes into child classes.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
TObject > TPersistent > TComponent
If a glass is a container and a wine glass is a kind of glass then…
TContainer > TGlass > TWineGlass
class TContainer
{
// members
}
class TGlass : public TContainer
{
// members
}
class TWineGlass : public TGlass
{
// members
}
1. What is a superclass and what is a subclass?
In the above example TContainer is the superclass above TGlass. Parent and child relationship exists where the class above knows of the properties of the parent class.
2. How would you write a public inheritance in C++?
See above.
3. In what ways can a subclass extend the functionality of a superclass?
Many. With virtual or abstract methods declared in parent classes you can override declarations in parent classes that provide the logical framework for sub classes
- We can take attributes from other classes and specialize them while still maintaining the original classes.
- Children inherit genetic information from their parents and then specialize with their own personality.
- A superclass is the original class that a subclass derives from.
- class subclassName : public superclassName{};
- You can take all the old variables and functions to apply to the new class.
- inherits properties of other objects
- minimize code duplication
- improve code structure
- CAR: Audi, VW, Volvo etc.
- a subclass inherit from a superclass
- class bar {}
class foo : bublic bar {} - add further methods and attributes
Inheritance allows creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them.
A CocaCola class and a DrPepper class can inherit stuff from a Soda class
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 .
class NameOfClass : public NameOfClassToInheritFrom
It can add more methods and variables
Part 1:
-
Why do we need inheritance in C++?
Inheritance is a good way of 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 modeled in C++ using inheritance.
A good example would be a family tree since people get their genes from their parents but then they add their own personality.
Part 2:
-
What is a superclass and what is a subclass?
A superclass is the base class (origin) and a subclass is the derived class. -
How would you write a public inheritance in C++?
class DerivedClass : public BaseClass {}; -
In what ways can a subclass extend the functionality of a superclass?
Bith variables and member functions extend from the superclass to the subclass.
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. Which models an “is-a” relationship between two objects.
Give a simple example of objects in the real world that could be modelled in C++ using inheritance. 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).
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.
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.
In what ways can a subclass extend the functionality of a superclass? A subclass inherits both behaviors (member functions) and properties (member variables) from the superclass. Once inherited, the subclass becomes a superclass when a subclass inherits previous functions and variables.
-
Why do we need inheritance in C++?
It allow us to create object that are directly connected to other objects and have automatically have the same attributes and behaviors. But the new objects can be specialized or extended. -
Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Marlboro and Lucky Strike inherit form cigarette. Cigarettes are unhealthy- so are Marlboro and Lucky Strike.
----------------------------------basic-inheritance-in-c--------------------
-
What is a superclass and what is a subclass?
Superclass = base class = parent class -> the class being inherited.
Subclass = derived class = child class -> the class doing the inheriting. -
How would you write a public inheritance in C++?
class Marlboro: public Cigarette
{
…
} -
In what ways can a subclass extend the functionality of a superclass?
The subclass could have new methods, be more detailed…
Part 1
1) Why do we need inheritance in C++?
We need inheritance to retain desirable, useful features from previous versions, for backward compatibility and and to avoid duplicating code.
2) Give an example of objects in the real world that could be modeled in C++ using inheritance.
- Shop tools
1.1 Saws
1.1.1 Band saw
1.1.2 Table saw
1.1.3 Hand saw
1.2 Drills
1.2.1. Hand drill
1.2.2 Electric drill…
Part 2
1) What is a superclass and what is a subclass?
Superclass = Base class = Parent class = the class that is inherited from.
Subclass = Derived class = Child class = the class doing the inheriting.
2) How would you write a public inheritance in C++?
class HouseCat : public MostlyDomesticatedAnimal
3) In what ways can a subclass extend the functionality of a superclass?
A subclass extends the functionality of a superclass by inheriting the member functions and member variables of of the superclass, and also by being able to have their own member functions and variables (specific to their subclass).
1 - It allows for an additional type of relationship between data, the ‘is-a’ relationship.
2 it can be almost everything who has difference types like cars, houses, land end etc.
3 A superclass is a perent class on top of the chain and sbuclass go after like a child class.
4 class : public {
…
}
5 - They are able to inheret the superclasses capabilities and yet include more methods and variables in order to go beyond what went before.
1. Why do we need inheritance in C++?
Object composition is perfect for building new objects that have a “has-a” relationship with their parts. However, object composition is just one of the two major ways that C++ lets you construct complex classes. The second way is through inheritance, which models an “is-a” relationship between two objects.
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.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
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++?
class Class2 : public Class1
{
};
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.
-
Why do we need inheritance in C++? It helps to organise complex linked object structures in a meaningful way.
-
Give a simple example of objects in the real world that could be modelled in C++ using inheritance…a Car has Colour, and Engine. Color can be Metalic, Gloss, Matt. The Engine could be Electric, Petrol, Diesel…
1b. What is a superclass and what is a subclass? A superclass is the parent class, from which the subclass or child class inherits the members from.
2b. How would you write a public inheritance in C++?
class newMyChildClass: public existingParentClass
{....
3b. In what ways can a subclass extend the functionality of a superclass? It offers relative, and more extensive options and diversity to the parent class.
Part 1
- For a class to be able to inherit properties and behaviors from another class.
- Animal <-- Mammal <-- Dog / Cat
Part 2
- The class being inherited from is called a superclass, the class doing the inheriting is called the subclass.
- class SubClass : public SuperClass
- A subclass can have its own member variables and member functions.
PART1:
- Why do we need inheritance in C++?
- We need inheritance because it allows us to create new objects by directly acquiring the attributes and behaviours 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.
- When babies are born, they inherit their parents’ genes and physical attributes, but as the babies get older, they are able to add their own personality on top of it all
PART2:
- What is a superclass and what is a subclass?
- The class being inherited from is the superclass, and the subclass is the class doing the inheriting
- How would you write a public inheritance in C++?
- class BaseballPlayer : public Person(){…};
- In what ways can a subclass extend the functionality of a superclass?
- A subclass can extend the functionality of a superclass by overriding existing methods and by adding new members and methods
Why do we need inheritance in C++?
It allows us to build on top of a previous version a new but slightly different version of class by incorporating the older versions members and methods.
Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
black and white television -->> colour television -->> Hi Def television
What is a superclass and what is a subclass?
A superclass is the class being inherited from while the subclass is the class doing the inheriting
How would you write a public inheritance in C++?
class Name_Of_New_Class : public Name_Of_Class_To_Inherit_From
In what ways can a subclass extend the functionality of a superclass?
It can extend it by adding both new members and new methods to the underlying superclass
Answers
Introduction to inheritance
-
Inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending them. In this way, two objects are bounded by an “is-a” relationship.
-
For example, computers and phones inherit from hardware.
Basic of inheritance
-
Superclass is the parent class (eg. hardware), while subclass is the child or derived class (eg. PCs and phones).
-
Class PCs : public Hardware {
… members… } -
Subclass can add new methods or variables and override existing methods.
-
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
-
- Vehicle
1.1 Car
1.1.1 Volvo
1.1.2 Audi
1.2 Motorcycle
1.2.1 Suzuki
1.2.2. Yamaha
- Vehicle
-
A superclass is a parent class, is being inherited by a child class
A subclass is a child class inheriting a parent class -
class Cat : public Animal
{
...
};
-
Add new members
Add new methods
Override existing methods
-
Inheritance allows us to create new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them.
-
Car and motorbike inherit from vehicle.
-
Superclass: The class being inherited from (also called the parent class).
Subclass: The class doing the inheriting (also called the child class). -
Class Car : Public Vehicle
-
A subclass inherits both behaviors (member functions) and properties (member variables) from the superclass. They can also define their own new functions, or override old inherited functions.
-
Because inheritance allows us to create new objects by directly acquiring the attributes and behaviors of other objects and then extending and specializing them. Inheritance models an “is-a” relationship between two objects, unlike object composition which is great for building new objects that have a “has-a” relationship. In a nutshell, it allows us to create a new class that inherits the behaviors (member functions) and properties (member variables) or its parent class.
-
Person > Employee > IT Professional > Developer > Full-stack developer > Blockchain full-stack developer
- A superclass (or parent class) is the class being inherited from by a subclass (or child class), which is doing the inheriting.
class MyChildClass : public MyParentClass
{
...
};
- By specializing its attributes and behaviors. For instance, an
Employee
child class would pick up the attributes and behaviors of aPerson
parent class (such as “gender” or “age”) and add its own specialized ones (such as “salary”, “worker ID”, which are not members of thePerson
class).
I have a question @ivga80 : In parent and child classes, are “attributes” and “properties” the same? Are they member variables?