[C++] Chap 07 - Constructors and Other tools
[C++] Chap 07 - Constructors and Other tools
Constructors
- Initialization of class object (Called when objects are declared)
- Must have the same name as the class
- No output type. (NOT even void)
- Placed in the public section
- Values in parentheses are passed to arguments to constructor.
1
2
3
DayOfYear date1(7, 4), date2(5, 5);
// Called when declaring class object
date1.DayOfYear(7, 4); //Very ILLEGAL. Cannot call constructor like other member functions
Constructor definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DayOfYear::DayOfYear(int monthValue, int dayValue) {
{
month = monthValue;
day = dayValue;
}
// First DayOfYear : class name
// Second DayOfYear : constructor name
// Note no return type
DayOfYear::DayOfYear(int monthValue, int dayValue) : month(monthValue), day(dayValue)
{
// Body doesn't have to be empty
// Can validate the data!
// Ensure only appropriate data is assigned to class private member variables
}
- Constructers can also be overloaded.
- Constructor which doesn’t have any parameter, is called default constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class DayOfYear {
public :
DayOfYear(int monthValue, int dayValue);
private :
const int month;
const int day;
};
DayOfYear::DayOfYear(int monthValue, int dayValue) {
{
month = monthValue;
day = dayValue;
}
// since month and day are const member variables,
// cannot declare like month = monthValue;
// because the 'month' should be initialized when created
Explicit Constructor Calls
1
2
3
holiday = DayOfYear(5, 5);
// Create and initialize an anonymous object
// and assign it to holyday.
- Quite different from standard member function call (since it has no return type)
Default Constructors
- If no constructors defined, a default constructor doing nothing will be automatically created
- Otherwise, if you want to declare like
SampleClass myVar;, need to define a default constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
class SampleClass
{
public:
SampleClass(int param1, double param2);
SampleClass();
void doStuff();
private:
int data1;
double data2;
};
int main() {
SampleClass myvar, myvar2(1, 2.0);
// if you want to define myvar, needto define default constructor
Class member variable
- Call back to member object’s constructor
1
2
3
Holiday::Holiday() : date(1, 1), parkingEnforcement(false){
}
//date(1, 1) is a invocations of constructors from the class 'DayOfYear'
More tools
Const modifier for functions
- Mark a funciton with a
constmodifier so as to notify it should not change the values of the object - If const is used for a class type, then every member function should have const modifier.
1
2
3
4
5
void foo(const BankAccount& ba)
{
bar;
ba.output(); // ILLEAGL if output does not have the const modifier
}
Three uses of Const
const double pi=3.1416;Global variablevoid display(const Student& a);Parametervoid show() const;Function modifier
Inline Member functions
- Function definition can be defined in the class definition
- Efficient for short definitions
- Can only be called in the same file as the one in which it is defined
Maybe not included..
Static member variables
- All object of class share only one copy.
- On object changes it, all can see the change
- Similar to global var. But only for the objects of the class
Static member functions
- Cannot access the object’s non-static variable data
- Only access static data/function of class
- Nonstatic member function can also access static variables of the class
1
Server::getTurn(); // Class::func(); format
- Every static variable must be initialized outside the class function : only once!
This post is licensed under CC BY 4.0 by the author.