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

Slides:



Advertisements
Similar presentations
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.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
Chapter 10 Introduction to Classes
 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. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
More C++ Features True object initialisation
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 CSC241: Object Oriented Programming Lecture No 02.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
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.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Learning Objectives Pointers as dada members
Submission Example May 14, 2018
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Object Oriented Programming (OOP) Lecture No. 8
Constructors & Destructors.
CS410 – Software Engineering Lecture #11: C++ Basics IV
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
Classes Access Specifiers
Fraction Abstract Data Type
Contents Introduction to Constructor Characteristics of Constructor
Classes Copy Constructors
Constructors and Destructors
Classes Static Members
CS148 Introduction to Programming II
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Classes Introduction CSCI 240
CS148 Introduction to Programming II
9-10 Classes: A Deeper Look.
Object Oriented Programming (OOP) Lecture No. 12
Classes Class Data Members & Initializers
(4 – 2) Introduction to Classes in C++
Classes Member Qualifiers
SPL – PS3 C++ Classes.
Presentation transcript:

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

Class An IMPORTANT FACET OF OOD and C++ A Set of Homogeneous Objects An Abstract Data Type (ADT) A Collection of Data Items or Functions or Both An User Defined Type – describes a new data type Class Definition is a Type Declaration -- No Space is Allocated

Structure of a Class Data Members Member Functions Accessing Rights -- public, private and protected Data members are variables of either fundamental data types or user defined data types. Member provide the interface to the data members and other procedures and function. Thus, if any other object or function needs to access the data members, it should do so through the member functions of the class. The default member accessibility is private, meaning that only class members can access the data member or member function. A client uses the public interface meaning it accessed public members. In order to assure data stability of the class, it is recommended that only member functions be public, and the data members are accessed only via get and set functions and procedures.

Syntax of a Class class Class_Name{ private: //Default and Optional data_member_specification_1; : data_member_specification_n; public: data_member_specification_n+1; : data_member_specification_n+m; }; //Don't forget the semicolon after the closing brace. Each class declaration starts with the keyword class followed by the name of the class.  The definition of the class falls between the open and closed braces that follow the class name.

An Example of a Class // date.h /* A Simple Class to Represent a Date */ #define size 50 class date{ private: int day, month, year; char *string_date; public: void set_date( int ip_day,int ip_month,int ip_year); void set_string_date(char *ip_date); };

An Example of a Class // date.cpp #include “date.h” /* Member functions of the "date" class */ void date::set_date(int ip_day,int ip_month,int ip_year) {day = ip_day; month = ip_month; year = ip_year;} void date::set_string_date(char *ip_date) {string_date = new char[size]; strcpy(string_date, ip_date);}

An Example of a Class // client.cpp #include “date.h” /* "today" and "yesterday" are instances of "date" */ main(){ date today, yesterday; today.set_date(19, 06, 1995); today.set_string_date("June 19, 1995"); yesterday.set_date(18, 06, 1995); yesterday.set_string_date("June 18, 1995"); } Compile with % g++ client.cpp date.cpp Later, we’ll learn to write a make file.

Class - Example 2 // student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{   private: char *name; char *id; char *email;   public: void setName(char *studentName); void setId(char *studentId); void setEmail(char *studentEmail); };

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

Class – Example 2 contd…. // client.cpp #include “student.h” /* “csStudent" is an instance of “Student" */ main(){     Student csStudent; // Allocates space     csStudent.setName("Susie Creamchese");     csStudent.setId("123456789");     csStudent.setEmail("screamch@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 50 class Student{   private:     char *name;     char *id;     char *email;   public: Student(); //Default     Student(char *, char *, char *); };

Constructor – Example 2 contd…. // 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 // date.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 contd…. // date.cpp #include “date.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 contd…. // client.cpp #include “student.h” /* “csStudent" is an instance of “Student" */ main(){     Student csStudent1;     Student csStudent2 ("Susie Creamchese“, "123456789“, "screamch@iupui.edu"); }

Accessing Rights Private: Member Functions of the Class and its Friends Protected: Member Functions and Derived Classes Public: Entire World Thumb Rule -- Data Members are Private and Member Functions are Public

Accessing Rights – Example 1 // date.h /* A Simple Date Class With a Proper Accessing */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); //Constructor ~date(); //Destructor void print_date(); //Member Function };

Accessing Rights – Example 1 (cont) // date.cpp #include “date.h” date::date(char *ip_date){ day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date);} date::~date() {delete[] (string_date);} void date::print_date() {cout << "Date is: " << string_date << endl;}

Accessing Rights – Example 1 (cont) // client.cpp #include “date.h” main(){ date today("June 19, 1995"); // ERROR cout << "Date is: " << today.string_date << endl; today.print_date(); //CORRECT }

Accessing Rights - 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(char *, char *, char *); ~Student(); void printData(); };

Accessing Rights – Example 2 …. // student.cpp #include student.h /* Member functions of the “Student" class */ 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; } void Student::printData(){ cout << “Name: ” << name << endl << “ID : ” << id << endl << “Email : ” << email << endl;}

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

Copy Constructor Constructor with a Reference to THAT CLASS as an ARGUMENT X(X &) Format Initialization of a New Object by Another Object of that Class An Object is Passed by Value to a Function An Object is Returned from a Function Shallow, Intermediate and Deep Copying -- Involvement of Pointers Implicit Copy Constructor is Created by the Compiler to Perform Memberwise Initialization

Copy Constructor – Example 1 // date.h #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); //Constructor ~date(); //Destructor void print_date(); //Member Function };

Copy Constructor – Example 1 …. // date.cpp #include “date.h” Date::date() //default constructor {day = 0; month = 0; year = 0; string_date = 0;} date::date(char *ip_date){ //Constructor day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date); } //Shallow Copy Constructor date(const date &ip_date) {string_date = ip_date.string_date;} //Destructor date::~date() {delete[] (string_date);}

Copy Constructor – Example 1 …. // client.cpp #include “date.h” main() { date today("June 21, 1995"); //Use of shallow copy constructor date extra_day = today; } “Shallow copy” simply means that all the data member values are copied.

Copy Constructor – Example 2 // date.h #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); //Constructor ~date(); //Destructor void print_date(); //Member Function };

Copy Constructor – Example 2 …. // date.cpp #include “date.h” Date::date() //default constructor {day = 0; month = 0; year = 0; string_date = 0;} date::date(char *ip_date){ //Constructor day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date); } //Deep Copy Constructor date(const date &ip_date) { string_date = new char[size]; strcpy(string_date, ip_date.string_date); } //Destructor date::~date() {delete[] (string_date);}

Copy Constructor – Example 2 …. // client.cpp #include “date.h” main() { date today("June 21, 1995"); //Deep copy constructor date extra_day = today; } “Deep copy” means that objects pointed to by data members are also copied. Deep copying may be required whenever you have data members that are pointers.

Copy Constructor - Example 3 // student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{   private: char *name; char *id; char *email;   public: Student(char *, char *, char *); Student(Student &); //Copy constructor ~Student(); };

Copy Constructor – Example 3 …. // student.cpp #include “student.h” /* Member functions of the “Student" class */ 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;} //Shallow copy constructor Student::Student (Student &s){ name = s.name; id = s.id; email = s.email; }

Copy Constructor – Example 3 …. // client.cpp #include “student.h” /* “csStudent" is an instance of “Student" */ main(){     Student csStudent1("Susie Creamchese“, "123456789“, "screamch@iupui.edu");     Student csStudent2 = csStudent1; } What happens when csStudent2 changes its name?

Copy Constructor - Example 4 // student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{   private: char *name; char *id; char *email;   public: Student(char *, char *, char *); ~Student(); Student(Student &); //Copy constructor };

Copy Constructor – Example 4 …. // student.cpp #include “student.h” /* Member functions of the “Student" class */ 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;} // Deep copy constructor Student::Student(Student &s){ name = new char[size]; strcpy(name, s.name); id = new char[size]; strcpy(id, s.id); email = new char[size]; strcpy(email, s.email);}  

Copy Constructor – Example 4 …. // client.cpp #include “student.h” main(){     Student csStudent1 (“Susie Creamchese”, “123456789”, “screamch@iupui.edu”);     Student csStudent2 = csStudent1; } What happens when csStudent2 changes its name?

Static Members Data and Functions Can be static Exists Independently of Any Instantiated Objects

Static Data Members COMMON to All the Objects of that Class Exist Even When NO Class Objects Exist Definition for static Data Members is a Declaration in header file Initialization ONLY by an Initializer or an Assignment Statement inside a static member function Accessing -- class_name::data_name or object_name.data_name

Static Data Members -- Example // student.h #define size 20 class student{ char *name, *ss; static char *instructor; public: student(char *, char *); ~student(); };

Static Data Members – Example (cont) // student.cpp #include “student.h” #include <iostream> student(char *ip_name, char *ip_ss){ name = new char[size]; ss = new char[size]; strcpy(name, ip_name); strcpy(ss, ip_ss);} void print(){ cout << "Name: " << name << endl; cout << "SS: " << ss << endl; cout << "Instructor: " << instructor << endl;} }; /* Definition and Initialization of Static member */ char *student::instructor = "Henry";

Static Data Members -- Example // client.cpp #include “student.h” main() { student s1("John", "012345678"); student s2("Tom", "999999999"); s1.print(); s2.print(); } Name: John SS: 012345678 Instructor: Henry Name: TOM SS: 999999999 Instructor: Henry  

Static Member Functions COMMON to All Objects of the Class Created by the Class Definition -- Exist Even when NO Class Objects Exist Can Only Access Static Data Members of the Class or other static member functions Accessing -- class_name::function_name() or object_name.function_name()

Static Member Functions -- Example // student.h #define size 20 class student{ char *name; char *ss; static char *instructor; public: student(char *ip_name, char *ip_ss); static void print_instructor(); };

Static Member Functions – Example (cont) // student.cpp #include “student.h” #include <iostream> student::student(char *ip_name, char *ip_ss){ name = new char[size]; ss = new char[size]; strcpy(name, ip_name); strcpy(ss, ip_ss);} static void student::print_instructor() { cout << "Instructor: " << instructor << endl;} /* Definition and Initialization of Static member */ char *student::instructor = "Henry";

Static Member Functions – Example (cont) // client.cpp #include “student.h” main() { student::print_instructor(); } Output will be: Instructor: Henry Notice that no instance of student is required.

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