CS202 Week2
CS202 Week2
Week 2
Constructors & Destructor
10/2021
CS202 – What will be discussed?
o Constructors
o The this pointer
o Destructor
o Member Initialization
o Copy constructor
o Assignment operator
class Date
{ int main()
public: {
Date(int iNewDay); Date today; //error
... …
private: return 0;
... }
};
o Syntax: correct
o Semantic: legal
o Useful: NO!!!
dbtien @ Programming Systems
The code should be
Date::Date(int iDay, int iMonth, int iYear)
{
this->iDay = iDay;
this->iMonth = iMonth;
this->iYear = iYear;
}
int main()
{
Rectangle a;
Rectangle b(a); // invoke copy constructor
Rectangle c = a; // invoke copy constructor
}
ptr ptr
a a
b b
ptr ptr