Function Overloading: A function (including constructor) can have many versions, differentiated by its parameter list (number, types and orders of parameters). Caller can choose to invoke a particular version by matching the parameter list.
Function Default Argument: In C++, default values can be assigned to trailing function's parameters. If the caller does not supply these arguments, compiler would insert the default value accordingly. For example,
// The Point class declaration (in header file "Point.h")
class Point {
......
Point(int = 0, int = 0);
}
// The Point class implementation (Point.cpp)
Point::Point(int x, int y) : x(x), y(y) { }
// Test Driver (TestPoint.cpp)
Point p1; // (0, 0)
Point p2(4); // (4, 0)
Point p3(5, 6); // (5, 6)
➔The default value shall be specified in the class declaration. I shall NOT be specified in the function implementation.
➔Default argument is applicable to all functions, including constructor function.
Constructing Instances: As mentioned, instances (or objects) are concrete realizations of a class. A constructor would be invoked when declaring an instance to initialize the instance. The syntax to declare an instance is:
ClassName instanceName; // Invoke default constructor
ClassName instanceName(constructor-parameter-list); // Invoke the constructor with matching parameter list
Public Getters and Setters for Private Variables: Member variables are usually declared private to prevent direct access (called data hiding). Instead, public getter and setter are defined to retrieve (get) and modify (set) the private member variable.
Implementing Member Functions in Class Declaration: You can include the function's implementation inside the class declaration, as follows:
/* Point with integer coords with inline functions (PointInline.h) */
#include
using namespace std;
// Class Declaration with inline implementation
class Point {
private:
int x, y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) { }
int getX() const { return x; }
int getY() const { return y; }
void setX(int x) { this->x = x; }
void setY(int y) { this->y = y; }
void setXY(int x, int y) { this->x = x; this->y = y; }
void print() const {
cout << "Point @ (" << x << "," << y << ")";
}
};
Functions that implemented inside the class declaration are automatically treated as inline functions. That is, they will be expanded in place by the compiler (if the compiler chooses to do so), instead of performing a more expensive function call.
Dot (.) Member Selection Operator: Dot operator (.) is used to access public class members, in the form of instanceName.memberName, e.g., point1.getX(), point2.print().
Arrow (->) Member Selection Operator: Arrow operator (->) is used with object pointer. Suppose *pObj is a pointer to an object, instead of using (*pObj).member to select a member, it is more convenient to use the arrow notation, in the form of pObj->member.
Memberwise Assignment: The assignment operator (=) can be used to assign one object into another object (of the same class), in memberwise manner. For example,
Point p1(4, 5);
Point p2 = p1; // Also (4, 5)
(Note: The compiler automatically generates an implicit assignment operator operator=(), which performs memberwise copying.)
Passing Objects into Function: Objects are passed by value into function. A copy is created (via memberwise assignment) and passed into the function. The caller's copy cannot be modified inside the function.
Pass by reference can be archived by passing an object pointer or an object reference.
Object Pointer and Dynamic Allocation: To allocate an instance of a class dynamically, define an object pointer and use new operator to allocate the storage. The new operator return a pointer pointing to the storage allocated. You could use arrow operator -> to access the members with object pointer in the form of pObj->member (same as (*pObj).member). You need to use the delete operator to free the allocated storage.
Similarly, you can use new[] and delete[] to dynamically allocate array of objects.
Function Default Argument: In C++, default values can be assigned to trailing function's parameters. If the caller does not supply these arguments, compiler would insert the default value accordingly. For example,
// The Point class declaration (in header file "Point.h")
class Point {
......
Point(int = 0, int = 0);
}
// The Point class implementation (Point.cpp)
Point::Point(int x, int y) : x(x), y(y) { }
// Test Driver (TestPoint.cpp)
Point p1; // (0, 0)
Point p2(4); // (4, 0)
Point p3(5, 6); // (5, 6)
Notes:
➔The default value can only be assigned to the trailing arguments.➔The default value shall be specified in the class declaration. I shall NOT be specified in the function implementation.
➔Default argument is applicable to all functions, including constructor function.
Constructing Instances: As mentioned, instances (or objects) are concrete realizations of a class. A constructor would be invoked when declaring an instance to initialize the instance. The syntax to declare an instance is:
ClassName instanceName; // Invoke default constructor
ClassName instanceName(constructor-parameter-list); // Invoke the constructor with matching parameter list
Public Getters and Setters for Private Variables: Member variables are usually declared private to prevent direct access (called data hiding). Instead, public getter and setter are defined to retrieve (get) and modify (set) the private member variable.
Implementing Member Functions in Class Declaration: You can include the function's implementation inside the class declaration, as follows:
/* Point with integer coords with inline functions (PointInline.h) */
#include
using namespace std;
// Class Declaration with inline implementation
class Point {
private:
int x, y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) { }
int getX() const { return x; }
int getY() const { return y; }
void setX(int x) { this->x = x; }
void setY(int y) { this->y = y; }
void setXY(int x, int y) { this->x = x; this->y = y; }
void print() const {
cout << "Point @ (" << x << "," << y << ")";
}
};
Functions that implemented inside the class declaration are automatically treated as inline functions. That is, they will be expanded in place by the compiler (if the compiler chooses to do so), instead of performing a more expensive function call.
Dot (.) Member Selection Operator: Dot operator (.) is used to access public class members, in the form of instanceName.memberName, e.g., point1.getX(), point2.print().
Arrow (->) Member Selection Operator: Arrow operator (->) is used with object pointer. Suppose *pObj is a pointer to an object, instead of using (*pObj).member to select a member, it is more convenient to use the arrow notation, in the form of pObj->member.
Memberwise Assignment: The assignment operator (=) can be used to assign one object into another object (of the same class), in memberwise manner. For example,
Point p1(4, 5);
Point p2 = p1; // Also (4, 5)
(Note: The compiler automatically generates an implicit assignment operator operator=(), which performs memberwise copying.)
Passing Objects into Function: Objects are passed by value into function. A copy is created (via memberwise assignment) and passed into the function. The caller's copy cannot be modified inside the function.
Pass by reference can be archived by passing an object pointer or an object reference.
Object Pointer and Dynamic Allocation: To allocate an instance of a class dynamically, define an object pointer and use new operator to allocate the storage. The new operator return a pointer pointing to the storage allocated. You could use arrow operator -> to access the members with object pointer in the form of pObj->member (same as (*pObj).member). You need to use the delete operator to free the allocated storage.
Similarly, you can use new[] and delete[] to dynamically allocate array of objects.
Comments
Post a Comment