1 / 16

Classes Constructors & Destructors

Department of Computer and Information Science, School of Science, IUPUI. Classes Constructors & Destructors. CSCI 240. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Constructor. Special Member Function used for Initialization -- Same Name as the Class Name

dianep
Télécharger la présentation

Classes Constructors & Destructors

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. Department of Computer and Information Science,School of Science, IUPUI ClassesConstructors & Destructors CSCI 240 Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. Constructor • Special Member Function used for Initialization -- Same Name as the Class Name • Does NOT Return a Value • Cannot be virtual and static • Implicitly Invoked When Objects are Created or Copied • Can Have Arguments • Cannot be Explicitly Called • Multiple Constructors are Allowed • Default Constructor -- No Arguments -- Explicit or Implicit • Copy Constructor -- Explicit or Implicit

  3. Constructor – Example 1 • // date.h/* A Simple Date Class With Constructors */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(); //default date(int ip_day, int ip_month, int ip_year); date(char *ip_date); };

  4. Constructor – Example 1 • // date.cpp#include “date.h”//Default Constructor date::date() {day = 0; month = 0; year = 0; string_date = 0;} //First Constructor date::date(int ip_day, int ip_month, int ip_year){ day = ip_day; month = ip_month; year = ip_year; string_date = 0;}//Second Constructor date::date(char *ip_date){ day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date);}

  5. Constructor – Example 1 • // client.cpp#include “date.h”main() { //"default_day" is an object of "date". //Default Constructor is invoked. date default_day; // "today" is an object of "date". //First Constructor is invoked. date today(02, 02, 2004); // "string_today" is an object of "date". //Second Constructor is invoked. date string_today(“February 2, 2004"); }

  6. Constructor - Example 2 • // student.h/* A Simple Class to Represent a Student */#define size 50class Student{  private:    char *name;    char *id;    char *email;  public: Student(); //Default    Student(char *, char *, char *);};

  7. Constructor – Example 2 (cont) • // student.cpp#include “student.h”/* Member functions of the “Student" class */Student::Student(){name = new char[size]; id = new char[size]; email = new char[size];}Student::Student(char *studentName, char *studentId, char *studentEmail){name = new char[size];   strcpy(name, studentName);id = new char[size];   strcpy(id, studentId); email = new char[size];    strcpy(email, studentEmail);}

  8. Constructor – Example 2 contd…. • // client.cpp#include “student.h”/* “csStudent" is an instance of “Student" */main(){    Student csStudent1;    Student csStudent2 ("Susie Creamchese“, "123456789“, "screamch@iupui.edu");}

  9. Destructor • Special Member Function used for Clean-up -- Same Name as the Class Name with a ~ • One Single Destructor • Invoked When an Object Goes Out of Scope • Can be Explicitly Called -- Unusual • Cannot Accept Parameters and Does Not Return A Value • Cannot be Declared static, const or volatile • Can be virtual

  10. Destructor – Example 1 • // date.h/* A Simple Date Class With a Destructor */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); ~date(); //Destructor };

  11. Destructor – Example 1 • // date.cpp#include “date.h”//Constructor date::date(char *ip_date){ day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date);} //Destructor date::~date() {delete[] (string_date);}

  12. Destructor – Example 1 (cont) • // client.cpp#include “date.h”main(){ // "today" is an object of "date". Constructor is invoked. date today("June 19, 1995");}

  13. Destructor - Example 2 • // student.h/* A Simple Class to Represent a Student */#define size 50class Student{   private: char *name; char *id; char *email;   public: Student(); //Default Student(char *, char *, char *); ~Student();};

  14. Destructor – Example 2 (cont) • // student.cpp#include “student.h”/* Member functions of the “Student" class */Student::Student(){ name = new char[size]; id = new char[size]; email = new char[size];}Student::Student(char *studentName, char *studentId, char *studentEmail){ name = new char[size];    strcpy(name, studentName); id = new char[size];    strcpy(id, studentId); email = new char[size];    strcpy(email, studentEmail);}Student::~Student(){ delete [] name; delete [] id; delete [] email;}

  15. Destructor – Example 2 (cont) • // client.cpp#include “student.h”/* “csStudent" is an instance of “Student" */main(){    Student csStudent1;    Student csStudent2 ("Susie Creamchese“, "123456789“, "screamch@iupui.edu");}

  16. Acknowledgements • These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.

More Related