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

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
Abstract Data Type Fraction Example
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
1 CSC241: Object Oriented Programming Lecture No 05.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Object-Oriented Programming (OOP) Lecture No. 24.
SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Object Oriented Programming using Java - Composition
Constructors and Destructors
Examples of Classes & Objects
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Object Oriented Programming (OOP) Lecture No. 8
Programming with ANSI C ++
Abstract Data Types Polynomials CSCI 240
Object Oriented Programming using Java - Class Instance Variables
Constructors & Destructors.
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Constructor & Destructor
group work #hifiTeam
Classes Access Specifiers
Advanced Programming Basics
Negative Integer Representation
Contents Introduction to Constructor Characteristics of Constructor
Classes & Objects: Examples
Constructors Member Functions Inheritance
Classes Copy Constructors
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Pointers Call-by-Reference CSCI 230
Constructors and destructors
Constructors and Destructors
Object Oriented Programming (OOP) Lecture No. 13
Classes Static Members
CS148 Introduction to Programming II
Constructors Member Functions Inheritance
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Classes Introduction CSCI 240
Classes Class Data Members & Initializers
Classes Member Qualifiers
SPL – PS3 C++ Classes.
Presentation transcript:

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

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

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); };

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);}

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"); }

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 * ; public: Student();//Default Student(char *, char *, char *); };

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]; = new char[size];} Student::Student(char *studentName, char *studentId, char *student ){ name = new char[size]; strcpy(name, studentName); id = new char[size]; strcpy(id, studentId); = new char[size]; strcpy( , student ); }

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“, " “, }

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

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 };

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);}

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"); }

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 * ; public: Student();//Default Student(char *, char *, char *); ~Student(); };

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]; = new char[size];} Student::Student(char *studentName, char *studentId, char *student ){ name = new char[size]; strcpy(name, studentName); id = new char[size]; strcpy(id, studentId); = new char[size]; strcpy( , student );} Student::~Student(){ delete [] name; delete [] id; delete [] ; }

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“, " “, }

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