Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.

Similar presentations


Presentation on theme: "Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer."— Presentation transcript:

1 Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu CSCI 240

2 Dale Roberts 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 Dale Roberts 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 Dale Roberts 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 Dale Roberts 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 Dale Roberts 6 Constructor - Example 2 // student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{ private: char *name; char *id; char *email; public: Student();//Default Student(char *, char *, char *); };

7 Dale Roberts 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 Dale Roberts 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 Dale Roberts 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 Dale Roberts 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 Dale Roberts 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 Dale Roberts 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 Dale Roberts 13 Destructor - Example 2 // student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{ private: char *name; char *id; char *email; public: Student();//Default Student(char *, char *, char *); ~Student(); };

14 Dale Roberts 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 Dale Roberts 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 Dale Roberts 16 Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.


Download ppt "Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer."

Similar presentations


Ads by Google