1 / 12

CSCI 383

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

kim
Download Presentation

CSCI 383

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 383 Object-Oriented Programming & Design Lecture 10 Martin van Bommel

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

More Related