160 likes | 187 Views
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
E N D
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
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
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); };
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);}
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"); }
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 *);};
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);}
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");}
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
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 };
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);}
Destructor – Example 1 (cont) • // client.cpp#include “date.h”main(){ // "today" is an object of "date". Constructor is invoked. date today("June 19, 1995");}
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();};
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;}
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");}
Acknowledgements • These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.