You inherited a Person from the Baseballplayer
Article 1:
- We need inheritance in C++ because inheritance allows us to construct complex classes by “is-a” relationships between two objects.
- Ford F-150 & Toyota Tundra are both trucks.
Article 2: - A superclass is the class which characteristics are inherited from and the subclass is the class that inherits characteristics/behaviors from the superclass.
- Class MLB Team: public Detroit Tigers{…};
- Subclasses can extend the functionality of a superclass by the subclass inheriting the members of the superclass. If anything is updated/changed in the superclass, the subclasses are updated as well.
- Why do we need inheritance in C++?
We need inheritance so that we can reduce duplication/clutter of code.
- Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Cats, dogs and birds from ‘animal’
1. 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 .
2. How would you write a public inheritance in C++?
class Bird : public Animal
3. In what ways can a subclass extend the functionality of a superclass?
Because the subclass defines its own member variables and functions.
Why do we need inheritance in C++?
Inheritace allow us to add previous attributes from other classes without having the need to create them from scratch, to a new class
Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
We could give as an example an hardware store. The screw section may have a lot of screws, and even if they are all different they have common atributes such as color, material, usage (wood, iron, plastic, …)
What is a superclass and what is a subclass?
Superclass is also called parent class (the class that is above and from we inherit properties)
Subclass or child class are the ones that are below the parent class, and share the same properties of the parent + properties of their own.
How would you write a public inheritance in C++?
When we are defining a new class we define the public class after the name, so as an example:
class NovaClasse : public Alunos{ }
In what ways can a subclass extend the functionality of a superclass?
As referred before the subclass has all the functionalities of the superclass, plus attributes and functions of its own.
Part 1
- Inheritance give us the ability to use the object in the parent class.
- Ethereum and other alt coins inherits from Bitcoin.
Part 2 - A superclass is the parent class where the child class or the subclass inherits its properties.
- class Bitcoin {
public:
…
}; - By adding a new methods and variable in it.
- Inheritances allows you to create new objects by acquiring attributes and behaviors of other objects, as well as extend or specialize them.
2 . A simple example of objects in the real world:
DeFi
Eth
Maker
Curve
-
A superclass is the class that is being inherited from, a subclass is the class that is doing the inheriting.
-
Public inheritance is written:
class SomeClass: public someinheritance -
A subclass and extend the functionality of a superclass because it allows you to create ne variable and methods as well as overwriting existing one.
Part 1
-
Allows you to build new objects that have a “has a” relationship with other similar parts.
-
Beverage
Drink
Water
Part 2
-
The parent is the superclass, and the child is the subclass.
-
baseClass (orangeJuice) : parentClass (beverage)
-
A subclass inherits the functions from the superclass, and is able to add further functions and variables to the subclass. An added benefit is that if you update, or modify the subclass, all derived functions will automatically inherit the changes.
// First Answers
1- Inheritance models an “is-a” relationship between two objects. It derives properties and characteristics from another class. It allows us to reuse the code functionality, and provides a faster implementation.
2-
- Vehicle
1.1 Car
1.2 Truck
1.3 Bus
The methods fuelConsumption(), modelYear(), weight() will be same for all of the three classes.
// Second Answers
1- The class being inherited from is called the superclass, and the class doing the inheriting is called the subclass .
2-
// Base class
class Vehicle {
};
// Derived class
class Car: public Vehicle {
};
3- The subclass can define new functions or variables to extend the functionality.
Part 1.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. This also provides an opportunity to reuse the code functionality and fast implementation time.
- Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Example:
Fruits (is the parent class) Bananas and strawberries are the children!
Part 2.
-
What is a superclass and what is a subclass?
Sub Class: The class that inherits properties from another class is called Sub class or Derived Class. Super Class: The class whose properties are inherited by sub class is called Base Class or Super class. -
How would you write a public inheritance in C++?
Class person
{
Public:
} -
In what ways can a subclass extend the functionality of a superclass?
By including additional methods and variables.
Part 1
- Why do we need inheritance in C++?
A class is an important data type in C++ that makes it possible to create objects, that contain specific members, from a programmer-defined blueprint. Some class members are unique to a particular class but there are, most likely, variables and functions that are common and relevant to many classes within a large and complex program. Instead of writing the same code to define these common members in each class, inheritance offers the possibility to create a parent class that holds these common members and all relevant child classes can, through inheritance, acquire access to these members. This reduces complexity and redundancy of code and makes implementing changes or updates of these common members simpler because these changes are done in the parent class but child classes inherit these changes as well.
Additionally, by using inheritance chains, it is possible to create a set of classes where general classes contain members that are rather generic, and more specific classes at the end of the chain contain the inherited general member variables and functions as well as members that are more specific and relevant only for a specific class.
- Give a simple example of objects in the real world that could be modelled in C++ using inheritance?
Animals >> Mammals, non-mammalians >> …
Animals is the parent class that contains members that represent characteristics or behavior that are common to both child classes, mammals and non-mammalians. The respective child class contains members that are only relevant to the particular child class. The child classes can then be split up even further, e.g. mammals can be divided into human and non-human etc.
Part 2
- What is a superclass and what is a subclass?
A superclass (parent class) is the class from which the subclass (child class) inherits members.
- How would you write a public inheritance in C++?
class ParentClass
{
public:
int m_a;
void print()
{
std::cout << m_a << endl;
}
};
class ChildClass : public ParentClass // -> ChildClass inherits members from ParentClass
{
public:
int m_b;
void print2()
{
std::cout << "Number " << m_a << " and number " << m_b << endl;
}
};
- In what ways can a subclass extend the functionality of a superclass?
The subclass can have its own variables and functions that together with the inherited members from the superclass can extend the overall functionality because objects created from the subclass have more members to potentially interact with.
1/1. So we don’t have to define everything from the very basics.
1/2. Machines -> Vehicles -> Cars -> Volvos
2/1. Superclass is the class that is being inherited from. Subclass is the one that inherits things.
2/2. class SubClass : public ParentClass {…}
2/3. It can have more and more specific member variables and functions. This method basically help us not to be forced to repeat code.
PART 1
-
Why do we need inheritance in C++?
Utilising inheritance in C++ means that we don’t have to redefine information from base classes and can make changes to base classes that add functionality/update instructions to child classes throughout the program.
If used properly, there should be consistency going up the hierarchy, and changing something from a parent should not have an incorrect impact to child classes.
Inheritance saves the programmer time and code having to reinstate information, which can become confusing. Updates are also seamless when inheritance is used correctly. -
Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Corporate structures are a simple way to describe some hierarchy that would benefit from inheritance.
Starting from a parent class which can be as general as need be, such as species, to awfully specific managerial positions details, you could model the additional members as you go down the child classes.
PART 2
-
What is a superclass and what is a subclass?
A superclass is the parent class in a relationship between two classes with more general member/s. The superclass is higher up the hierarchy.
The subclass is the child class in a relationship between these two with more specific member/s. The subclass is further down the hierarchy.
So in this case, the subclass will inherit member/s from the superclass. -
How would you write a public inheritance in C++?
class Fish: public Animal {
public:
type: member
type: member
};
-
In what ways can a subclass extend the functionality of a superclass?
A subclass can extend the functionality of a superclass by copying all of the existing functionality of the superclass, and then adding more functionality into itself that is more unique to the subclass.
This can be by defining their own newer functions, or overriding superclass functions, only for itself (and its child classes).
-
It helps by allowing one to minimize the duplication of code and instead allowing the same properties of objects to be applied to others.
-
“Mushrooms” and “molds” can inherit from the class “Fungus”.
-
The subclass is derived from the superclass.
-
After the class declaration, a colon is used, followed by the word “public” and the class name to inherit.
-
Not only can they inherit the capabilities of a superclass, but they can add new methods or variables and add yet another level of complexity.
Why do we need inheritance in C++?
Inheritance is needed in C++, so we dont have to rewrite everything
Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Fruits, apple and banana were used as examples.
What is a superclass and what is a subclass?
superclass,also known as parent, is the class which gives its member functions and values to the subclass.
Superclass is, and subclass is based on superclass
How would you write a public inheritance in C++?
with : public Class before {
In what ways can a subclass extend the functionality of a superclass?
subclass can add it’s own member functions and values.
-
with inheritance it is possible to create hierarchy, which has some advantages, for example: save time (we don’t have to re-establish variables for objects who can inherit those variables from other objects). Fewer coding, less failures.
-
example: buildings -> theatres -> London Globe Theatre
second part:
-
superclass: the highest (and most abstract) class who has logically only childs. Also called parent class
-
class Theatre : public Building {
…
}; -
while a superclass has more abstract values (e.g. in my example it could be capacity, size, number of windows…), a subclass can have more specified values that are useful only for this subclass. For example: the subclass “Theatre” can have variables such as number of seats, director name, current shows,…)
- Why do we need inheritance in C++?
Inheritance allows us to create new objects without redefining the attributes and behaviors of previous objects. This saves us time as developers.
- Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Lions and tigers inherit from mammals; mammals inherit from the animals kingdom; etc.
- What is a superclass and what is a subclass?
A superclass (aka parent class) is the class being inherited from. The subclass (aka child class) is the class inheriting from the superclass/parent class.
- How would you write a public inheritance in C++?
Class Lion: public Animal {
public:
string name
string colour
etc.
}
- In what ways can a subclass extend the functionality of a superclass?
A subclass extends the functionality of a superclass by getting all of the latter’s functionalities without the programmer having to redefine them all from scratch.
- we can construct complex classes
2.wrestling and jui jitsu, jui jitsu inherited alot of technique and rules from wrestling but had added features to make a new exiting sport
1.the class that is being inherited from
-
class sdsdsd : public hwhwwh{};
-
When BaseballPlayer inherits from Person, BaseballPlayer acquires the member functions and variables from Person.
You just defined a new class without inheriting from anything,
Oops! That slipped past me, but I just fixed it. Thank you for bringing that to my attention!
R1
-
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. -
Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
Animal > Canine > Wolf
R2
-
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. -
How would you write a public inheritance in C++?
class Canine : public Animal
{
public: -
In what ways can a subclass extend the functionality of a superclass?
The subclass can add new members and variables. They can also override existing methods.