- C++ Game Development By Example
- Siddharth Shekar
- 297字
- 2021-06-24 14:26:13
Inheritance
One of the key features of C++ is inheritance, with which we can create classes that are derived from other classes so that the derived or the child class automatically includes some of its parent's member variables and functions.
For example, we looked at the shape class. From this, we can have a separate class called circle and another class called triangle, which has same properties like area as other shapes, such as area.
The syntax for an inherited class is as follows:
class inheritedClassName: accessSpecifier parenClassName{ };
accessSpecifier could be public, private, or protected depending upon the minimum access level you want to provide to the parent member variables and functions.
Let's look at an example of inheritance. Consider the same shape class, which will be the parent class:
class shape { protected: float a, b; public: void setValues(float _length, float _width) { a = _length; b = _width; std::cout << "length is: " << a << " width is: " << b <<
std::endl; } void area() { std::cout << "Area is: " << a * b << std::endl; } };
Since we want the triangle class to access a and b of the parent class, we have to set the access specifier to protected, as shown previously, otherwise, it will be set to private by default. In addition to this, we also change the data type to floats for more precision. After doing this, we create a setValues function instead of the constructor to set the values for a and b. We then create a child class of shape and call it triangle:
class triangle : public shape { public: void area() { std::cout << "Area of a Triangle is: " << 0.5f * a * b <<
std::endl; } };
Due to inheritance from the shape class, we don't have to add a and b member variables, and we don't need to add the setValues member function either, as this is inherited from the shape class. We just add a new function called area, which calculates the area of a triangle.
In the main function, we create an object of the triangle class, set the values, and print the area, shown as follows:
int main() { shape rectangle; rectangle.setValues(8.0f, 12.0f); rectangle.area(); triangle tri; tri.setValues(3.0f, 23.0f); tri.area(); _getch(); return 0; }
Here is the output of this:

To calculate the area of circle, we modify the shape class and add a new overloaded setValues function, as follows:
#include <iostream> #include <conio.h> class shape { protected: float a, b; public: void setValues(float _length, float _width) { a = _length; b = _width; std::cout << "length is: " << a << " height is: " << b <<
std::endl; } void setValues(float _a)
{
a = _a;
} void area() { std::cout << "Area is: " << a * b << std::endl; } };
We will then add a new inherited class, called circle:
class circle : public shape { public: void area() { std::cout << "Area of a Circle is: " << 3.14f * a * a << std::endl; } };
In the main function, we create a new circle object, set the radius, and print the area:
int main() { shape rectangle; rectangle.setValues(8.0f, 12.0f); rectangle.area(); triangle tri; tri.setValues(3.0f, 23.0f); tri.area(); circle c; c.setValues(5.0f); c.area(); _getch(); return 0; }
Here is the output of this:

- 筆記本電腦使用、維護(hù)與故障排除實戰(zhàn)
- 計算機(jī)組裝與系統(tǒng)配置
- 嵌入式技術(shù)基礎(chǔ)與實踐(第5版)
- 電腦常見故障現(xiàn)場處理
- Unity 5.x Game Development Blueprints
- Mastering Manga Studio 5
- Learning Stencyl 3.x Game Development Beginner's Guide
- 分布式微服務(wù)架構(gòu):原理與實戰(zhàn)
- CC2530單片機(jī)技術(shù)與應(yīng)用
- 基于Apache Kylin構(gòu)建大數(shù)據(jù)分析平臺
- Rapid BeagleBoard Prototyping with MATLAB and Simulink
- 固態(tài)存儲:原理、架構(gòu)與數(shù)據(jù)安全
- OpenGL Game Development By Example
- 新編電腦組裝與硬件維修從入門到精通
- Zabbix 4 Network Monitoring