Translated by Nonna Shulga Copyright 2014 Tatyana Sopronyuk All rights reserved. ISBN-10: 1502520907 ISBN-13: 978-1502520906 Fragment of this book free TABLE OF CONTENTS FOREWORD Elements of object-oriented programming 1.1. Object. Class. Structure 1.2. Standard objects of the stream input/output 1.3.
Overloading operations for the types struct, union and enum 1.4. Overloading of functions 1.5. Example of operators overloading for two structural types 1.6. Templates of functions 1.7. Example of overloading functions and usage of function templates Questions for self-control Tasks for independent work 2. Work with the objects within one class 2.1.
Operating access to the members of the class 2.2. Constructors 2.3. Static members of the class 2.4. Destructors 2.5. Dynamic creation and deleting of objects 2.6. Friends of the class 2.7.
Overloading operations for classes 2.8. Binary and unary operations 2.9. Assignment operator 2.10. Operators of the call , indexing and type conversion 2.11. Overloading of the operator new and delete 2.12. Example of the elaboration of the class Set of the Integer Numbers Questions for self-control Tasks for the independent work 3.
Creation of new classes using the existing ones 3.1. Building classes with member-objects 3.2. The templates of the classes 3.3. Inheritance 3.4. Hierarchy of classes inheritance Unary inheritance 3.5. Transformation of the pointers on the objects from one hierarchy of classes 3.6.
Transformation of the types for the objects from one hierarchy of classes 3.7. Multiple inheritance Questions for self control Tasks for independent work 4. Elaboration of family classes. Abstraction 4.1. Static and dynamic polymorphism 4.2. Virtual functions 4.3.
Abstract classes 4.4. Projecting and building the class library with virtual functions 4.5. Polymorphic functions 4.6. Virtual inheritance Questions for self-control Tasks for the independent work 5. Assignments Assignment 1. Structures.
Outer functions and member-functions of the structure Assignment 2. Overloading of operations and functions. Functions templates Assignment 3. Structures and classes Assignment 4. Creation of classes Assignment 5. Creation of classes, implementing the existing classes.
Inheritance. Aggregation Assignment 6. Abstract classes LIST OF LITERATURE
FOREWORD
Object-oriented programming or OOP is methodology of programming, based on the concept that a program can be created as a collection of objects working together. This methodology focuses on the connections between objects rather than the details of their realization. All the OOP languages, especially C++, are based on three main ideas: encapsulation, polymorphism and inheritance [13, 18, 19, 22-26, 28]. Encapsulation is a mechanism linking in one construction the data and the code, which manipulates with these data.
Encapsulation also defends the part of the functions and data against the outer intrusion or wrong usage. In object-oriented programming, functions and the data with which they operate, can be linked into one whole. Such a union creates a class type. In C++ encapsulation is realized with the help of classes. Encapsulation insures the protected capsule against outer functions. Access to the code and data within the capsule is controlled by an interface which is given to the user.
Thus, class is a capsulated abstraction with the strictly defined rules of the access to the separate sections. Object is a variable of the class type (instance of the class). Object supports encapsulation. Within the object both codes and data can be private . Private codes or data are accessible only for other parts of these objects. Thus, private codes and data are not accessible for those functions existing outside the object.
If codes and data are open, then even if they exist within the object, they are accessible for other parts of the program. As a rule, the open part of the program is used in order to insure the controlled access to the private elements of the object. The object unites the codes and the data and is a variable of the class type, defined by the user. Polymorphism (from the Greek polymorphos) is a peculiarity which allows using the same words to denote several tasks, identical by the content but different as to their realization. The purpose of polymorphism, according to OOP, is the usage of one name for the task of the actions, general for the class. The exertion of every concrete action will be defined by the type of the data.
The types of the data, used as arguments at the calling of functions , denote how the concrete version of the function is really called. Thus, in C++ it is possible to use one name of the function for the realization of different actions. Such a possibility is called function overloading . The overloading of functions is one of the forms of polymorphism In more general sense the concept of polymorphism means that it is possible to create general interface for the groups, common by the content and not by the realization of actions. The advantage of polymorphism is the fact that it allows using one interface for the task of the only type of actions and makes it impossible lowering the complexity of programs. The choice of the concrete action, depending on the situation, is exerted by the compiler.
It is enough to use only the general interface. Polymorphism is also used for the operators. In fact polymorphism in the language C is used limited, in particular, in arithmetic operators. For example, the symbol + is used for the adding of integer, long integer and real numbers. In this case the compiler automatically defines which type of arithmetics is necessary. It is also possible in the language C++ to use this conception for other types of the data, put forward by the user.
So, the symbol + may be used for the exertion of the operation by adding complex numbers. For this it is necessary to define the corresponding operators function. This type of polymorphism is called overloading of operators . The main thing in polymorphism is the fact that it allows to manipulate with the objects of different complexity by means of creating for them general standard interface for the realization of identical actions. The inheritance is a process with the help of which one object acquires the peculiarities of the other one. To be more exact, the object can inherit the peculiarities of the other object and add to them features, characteristic only for its own.
Inheritance allows supporting a concept of the hierarchical classification . Without its usage for every object it would have been necessary to introduce all characteristics which would have defined it completely. By using inheritance it is possible to describe the object, indicating the general class (the family base or ancestor class) to which it belongs and those specific features that makes the object unique. The generated class inherits all the features, linked with the parent base and adds to them its own specifics characteristics. Inheritance plays the determining role in OOP. The textbook supplies the students with all the information needed in the course Object-oriented programming.
The textbook consists of 5 parts. At first we consider the possibilities of C++, which did not exist in the language C and which dont refer directly to the notion of classes, in particular, stream input/output, operators of dynamic allocation of memory and setting it free, overloading of functions, template functions. Further, we passed over from the notion of structure in the language C to the notion of the class. We described the operations of the construction of the class and its elimination, static members of the class, friendly functions, interface and realization of the class, overloading of operators for the objects of the given class, templates of classes and friendly ones. We have also considered means of using created classes (aggregation and inheritance) for the extending possibilities of existing classes and the notion of the base and derivative classes, multiple inheritance, building of the hierarchy of classes, usage of virtual functions, abstract classes, polymorphic functions, virtual inheritance. In every part of the textbook we have given examples of programs with comments, demonstrating suggested theoretical material.