Reusing Code in C++: inheriting and template

来源:互联网 发布:mac os sierra u盘 编辑:程序博客网 时间:2024/05/16 04:12
Reusing Code in C++: inheriting and template
The worldview of Object-oriented means a lot, including taking an object-oriented perspective to observe things and solve problems. OOP means that creating programs by using objects and creating objects. For an OOP program, the first thing a programmer thinks is what the objects the program would use to solve the problem and build the system, but not solving problem itself. Using and creating different objects are daily common through reusing provided code. Objects are the elements of the program.

Creating new objects by instancing new classes which are the inheritances of old classes is a sub-problem of creating different objects.

All efforts of reusing code in C++ serves classes-creating or type-creating which serves objects-creating. The program uses objects to build systems and solve the problem.




What is inheritance? Why is this facility designed like this? How to understand the Is-a relationship and has-a relationship?

In real and artificial world, the features of more complicated and concrete things or objects are sets of simple and abstract features! So, using integrated sets of abstract concepts and features to describe a special object is a natural developed thought. This thought or process of thinking can be utilized to model concepts from more general to more special!

Formal language with its self-contained is symbolized and information-condensed, formally, also describes the abstract concepts and relationship among them, for example, as mathematics does. Using formal language to model system and represent problem is amazing and powerful. Computer language is one of formal language. Using the thought(the process of thinking ) of utilizing abstract concepts to compose special objects is great.

As a computer language, C++ provides more facilities than C to reuse the code, and that makes you could concentrate on your project without concerning on any changes in a provided and reused code. You have many choices to use the same class code by public, protected and private inheritance. Every inheritance strategy is a concise and different consideration about the relationship between the provided code and the new class. The art of OOP is the consideration about the whole picture, which is described by your program and the provided nice designed libraries.

The implementation of inheritance is simple and easy to understand. When you need other debugged written code, you can modify and add new features to it, but you may break the old pure code with introducing new bug in it. So, you have to invent new machinery to deal with this problem. The answer is working with the compiler through inheritance mechanism.
Understanding the Whole picture and essence of inheritance
The mechanism of inheritance, including single or multiple public, protected and private inheritance, makes the features of base class, namely the protected and public members, to be public, protected or private. In terms of the accessing right of members in derived class, program can get more features from the public inheritance mechanism than protected and private. That means protected and private hide more features of base classes. However, in the derived class inherited in public, protected and private, the public methods can still access the methods of base class. Only in public inheritance, can program get all the features of the classes of derived and base. In OOP, all classes’ methods are useful, so hiding more features is not good at one hand. Program hopes to visit all the methods from the outside of the world, at the other. So, it is a balance that making inheritance properly, at one hand, fulfills the hiding command, at other hand, meets the design of entire program.

For the coupled data and methods called subobject, other methods can only access these data by their coupled methods in any style of inheritance!

Subobjects are real components of the program.

Understanding and Using public inheritance
For the program, no matter which the class is, what program can access or see is the public members of the derived class and all of the public members of inherited classes by this class. The program can get all the public by “->” and “.”. The newest class of every public inheritance level can access the inherited classes’ data members only with the inherited classes’ public methods.

When a new class inherits many features from classes publicly, this class has interfaces of the base classes. When a class which inherited many features from other classes publicly and when an object of derived class is created in memory, lots of objects of base classes would be created also by calling a chains of constructors! The fact that an object of class is created equals to the creation of many base objects! In short, an object of newest class are a set of nameless objects. Program can access these public methods in these derived classes’ objects. The newest class is a kind of base class, this relationship respects the restriction added by base class to newest class. Conflict between the general and individual leads the polymorphism.


Understanding and Using private inheritance


Understanding and Using Multiple (public, private or protected) Inheritance
Base class virtual or abstract is a kind of special class, many classes could inherit its features, also it can be a subject of any class, more classes may have a shared base class! Mechanism of Multiple inheritance makes a class may inherit more than one class at a time! This fact can bring the new class more features which come from different classes or the same classes. In short, new classes hope to inherit pure different features, which maybe have same name, from different base classes and MI brings many problems to be deal with.

How to make sure a class inherit pure features after multiple inheritance and a chain of inheritance is the issue. What can be done is that making the base class virtual.

Is-a and has-a is very different!
If the derived class, publicly, can still maintain the methods of base class, that means the outside world can access the methods of base class through an object of derived class directly! That is is-a relationship.

Private, protected inheritance and containment make the derived class has an implement of base class. That means the outside world can not access the methods of base class directly via an object of derived class.

But, the derived class would, with inheritance implicitly, have nameless subobjects-whose features are exposed to outside world-of base classes, and named subobjects with containment explicitly.

1,Public Inheritance:

Real Meaning of Public Inheritance
The style, process and results of public inheriting means a lot. One of the results is that derived class is a kind of base class, with the influence to type cast!

First, the objects of derived class can be assigned to the objects of base class without explicit expression.
Second, the reference and pointer of base class can refer and point to the objects of derived class without explicit expression.
Third, the derived classes can rewrite the methods of base class to implement different behaviors.That is polymorphic inheritance.

Private, Protected, Public members with private, protected, public Inheritance
Protected members in class:
Why is protected needed?

Template: another smart invention to reuse code for building a class or function, giving C++ a new level of abstraction

Why do we need template even STL?
To answer this question, a thinking about general and special is needed. Class template and function template give generic description of a kind of class and function. Reusing the template can make many special classes and functions in terms of different types. Template is another technology or facility to reuse already packaged code in libraries, comparing with inheritance and containment.

On the another hand, ADTs, such as stack, queue,list and tree, need templates to generically generate special and concrete versions of ADT in terms of different types, and STL just makes it done.

A template is more general and abstract than class specialized by some data type. In a set of classes and templates which consist of the conceptual world paralleling with the real world, templates are more abstract than classes, classes are concrete and real “things” in conceptual world, while the templates are relatively more abstract concepts. In that conceptual paralleling world, the abstract concept and concrete concept are relative.

Additionally, templates are convenient when, generally, discussing about some general algorithms-- sorting and searching, without the special types.

The function of template is generating types(templates or classes) by given specifying parameters through identical description!
原创粉丝点击