
 |
![]() |
|
|
|
|
 |
Inheritance
See Also: What is Inheritance?
Creating a Derived Class
Creating an Object
Simple Inheritance
Overloading Derived Classes
Dominating Member Functions
Polymorphism & Virtual Functions
Hello everyone, welcome to another exciting chapter in C++. As you can see by the title, this chapter will cover the topic inheritance. Inheritance in C++ is similar to Inheritance in Biology. For instance, some people think that reptiles evolved from the dinosaurs. The reptiles inherited the dinosaur’s scaly skin and tail. This type of association can also be used in programming, for instance, say you have a class named Reptile, and in this class you had traits such as member functions and variables. The base class would look like this:
 | |  | | |
//tq.h
//Example of simple inheritance
//base class
class Reptile
{
public:
Reptile(): itsHeight(100),itsWeight(2000){};
void setHeight(int height) { itsHeight = height;}
int getHeight() {return itsHeight;}
void SetWeight(int weight) { itsWeight = weight;}
int getWeight() { return itsWeight;}
protected:
int itsHeight;
int itsWeight;
};
|
|  |
|
This looks like a regular class, except for the new keyword protected. Protected is used when you are using inheritance. The reason we don not use private when we use inheritance is because private is not available to derived or supersets of the base class. If you create a derived class from Reptile and named it Lizard, the Lizard class would have all the member variables and functions that the base class has, even the constructor would affect the derived class.Please Rate this Code:
Comments for: Inheritance
There are NO user contributed comments for Inheritance.
|
 |
|
 |
|
|
|
|
|
|
|