-
Inheritance in C++ allows us to easily create new objects by directly acquiring the attributes and behaviours of other objects and then extending/specializing them. Doing so, makes it so that we don’t have to redefine the information/attributes/functions from the superclass (parent class) inside the child class (subclass).
-
A simple example of objects in the real world that could be modelled in C++ using inheritance could be automobiles (they all have the same attributes and behaviours; they are modes of transportation, have doors, a colour, tires, and so on), then we can specialize them by narrowing down to their style (sedan, compact, subcompact, truck, van, etc.), or to the number of doors, size of engine, colour, etc…
-
A superclass is the class from which attributes and behaviours are being inherited (aka parent class).
A subclass is the class doing the inheriting (aka child class) -
To write a public inheritance in C++, declare it using “public:”. such as
class “NameClass” : public “public keyword”{
public:
//balance of class attributes and functions
} -
A subclass can extend the functionality of a superclass by inheriting the functionality of the superclass and bringing in it’s own functions.
-
Why do we need inheritance in C++?
Minimize code duplication -
Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
class BaseballPlayer : public Person
{
…
} -
What is a superclass and what is a subclass?
A superclass is the parent class that have some attribute. A subclass inherit attribute from supper class -
How would you write a public inheritance in C++?
class : public{
…
} -
In what ways can a subclass extend the functionality of a superclass?
subclass can add new methods and new variables
Ans 1:
Inheritance shows the hierarchical structure of how the general description of a known item, drills further down to the specific identification of that item. It also shows how the previous steps down through that hierarchical system, from general to specific, transfers previous characteristics of value. This value is carried/transferred/ retained from that predecessors state, and thus added to the newly arrived and improved specific state.
Ans 2:
General description: "Cars"
Make1: Mazda
Model: 3
Colour: Red
Engine capacity: 4cyl
Engine fuel: Petrol
Transmission: 3spd Automatic
Make2: Mitsubishi
Model: Lancer
Colour: Grey
Engine capacity: 4cyl
Engine fuel: Petrol
Transmission: 5spd Manual
Make3: Toyota
Model: Camry
Colour: Blue
Engine capacity: V6cyl
Engine fuel: Petrol
Transmission: 3spd Automatic
Make4: Chrysler
Model: Dodge
Colour: Black
Engine capacity: 6cyl
Engine fuel: Diesel
Transmission: 6spd Manual
Ans. 1
In the inheritance hierarchical structure, the superclass (baseclass) is defined as the parentclass and the subclasses (derivedclass) are defined as the childclass.
Ans. 2
class Chrysler : public Dodge {
};
Ans. 3
Functionality within the subclass is introduced through the use of programmed member functions and variables.
This subclass also initially inherits the reference point, being the base/parent/superclass keyword name definition, thus extending this functionality to the subclass as an inheritance.
Note:
Therefore, any deliberate changes made to the parent, after first initial developments, will immediately update all the child subclass(es) accordingly.
Answer two would basically be initialized Car objects because they all have the same variables.
A better example of inheritance would be Car : Vehicle and Truck : Vehicle.
What about the rest of the homework?
Part 1:
1. Why do we need inheritance in C++?
Inheritance involves creating new objects by directly acquiring the attributes and behaviours of other objects and then extending or specializing them, thus reducing the amount of code and making the program more efficient.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Vehicle > Car > Audi > A5 > Coupe
Part 2:
1. What is a superclass and what is a subclass?
The superclass(parent class) is the class that is being inherited from, and the subclass(child class) is the class that inherits from the superclass.
2. How would you write a public inheritance in C++?
class SubClassName : public SuperClassName
{
...
};
3. In what ways can a subclass extend the functionality of a superclass?
The subclass inherits all the member functions and member variables from the superclass and from there can define new functions or variables to extend the functionality.
-
We need inheritance in C++ to build “is-a” relationships between objects while minimizing redundancies in derived classes.
-
A family tree could be modelled using inheritance in C++.
-
A superclass is the class being inherited from (parent class), while the subclass (child class) is the class that inherits from the superclass.
-
To write a public inheritance in C++ first declare then the superclass, separated by a colon.
ie.
class Class : public Superclass -
A subclass can extend the functionality of a superclass by allowing different variables/functions to be added and/or modified without having to redefine the information from our base class.
Part 1
1. Why do we need inheritance in C++?
= When we create classes that inherits from a base class, we save time on adding the general information that would be the base of both classes.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
= In a game you may have different items for the characters.
So you could have Clothes as the base class, then Dress, Pants and Shirt that inherited the basic functions of clothes.
Part 2
1. What is a superclass and what is a subclass?
= Superclass is the parent or base class that are being inherited from.
subclass is the child or derived class that are doing the inheritance.
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?
= They can inherit the basic features from the superclass, but we can then add new member functions and members and get more defined and detailed and more functionality down the line.
-
Because it gives us another dimension and extra possibilites for constructing classes.
-
Vehicle -> Engine / NoEngine (the first is the parent, the latter are the children)
-
superclass is the grand-parent of the subclass.
class Engine {
public:
...
}
3. A subclass can inherit specific information from the superclass, from which it could get new information.
Why would the Engine inherit from a Vehicle? The Engine might be part of a Vehicle, therefore would be defined inside the Vehicle class. A Car could inherit from a Vehicle.
Here you only defined a new class but didn’t inherit from any other class. A public inheritance would look like this:
class Car : public Vehicle {
public:
Engine e;
}
-
We want to duplicate so little code as possible we want to reduce as much code as possible!
When creating a new class instead of writing completely new data members and members functions, the programmer can designate that the new class should inherit the members of existing class! -
As we are humans we inherit properties from the class “Human” such as ability to speak, breathe, eat, drink.
Superclass & subclass!
- What is a superclass and what is a subclass?
The sublass is the class that inherits from the superclass.
- How would you write a public inheritance in C++?
class Ivanontech : public forumivanontech {
//CODE…
};
- In what ways can a subclass extend the functionality of a superclass?
A subclass can define new functions or overrwrite intheriting functionality from superclass to extend functionality.
Does Ivanontech inherit from forumivanontech?
Part 1
1. Why do we need inheritance in C++?
Throw inheritance we can construct complex classes, which models an “in-a” relationship between 2 objects.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
The cell phone inherited many features from the old land phone
Part 2
1. What is a superclass and what is a subclass?
The class being inherited from is superclass, and the class doing the inheriting is called subclass.
2. How would you write a public inheritance in C++?
After the class declaration we use a colon, the word public, and then the name of the class we wish to inherit.
3. In what ways can a subclass extend the functionality of a superclass?
Subclass can inherit member functions and variables from superclass, then it can extends by creating derived classes.
1. Why do we need inheritance in C++?
As a means of grouping the same type of classes whose variables and functions may overlap but may have varying behaviors. One of the key reasons we need inheritance is to prevent code repetition as we define a base class (super class) which defines properties and behaviors in which its child classes can call upon as opposed to re-defining them.
We can use the base class as a template, by inheriting its features and functions to then override behaviors or simply define extra behaviors or features. This means we can avoid re-defining the already inherited features and functions from the inherited class.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
We can have a Shape
class and define different shapes as classes such as Circle
, Rectangle
, Triangle
etc… to inherit properties of Shape
.
1. What is a superclass and what is a subclass?
A super class is the base/parent class, whereas a subclass is a child class of the base class.
2. How would you write a public inheritance in C++?
Example:
class Circle : public Shape {
}
3. In what ways can a subclass extend the functionality of a superclass?
The subclass inherits properties and functions of the superclass. The subclass can then define further new properties and functions on top.
Part 1
- We need inheritance in C++ for backwards compatibility.
- Some examples of objects in the real would that could be modeled in C++ using inheritance might be plants of all species as well as animals of all species inheriting the genes from their predecessors and aimed at their optimal survival chances.
plant > tree > palm
animal > feline > lion
Part 2
- A superclass is the class that is providing the traits for the subclass that inherits them.
- To write a public inheritance in C++ you declare the class within the “public” scope.
- The subclass can extend the functionality of a superclass by automatically receiving the member functions and member variables through inheritance, and then adding the additional functions or member variables needed.
[quote=“ivan, post:1, topic:3172”]
1. Why do we need inheritance in C++?
To allow us to create objects that directly inherits its properties of another objects. It allows us to build “is-a” relationships between pbjects
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Fruits:
-Apple
-Orange
-Banana
1. What is a superclass and what is a subclass?
A superclass is like a parent and a subclass is a child which is being inherited by the parent.
2. How would you write a public inheritance in C++?
class Fruit: public Fruit{
…
};
3. 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.
[/quote
Is this a typo? A class can’t really inherit itself
- inheritance allows us to create new objects by directly acquiring the attributes and behaviours of other objects and extending and specializing them. It allows for more complex code.
- example : to show progression from general to specific i.e. food, fruit, apple, gala
1 superclass also known as parent or base is the class that is inherited from, subclass also known as child or derived class inherits functions and variables from the superclass.
2.
class Supervisor: public Employee
{
public:
// This Supervisor can oversee a max of 5 employees
long m_overseesIDs[5]{};
- subclass extend the functionality of a superclass by deriving functions and variables into their class
};
PART I
1.) Inheritance involves creating new objects by directly acquiring the attributes and behaviors of other objects and then extending or specializing them. This makes it easier to create new objects because with inheritance we don’t need to create an entirely new set of attributes, instead we inherit them from previous objects.
2.) World of Warcraft playable characters > Horde / Alliance > character races
PART II
1.) Superclasses are the parent class, from which a subclass (or child class) inherits behaviors and properties.
2.) class BaseballPlayer : public Person
3.) When a subclass extends a superclass, it inherits all the behaviors and functions of the superclass. It can also override existing methods.
- Inheritance is useful in C++ because it saves us work when creating a child class.
- One example of inheritance could be used when people sign up for a forum or website; everyone who joins would be defined under a ‘base member’ class, and from those we could define various child classes like: moderator, or admin.
- A superclass refers to the parent class and subclass refers to a child of that parent class.
- class childClass : public parentClass {};
- A subclass inherits all the variables and functions of it’s parent but can also define it’s own class member variables.