OOP C2 Class
Reference
ref
Introduction to Programming with C++ Page 250-259.
Reference is a new way to manipulate objects in C++.
A reference variable: An alias for another variable, The original data stored in the variable can be accessed and modified through its reference variable.
Declaration
Place the ampersand &
in front of the variable or after the data type for the variable.
1 |
|
-
Local or global variables:
type& refname = name;
For ordinary variables, the initial value is required.
-
In parameter lists and member variables:
type& refname
Binding defined by caller or constructor.
1
2
3void f(int& x);
f(y); //initialized when f is called
f(y + 2); //warn or err: y + 2 is not a virable,thus the f to recive is missing- Parameters can be passed by reference thus changes made to the parameters inside the function also made to the arguments.
- When passing an argument by value, the argument can be a literal, a variable, an expression, or even the
return value of another function; however, the argument must be a variable when it comes to reference. - Pass-by-reference is more efficient than pass-by-value because for some object types objects a lot of memory are taken.
A constant reference parameter could be specified to prevent its value from being changed by accident.
1
2const char &cr = c; //cannot modify cr,but can modify c
void func(const int& a);
Pointers vs. References
Pointers | Reference |
---|---|
can’t be null, initialized when declare | can be set to null and intialized without initialized value. |
an alias for an variable, dependent on an existing variable | independent of existing objects |
can’t change to a new “address” location | can change to point to a different address |
Restriction
- No references to references
- No pointers to references, but Reference to pointer is ok
- No arrays of references
Class
ref
Introduction to Programming with C++ Page 362-.
A class defines the properties and behaviors for objects.
example: Point
In C,
1 |
|
And the prototypes are
1 |
|
In C++, declarations are allowed and no more typedef
need for class
.
1 |
|
And the definitions of the functions of Point are
1 |
|
where ::
is the resolver(域解析器).
<Class Name>::<function name>
for<Class Name>
::<function name>
for global.
1 |
|
Declaration and Definition
- The class declaration, along with the prototype of the member functions should be put into a header file.
- The definition of the member functions should be put into another source file.
UML Diagram
Member functions
Constructors
Constructors, designed to perform initializing action, are invoked when a new object is created.
The compiler automatically(guaranteed) calls that constructor at the point an object is created if a class has a constructor, before client programmers can make operations.
peculiarities:
- Constructors must have the same name as the class itself.
- Constructors do not have a return type—not even void.
- Constructors,play the role of initializing objects, are invoked when an object is created.
- It is a common mistake to put the void keyword in front of a constructor.
- A (local or global) variable can be declared and initialized in one statement. However as a class member, a data field cannot be initialized when it is declared before C++ 11 standard.
1 |
|
Default constructor
A default constructor: one that can be called with no arguments.
There may be no default constructors.
1
2
3
4
5
6
7
8
9
10
struct Y {
float f;
int i;
Y(int a); //NOt a default constructor
};
Y y0[3] = {1,2,3}; //OK
Y y1[] = { Y(1), Y(2), Y(3) }; //OK
Y y2[3] = { Y(1),Y(2) }; //Too few
Y y3[7]; //Too few
Y y4; //No paramater
- The compiler ensures that construction always happens.
- The compiler will automatically create one for you if and only if there are no constructors for a
class / struct
.
Destructor
The destructor is called automatically by the compiler when the object goes out of scope.
The only evidence for a destructor call is the closing brace of the scope that surrounds the object.
The destructor is named after the name of the class with a leading tilde (~).The destructor never has any arguments.
Call a function
1 |
|
There is a relationship with the function be called and the variable to call it.
The function itself knows it is doing something with the variable. But how? By this
.
Keyword this
ref
Introduction to Programming with C++ Page 455.
this:
- a hidden parameter for all member functions, with the type of the class;
- a natural local variable of all structs member functions that you can not define, but can use it directly;
- a built-in pointer to calling object itself.
1 |
|
can be recognized as
1 |
|
Inside member functions, this can be used as the pointer to the variable that calls the function.
Must specify a variable to call the function.
Object
An object represents an entity in the real world that can be distinctly identified. An object has a unique identity, state, and behavior.
An object is an instance of a class.
- State/Properties/Attributes/Data: the properties or status, represented by data fields with their current values.
- Behavior/Services/Operations: the functions, and to invoke a function on an object is to ask the object to perform an action.
An Objects is just a variable, and the purest definition is “a region of storage”. The struct
variables learned before are just objects in C++.
Obeject-oriented thinking
Characteristics
- Everything is an object.
- program is a bunch of objects telling each other what to do by sending messages.
- Each object has its own memory made up of other objects.
- Every object has a type.
- All objects of a particular type can receive the same messages.