Download presentation
Presentation is loading. Please wait.
Published byΈρις Δραγούμης Modified over 6 years ago
1
Department of Computer and Information Science, School of Science, IUPUI
CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
2
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
3
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.
4
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.
5
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); };
6
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);}
7
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.
8
Class - Example 2 // student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{ private: char *name; char *id; char * ; public: void setName(char *studentName); void setId(char *studentId); void set (char *student ); };
9
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::set (char *student ) { = new char[size]; strcpy( , student );}
10
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(" "); }
11
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
12
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); };
13
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);}
14
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"); }
15
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 *); };
16
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]; = 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 ); }
17
Constructor – Example 2 contd….
// client.cpp #include “student.h” /* “csStudent" is an instance of “Student" */ main(){ Student csStudent1; Student csStudent2 ("Susie Creamchese“, " “, }
18
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
19
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 };
20
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);}
21
Destructor – Example 1 (cont)
// client.cpp #include “date.h” main(){ // "today" is an object of "date". Constructor is invoked date today("June 19, 1995"); }
22
Destructor - Example 2 // date.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(); };
23
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]; = 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 [] ; }
24
Destructor – Example 2 contd….
// client.cpp #include “student.h” /* “csStudent" is an instance of “Student" */ main(){ Student csStudent1; Student csStudent2 ("Susie Creamchese“, " “, }
25
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
26
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 };
27
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;}
28
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 }
29
Accessing Rights - Example 2
// student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{ private: char *name; char *id; char * ; public: Student(char *, char *, char *); ~Student(); void printData(); };
30
Accessing Rights – Example 2 ….
// student.cpp #include student.h /* Member functions of the “Student" class */ 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 [] ; } void Student::printData(){ cout << “Name: ” << name << endl << “ID : ” << id << endl << “ ” << << endl;}
31
Accessing Rights – Example 2 ….
// client.cpp #include “student.h” /* “csStudent" is an instances of “Student" */ main(){ Student csStudent1; Student csStudent2 ("Susie Creamchese“, " “, }
32
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
33
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 };
34
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);}
35
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.
36
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 };
37
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);}
38
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.
39
Copy Constructor - Example 3
// student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{ private: char *name; char *id; char * ; public: Student(char *, char *, char *); Student(Student &); //Copy constructor ~Student(); };
40
Copy Constructor – Example 3 ….
// student.cpp #include “student.h” /* Member functions of the “Student" class */ 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 [] ;} //Shallow copy constructor Student::Student (Student &s){ name = s.name; id = s.id; = s. ; }
41
Copy Constructor – Example 3 ….
// client.cpp #include “student.h” /* “csStudent" is an instance of “Student" */ main(){ Student csStudent1("Susie Creamchese“, " “, Student csStudent2 = csStudent1; } What happens when csStudent2 changes its name?
42
Copy Constructor - Example 4
// student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{ private: char *name; char *id; char * ; public: Student(char *, char *, char *); ~Student(); Student(Student &); //Copy constructor };
43
Copy Constructor – Example 4 ….
// student.cpp #include “student.h” /* Member functions of the “Student" class */ 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 [] ;} // Deep copy constructor Student::Student(Student &s){ name = new char[size]; strcpy(name, s.name); id = new char[size]; strcpy(id, s.id); = new char[size]; strcpy( , s. );}
44
Copy Constructor – Example 4 ….
// client.cpp #include “student.h” main(){ Student csStudent1 (“Susie Creamchese”, “ ”, Student csStudent2 = csStudent1; } What happens when csStudent2 changes its name?
45
Static Members Data and Functions Can be static
Exists Independently of Any Instantiated Objects
46
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
47
Static Data Members -- Example
// student.h #define size 20 class student{ char *name, *ss; static char *instructor; public: student(char *, char *); ~student(); };
48
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";
49
Static Data Members -- Example
// client.cpp #include “student.h” main() { student s1("John", " "); student s2("Tom", " "); s1.print(); s2.print(); } Name: John SS: Instructor: Henry Name: TOM SS: Instructor: Henry
50
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()
51
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(); };
52
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";
53
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.
54
Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.