Download Constructors and Destructors - Advanced Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
Classes
Constructors & Destructors
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.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 (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….
#include “student.h”
/* “csStudent" is an instance of “Student" */
main(){
Student csStudent1;
Student csStudent2 ("Susie Creamchese“,
"screamch@iupui.edu");
Destructor – Example 1
/* 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
#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 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(); };
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; }