Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CQG / Задание1 / HDS_VCPP_Programming Rules.doc
Скачиваний:
5
Добавлен:
16.04.2013
Размер:
327.68 Кб
Скачать

Высшая Школа Программирования

Правила программирования на С++

Programming in C++ Rules and Recommendations

Introduction 4

Terminology 4

Source Code in Files 6

Code Structure 6

Naming Files 6

Comments 6

Include Files 7

Assigning Names 8

Style 9

Classes 9

Functions 10

Compound Statements 10

Flow Control Statements 11

Pointers and References 11

Miscellaneous 11

Classes 12

Considerations Regarding Access Rights 12

Inline Functions 12

Friends 12

const Member Functions 13

Constructors and Destructors 13

Assignment Operators 14

Operator Overloading 14

Member Function Return Types 15

Inheritance 15

Templates 15

Functions 15

Function Arguments 15

Function Overloading 16

Return Types and Values 16

Inline Functions 16

Temporary Objects 17

General 17

Constants 17

Variables 17

Pointers and References 18

Type Conversions 18

Flow Control Structures 19

Expressions 20

Memory Allocation 20

Fault Handling 21

Portable Code 22

Localization 22

Data Abstraction 22

Sizes of Types 23

Type Conversions 23

Data Representation 23

Underflow/Overflow 23

Order of Execution 23

Temporary Objects 24

Pointer Arithmetic 24

References 24

Appendix 25

A. Hungarian Notation 25

B. MS Visual C++ Resource Prefixes 26

C. Examples 27

Introduction

The purpose of this document is to define the style of programming in C++. The rules and recommendations presented here are not final, but should serve as a basis for continued work with C++. This collection of rules should be seen as a dynamic document; suggestions for improvements are encouraged.

Many different C++ implementations and class libraries are in use today. The rules and recommendations of this document mostly concern Microsoft Visual C++ and its class libraries, MFC and ATL.

Programs that are developed according to these rules and recommendations should be:

  • correct;

  • easy to maintain.

In order to reach these goals, the programs should:

  • have a consistent style;

  • be easy to read and understand;

  • be portable as possible to other architectures;

  • be free of common types of errors;

  • be maintainable by different programmers.

Questions of design, such as how to design a class or a class hierarchy, are beyond the scope of this document. This document does not also contain guiding principles for any particular software technology such as COM (Component Object Model).

In order to obtain insight into how to effectively deal with the most difficult aspects of C++, the provided examples (see Appendix C) should be carefully studied. C++ is a difficult language where there may be a very fine line between a feature and a bug. It places a large responsibility upon the programmer. In the same way as for C, C++ allows a programmer to write compact and, in some sense, unreadable code.

The examples often include class definitions having the format “class {};”. It is done to shorten them. It is not recommended that class definitions be written in this way. In order to make the code more compact, the examples provided do not always follow the rules. For example, all required comments are usually omitted.

The requirements of the document are split into two parts: rules and recommendations. The rules should never be broken. All exceptions to the rules are fully stated. The recommendations may be ignored if the program logic drastically forces you to do it.

Terminology

  1. An identifier is a name used to refer to a variable, constant, function, or type in C++. When necessary, an identifier may have an internal structure that consists of a prefix, a name, and a suffix (in that order).

  2. A class is a user-defined data type that consists of data elements and functions operating on that data. In C++, it may be declared as a class; it may also be declared as a struct or a union. Data defined in a class is called member data and functions defined in a class are called member functions or methods.

  3. A class/struct/union is said to be an abstract data type if it does not have any public or protected member data.

  4. A structure is a user-defined type for which only public data is specified.

  5. Public membersof a class are member data and member functions that are everywhere accessible by specifying an instance of the class and the name.

  6. Protected membersof a class are member data and member functions accessible by specifying the name within member functions of derived classes.

  7. A class template defines a family of classes. A new class may be created from a class template by providing values for a number of arguments. These values may be names of types or constant expressions.

  8. A function template defines a family of functions. A new function may be created from a function template by providing values for a number of arguments. These values may be names of types or constant expressions.

  9. A function body is a compound statement containing the statements that specify what the function does.

  10. An enumeration type is an explicitly declared set of symbolic integral constants. In C++ it is declared as an enum.

  11. A typedef is another name for a data type, specified in C++ using a typedef declaration.

  12. A reference is another name for a given variable. In C++, the address of (&) operator is used immediately after the data type to indicate the declared variable, constant, or function argument is a reference.

  13. A macro is a name for a text string defined in a #define statement. When this name appears in source code, the compiler replaces it with the defined text string.

  14. A constructor is a function that initializes an object.

  15. A copy constructor is a constructor whose first argument is a reference to an object of the same type as the object to be initialized.

  16. A default constructor is a constructor that needs no arguments.

  17. An overloaded function name is a name used for two or more functions or member functions having different types.

  18. An overridden member function is a member function in a base class re-defined in a derived class. Such a member function is declared virtual.

  19. A pre-defined data type is a type defined in the language itself, such as int.

  20. A user-defined data type is a type defined by a programmer in a class, struct, union, enum, or typedef definition or as an instantiation of a class template.

  21. A pure virtual function is a member function for which no implementation is provided. Pure virtual functions are specified in abstract base classes and must be defined (overridden) in derived classes.

  22. An accessor is a function that sets or returns the value of a data member.

  23. A forwarding function is a function that does nothing more than call another function.

  24. A constant member function is a function that cannot modify data members.

  25. An exception is a run-time program anomaly detected in a function or member function. Exception handling provides for the uniform management of exceptions. When an exception is detected, it is thrown (using a throw expression) to the exception handler.

  26. A catch clause is code executed when an exception of a given type is raised. The definition of an exception handler begins with the keyword catch.

  27. An abstract base class is a class from which no objects may be created; it is only used as a base class for the derivation of other classes. A class is abstract if it includes at least one member function declared as pure virtual.

  28. An interface is an abstract base class that does not usually provide any implementation. All its member functions are declared as pure virtual.

  29. An iterator is an object that, when invoked, returns the next object from a collection of objects.

  30. The scope of a name refers to the context in which it is visible.

  31. A compilation unit is the source code (after preprocessing) that is submitted to a compiler for compilation (including syntax checking).

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.