OOP C4 Function
Variables
local variable
Local variables are defined inside a method, have a scope limited to the method to which they belong.
- A
local variable
of the same name as a field will prevent the field being accessed from within a method. Local variables
are defined inside the body of a constructor or method. They can be initialized and used only within the body of their defining constructor or method.Local variables
must be initialized before they are used in an expression – they are not given a default value.Local variables
have ascope
that is limited to the block in which they are defined. They are not accessible from anywhere outside that block.
Fields
Fields: defined outside constructors and methods.
Fields
are used to store data that persists throughout the life of an object. As such, they maintain the current state of an object. They have a lifetime that lasts as long as their object lasts.Fields
haveclass scope
: their accessibility extends throughout the whole class, and so they can be used within any of the constructors or methods of the class in which they are defined.- But as long as they are defined as private, fields cannot be accessed from anywhere outside their defining class.
Formal parameter
Formal parameters: defined in the header of a constructor or method.
Formal parameters
receive their values from outside, being initialized by the actual parameter values that form part of the constructor or method call.Formal parameters
have ascope
that is limited to their defining constructor or method.
Relationship of three
-
Formal parameters
andlocal variables
persist only for the period that a constructor or method executes.Their lifetime is only as long as a single call, so their values are lost between calls. As such, they act as temporary rather than permanent storage locations.
-
All three kinds of variable are able to store a value that is appropriate to their defined type.
C++ access control
public
All member declarations that follow are available to everyone.
struct
defaults topublic
.
private
No one can access that member except inside function members of that type.
class
defaults toprivate
.
Friends
Friend : a way to explicitly grant access to a function that isn’t a member of the structure.
- The class itself controls which code has access to its members.
- Can declare a global function, a member function of another class, or even an entire class, as a friend.
protected
Initialization
Initializer list
- Can initialize any type of data
- pseudo-constructor calls for built-ins
- No need to perform assignment within body of ctor
- Order of initialization is order of declaration – Not the order in the list!
- Destroyed in the reverse order.
Initialization vs. assignment
1 |
|
initialization: before constructor
assignment: inside constructor
string must have a default constructor.
Arguments
Overloaded constructors / Function overloading
Same functions with different arguments list.
Default arguments
A default argument is a value given in the declaration that the compiler automatically inserts if you don’t provide a value in the function call.
-
defaults must be added from right to left !
1
2int harpo(int n, int m = 4, int j = 5);
int chico(int n, int m = 6, int j); //illeagle -
The default arguments are declared in its prototype, not defined in its function head.
function and Function call
Overhead(开销) for a function call
The processing time required by a device prior to the execution of a command:
- Push parameters
- Push return address
- …
- Prepare return values
- Pop all pushed
inline function
ref
Introduction to Programming with C++ P244 -
An inline function is expanded in place, like a preprocessor macro, so the overhead of the function call is eliminated: inline functions
are not called; rather, the compiler copies the function code in line at the point of each invocation.
1 |
|
- Repeat
inline
keyword at declaration and definition. - An
inline function
definition may not generate any code in.obj
file. - Any function you define inside a class declaration is automatically inline.
in header file
We can put inline functions’ bodies in header file
, then #include
it where the function is needed.
- Never be afraid of multi-definition of inline functions, Definitions of inline functions are just declarations. since they have no body at all.
Tradeoff
Body of the called function is to be inserted into the caller.
- May expand the code size
- But deduces the overhead of calling time.
It gains speed at the expenses of space. In most cases, it is worth.
Inline function is much better than macro in C.
It checks the types of the parameters.
Sp: Inline may not in-line
The compiler might decide not make it inline
if
- it finds the function is too large
- it finds the function calls itself (recursion is not allowed or indeed possible for inline functions)
- the feature might not be implemented for your particular compiler.
Pit-falls
- You can put the definition of an inline member function out of the class braces.
- But the definition of the functions should be put before where they may be called.
Reducing clutter
Member functions
defined within classes
use the in situ (in place) and maintains that
all definitions should be placed outside the class to keep the interface clean.
Inline or not?
Inline | Not inline |
---|---|
Small functions, 2 or 3 lines | Very large functions, more than 20 lines |
Frequently called functions | Recursive functions |
A lazy way is to Make all your functions inline, or Never make your functions inline.