2.2 CONSTRUCTORS
Constructors are a way to provide an initialization that is guaranteed to be called. A constructor is a member function with these special properties:
- The name of a constructor must be the same as the name of the class.
- A constructor does not have any return value. The compiler knows that every constructor has no return value. but a compiler error occurs if you actually write void at the front of the constructor's head.
- If a class has a constructor, then a constructor is called automatically whenever a variable of the class is declared.
Adding a constructor to the throttle class
Our throttle constructor has one parameter, which tells the total number of positions that the throttle contains. Here is the prototype for the new constructor, along with its precondition/postcondition
contract:
throttle (int size)
// Precondition: 0 < size.
// Postcondition: The throttle has size positions above the shutoff position,
// and its current position is off
The constructor's prototype is placed in the throttle class definition along with the other member functions' prototypes, as indicated here:
class throttle
{
public:
// CONSTRUCTOR
throttle (int size);
// MODIFICATION MEMBER FUNCTION
void shut_off();
...
Let us see some examples of using the constructor in declarations of throttle objects.throttle mower_control (4);
throttle apollo (40);
Often it is useful to provide several different constructors, each of which does a different kind of initialization. we could provide a second constructor with no parameters, The prototype for this constructor is shown here:throttle ():
// Precondition: None
// Postcondition: The throttle has one position above the shutoff position.
// and its current position is off.
A constructor with no parameters is called a default constructor. Here is a declaration of two throttles, with the first using the default constructor and the second using the other constructor:throttle toggle;
throttle complicated (100);
You may declare as many constructors as you like----one for each different way of initializing an object. Each constructor must have a distinct parameter list so that the compiler can tell them apart. Only one default constructor allowed.class throttle
{
public:
// CONSTRUCTORS
throttle();
throttle (int size);
// MODIFICATION MEMBER FUNCTIONS
void shut_off ();
void shift (int amount);
// CONSTANT MEMBER FUNCTION
double flow () const;
double is_on () const;
private:
int top_position;
int position;
};
throttle::throttle()
{
top_position = 1;
position = 0;
}
throttle::throttle(int size)
{
assert (0 < size);
top_position = size;
position = 0;
}
What Happens If You Write a Class with No Constructors?
If you write a class with no constructors, then the compiler automatically creates a simple default constructor. This automatic default constructor does not do much work. Generally, you should write your own constructors, including your own default constructor,
rather than depending on the automatic default constructor.
Inline Member Functions
Placing a function definition inside the class definition is called an inline member function. It has two effects:
- You don't have to write the implementation later.
- Each time the short function is used in your program. the compiler will recompile the short function definition and place a copy of this compiled short definition in your code. This saves some execution time.
Notice that when you declare an inline member function, there is no semicolon before the opening curly bracket or after the closing curly bracket.
WHEN TO USE AN INLINE MEMBER FUNCTION
Inline functions cause some inefficiency---your compiled code might be longer than it needs to be. Inline functions also result in a messier class definition, which is harder to read and harder
to debug.
Because of these problems, we recommend using an inline member function only for the simple situation when the function definition consists of a single short statement.
class throttle
{
public:
// CONSTRUCTORS
throttle();
throttle (int size);
// MODIFICATION MEMBER FUNCTIONS
void shut_off () {position = 0; }
void shift (int amount);
// CONSTANT MEMBER FUNCTION
double flow () const {return position / double (top_position); }
double is_on () const {return (position > 0); }
private:
int top_position;
int position;
};
2.3 USING A NAMESPACE, HEADER FILE, AND IMPLEMENTATION FILE
We 'd like to do so without revealing all the details of the new class's implementation. In addition, we do not want other programmers to worry about whether their own selection of names for
variables and such will conflict with the names that we happen to use.
These goals are accomplished by three steps:
- Creating a namespace
- Writing the header file
- Writing the implementation file
Creating a Namepace
The solution of name conflict, we can use an organizational technique called a namespace.
A namespace is a name that a programmer selects to identify a portion of his or her work.
All work that is part of our namespace must be in a namespace grouping, in the following form:
namespace main_savitch_2A
{
}
The word namespace is a C++ keyword, The word main_savitch_2A is the name that we chose for our namespace. A single namespace, such as main_savitch_2A, may have several different namespace grouping. For example, the throttle class definition can appear in a namespace grouping for main_savitch_2A,
later, we can open a second namespace grouping for main_savitch_2A, and place the function definitions in that second grouping., they don't need to be in the name file.they appear in two separate files:
- The class definition appears in a header file that provides all the information that a programmer needs in order to use the class.
- The member function definitions appear in a separate implementation file.
The Header File
The class definition for the new class appears in a namesapce grouping after the header file comment.
avoid duplicate definition by using a
macro guard. as follows:
#ifndef MAIN_SAVITCH_THROTTLE_H
#define MAIN_SAVITCH_THROTTLE_H
namesapce main_savitch_2A
{
class throttle
{
};
}
#endif
The Implementation File An implementation file for a new class has several items:
- comment
- include directives
- reopen the namespace and define the implementations
// file: throttle.cxx
// CLASS IMPLEMENTED: throttle
#include <cassert>
#include "throttle.h"
namespace main_savitch_2A
{
throttle::throttle()
...
}
本文是关于C++学习的笔记,主要聚焦在构造函数的使用,包括构造函数名称与类名相同、无返回值以及声明时的自动调用。此外,还讨论了如何使用命名空间、头文件和实现文件,包括创建命名空间、编写头文件和实现文件的细节。
1225

被折叠的 条评论
为什么被折叠?



