This is the Super class name, not a function
1. Why do we need inheritance in C++?
So we can create 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.
pets-dogs
1. What is a superclass and what is a subclass?
super class is the parent and subclass is the child. The child/subclass inherits from parent/supeerclass
2. How would you write a public inheritance in C++?
class manager: public Employee
{
public:
// This manager can oversee a max of 5 employees
long m_overseesIDs[5];
Manager()
{
}
};
3. In what ways can a subclass extend the functionality of a superclass?
Can add more methods and variables in addition to what it already has
Thanks for the correction. Your input is valuable.
1. Why do we need inheritance in C++?
It allows us to copy the structure (data types and methods) of a class and re-use them in other classes. This saves a lot of time and creates a hierarchical link between classes.
2. Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
You could think of machines as a class, then planes, and cars as sub classes, and models can be another subclass in C++.
1. What is a superclass and what is a subclass?
A superclass is the parent class that is inherited by another class. The subclass is any class that inherits from another class.
2. How would you write a public inheritance in C++?
You use the class keyword, semicolon, “public” keyword, then the name of the class being inherited. For example, class Car : public Machine { }
3. In what ways can a subclass extend the functionality of a superclass?
A subclass inherits all the variables and methods of the superclasses and it can add its own individual variables and methods that extends the functionality.
- Inheritance allows us to create objects that inherit properties of other objects.
- Pencil and chairs, for example, are inherit from wood
- A superclass or partent class is the one which attributes are inherited, and the subclass is the child class that inherits attributes
- class ChildClass : Public ParentClass
- The subclass can extend the implementation of a superclass with additional fields and functions, as well as overwriting values
1.To minimize duplication of code
2. Food:
-fruit(apple ,banana,orange)
-vegetable(carrot,cucumber,broccoli)
-meat(pork,beef,lamb)
1.Superclass is the one that is being inherited and the sublcass is the one that inherits.
2. class superlassName : public childClassName
3.Subclass extends the functionality of a superclass with the addition of new member functions and member variables on top of the ones already inherited from the superclass.
Uff, you explained the inheritance so well but then switched the way its defined Can you fix it?
PART I
1. Why do we need inheritance in C++?
It allows us to save time and space, this is because we don’t need to rewrite code that is inherited from a predecessor.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Hot drinks, Soft drinks and alcoholic beverages are all drinks. And each one of them can have more specific instances.
PART II
1. What is a superclass and what is a subclass?
Superclass or parent class is the predecessor, whilst a subclass or a child class is the one which will be inheriting properties from its parent class.
2. How would you write a public inheritance in C++?
class Whiskey: public AlchoholicBeverage{
//class properties and methods here
};
3. In what ways can a subclass extend the functionality of a superclass?
A subclass inherits all properties and methods from its superclass, making them its own, plus a subclass can also have its own properties and methods.
ouh It’s vice versa
class childClassName : public superlassName
thanks for correcting me
- We can that way create objects that inherit properties of another objects. No need to write everything from the scratch
- Student and teacher inherits from person
- You inherit properties from a superclass to subclass
- class Person : public Student {
//add members
} - Add methods/variables, override existing methods
You switched the definition. This would make a Person class inherit class Student
-
Why do we need inheritance in C++?
Inheritance provides a more efficient and streamline way to manage properties of objects within classes. The derived class can use properties of the Base class saving the trouble of entering code twice. It is also very efficient in the event a function is added or a bug fixed in the base class, the changes automatically update the functions of all inherited properties in the sub classes. -
Give a simple example of objects in the real world that could be modeled in C++ using inheritance.
A simple example would be having a base class of Trucks.
class Trucks
public:
m_drivetrain
m_engineLitres
m_fuelType
m_cab
class Make: public Trucks
m_model
m-color
m_bedsize
In this example the derived class Make could inherit the attributes from base class Trucks of standard categories drivetrain, engine size, fuel type and cab style. The derived class make could then create more specific info categories.
-
What is a superclass and what is a subclass?
Super class is also known as base class or parent class. The sub class is also known as the derived class or the child class. Just as it may sound the sub class inherits properties and functionality from the super class. -
How would you write a public inheritance in C++?
class Make : public Trucks
{
members and functions
}; -
In what ways can a subclass extend the functionality of a superclass?
The derived class extends the functionality of of the base class by creating more functions and narrowing the scope of it’s members. We are able to expand the behavior of the base class for more specific purposes.
I would argue this is not really the best example. You could put the color and engine site and fuel type in a class Vehicle and derive a class Truck from it
- Why do we need inheritance in C++? Inheritance allows another method of object composition in C++ that lets you construct complex classes in a “is-a” (relationship between two objects) model.
- Give a simple example of objects in the real world that could be modeled in C++ using inheritance. Hierachies that demonstrate progression over time, or those classifications that go from general to specific, such as the general category of fruit and specific objects such as an orange or apple.
- 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.
- How would you write a public inheritance in C++?
#include
class Employee ; public Person
{
// In this example, we’re making our members public for simplicity
public:
std::string m_name;
int m_age;
Person(std::string name = “”, int age = 0)
: m_name(name), m_age(age)
{
}
std::string getName() const { return m_name; }
int getAge() const { return m_age; }
};
- In what ways can a subclass extend the functionality of a superclass? A subclass class inherits both behaviors (member functions) and properties (member variables) from the parent. Because subclasses are full-fledged classes, they can have their own members that are specific to that class. i.e. Superclass = Flora , One of it’s subclasses could be Tree, and a subclass to trees could be Pine . so then the category Tree gives inheritance to pine, and becomes the parent to pine, even though Tree is also the child of Flora.
Here you just defined a class Person that didn’t inherit from any other class. Can you fix it?
1.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.
So things (objects) that already exists do not have to be created again, you inherit them from an other object.
2. Give a simple example of objects in the real world that could be modelled in C++ using inheritance.
Country >> State >> City >> Street >> Address >> HouseNumber >> Resident
+++++++++++
1. 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 .
2. How would you write a public inheritance in C++?
// BaseballPlayer publicly inheriting Person
class BaseballPlayer : public Person
3. In what ways can a subclass extend the functionality of a superclass?
By creating more functions and other kind of functionalities, we can add the additional functions or member variables we want.
class Employee; public Person
(Would that be correct?)
That is correct!
- It allows us to construct databases with relations between objects.
- Aircraft:
- Fix wing: jet plane, glider.
- Rotor wing: Helicopter, Gyroplane.
- Class being inherited from is the Superclass, and the class doing the inheriting is subclass.
- class YourClassName : public YourSuperClassName {…};
- Subclass inherits both behaviors (member functions) and properties (member variables) from the superclass and subclass can have their own behaviors and properties.