120 likes | 244 Views
CSCI 383. Object-Oriented Programming & Design Lecture 10 Martin van Bommel. Constructors. A constructor is a method that is executed automatically whenever an instance is created If a C++ method is a constructor It has the same name as the class to which it belongs
E N D
CSCI 383 Object-Oriented Programming & Design Lecture 10 Martin van Bommel
Constructors • A constructor is a method that is executed automatically whenever an instance is created • If a C++ method is a constructor • It has the same name as the class to which it belongs • It has no return type specified • A class can have more than one constructor (this is an example of function overloading) • Handout #1 CSCI 383 Lecture 2 M. van Bommel
Constructor Execution • When is a constructor executed? • When an instance is declared person p1(“John”, 20); • When an instance is created using new person* pPtr; pPtr = new person(“John”, 20); • When an argument is passed using call-by-value void process(person p); • When a temporary instance is created person p1(“John”, 20); p1 = person(“Mary”, 19); CSCI 383 Lecture 2 M. van Bommel
Four Categories of Constructors • Default constructors • system generated and user-defined • Copy constructors • system generated and user-defined • Conversion constructors • All other constructors CSCI 383 Lecture 2 M. van Bommel
Default Constructors • A default constructor is one that has no parameters • The system automatically generates a default constructor for a class if and only if the programmer does not define any constructor for the class. • It is called a system-generated default constructor • A system-generated default constructor does nothing (i.e., the constructor body is empty) • A default constructor is called when an instance is created, but no arguments are specified person p1; person p2(); // NO! What is this doing? CSCI 383 Lecture 2 M. van Bommel
Default Constructors • Built-in types, such as int, can be viewed as classes that have default constructors • When an array of class instances is declared, the default constructor for the class is called for each instance, in the order of their index numbers CSCI 383 Lecture 2 M. van Bommel
Constructors and Data Members • If a class has data members, the constructors of those data members are executed prior to the constructor of the class containing the data members • Unless otherwise specified, the default constructors of the data members are called when an instance of the class containing the data members is created class personRecord { private: person p; char* address //… public: personRecord(const char* n, int a, const char* addr); //… }; CSCI 383 Lecture 2 M. van Bommel
Constructors and Data Members • But one can specify different constructors to be called for the data members personRecord(const char* n, int a, const char* addr) : person(n, a) { … } • This technique can also be used to initialize data members that belong to a built-in type CSCI 383 Lecture 2 M. van Bommel
Copy Constructors • A copy constructor is one that has exactly one parameter, where the parameter is an reference to an instance of the same class • The system automatically generates a copy constructor for a class if and only if the programmer does not define a copy constructor for the class. • It is called a system-generated copy constructor • A system-generated copy constructor does a member-wise copy of the parametric instance to the new instance • One should always write a copy constructor if the members of a class point to dynamically allocated space. WHY? CSCI 383 Lecture 2 M. van Bommel
Conversion Constructors • A conversion constructor is a constructor that has exactly one parameter, where the parameter is not an instance of the same class • Conversion constructors are not generated automatically by the compiler CSCI 383 Lecture 2 M. van Bommel
Destructors • A destructor is a method that is executed when an instance is destroyed (or goes out of scope) • If a C++ method is a destructor • Its name if the name of its class, preceded by a tilde (~) • It has no return type specified • It has a void parameter list • Destructors are generally used to “clean up” before an instance is deallocated • The system automatically generates a destructor for a class if and only if the programmer does not define one. • It is called a system-generated destructor • The system-generated destructor does nothing (i.e., the method body is empty) • The destructors of an object’s data members are executed after the destructor of the class containing the members CSCI 383 Lecture 2 M. van Bommel
A Day in the Life of an Object • Creation (allocation) • Component constructors are called • Constructor is called • Instance is manipulated • Destructor is called • Component destructors are called • Destruction (deallocation) CSCI 383 Lecture 2 M. van Bommel