DOC PREVIEW
IUPUI CS 265 - Classes

This preview shows page 1-2-3-4-25-26-27-51-52-53-54 out of 54 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 54 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ClassesClassStructure of a ClassSyntax of a ClassAn Example of a ClassSlide 6Slide 7Class - Example 2Class – Example 2 (cont)Class – Example 2 contd….ConstructorConstructor – Example 1Slide 13Slide 14Constructor - Example 2Constructor – Example 2 contd….Slide 17DestructorDestructor – Example 1Slide 20Destructor – Example 1 (cont)Destructor - Example 2Destructor – Example 2 contd….Slide 24Accessing RightsAccessing Rights – Example 1Accessing Rights – Example 1 (cont)Slide 28Accessing Rights - Example 2Accessing Rights – Example 2 ….Slide 31Copy ConstructorCopy Constructor – Example 1Copy Constructor – Example 1 ….Slide 35Copy Constructor – Example 2Copy Constructor – Example 2 ….Slide 38Copy Constructor - Example 3Copy Constructor – Example 3 ….Slide 41Copy Constructor - Example 4Copy Constructor – Example 4 ….Slide 44Static MembersStatic Data MembersStatic Data Members -- ExampleStatic Data Members – Example (cont)Slide 49Static Member FunctionsStatic Member Functions -- ExampleStatic Member Functions – Example (cont)Slide 53Acknowledgements1Dale RobertsClassesDepartment of Computer and Information Science,School of Science, IUPUIDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected] 2652Dale RobertsClassClassAn IMPORTANT FACET OF OOD and C++ An IMPORTANT FACET OF OOD and C++ A Set of Homogeneous Objects A Set of Homogeneous Objects An Abstract Data Type (ADT) An Abstract Data Type (ADT) A Collection of Data Items or Functions or Both A Collection of Data Items or Functions or Both An User Defined Type – describes a new data An User Defined Type – describes a new data typetypeClass Definition is a Type Declaration -- No Class Definition is a Type Declaration -- No Space is AllocatedSpace is Allocated3Dale RobertsStructure of a ClassStructure of a ClassData Members Data Members Member Functions Member Functions Accessing Rights -- public, private and Accessing Rights -- public, private and protectedprotectedData members are variables of either Data members are variables of either fundamental data types or user defined data fundamental data types or user defined data types. These are private by default. types. These are private by default. Member functions are public by default and Member functions are public by default and provide the interface to the data members. Thus, provide the interface to the data members. Thus, if any other object or function needs to access if any other object or function needs to access the data members, it should do so through the the data members, it should do so through the member functions of the class.member functions of the class.4Dale RobertsSyntax of a ClassSyntax of a Classclass Class_Name{ class Class_Name{ private: //Default and Optional private: //Default and Optional data_member_specification_1; data_member_specification_1; : : data_member_specification_n; data_member_specification_n;public:public: data_member_specification_n+1; data_member_specification_n+1; : : data_member_specification_2n; data_member_specification_2n;}; //Don't forget the semicolon after the closing brace. }; //Don't forget the semicolon after the closing brace. Each class declaration starts with the keyword class followed by the Each class declaration starts with the keyword class followed by the name of the class. name of the class. The definition of the class falls between the open and closed braces The definition of the class falls between the open and closed braces that follow the class name.that follow the class name.5Dale RobertsAn Example of a ClassAn Example of a Class// date.h// date.h/* A Simple Class to Represent a Date */ /* A Simple Class to Represent a Date */ #define size 50 #define size 50 class date{ class date{ private: private: int day, month, year; int day, month, year; char *string_date; char *string_date; public: public: void set_date(int ip_day,int void set_date(int ip_day,int ip_month,int ip_year); ip_month,int ip_year); void set_string_date(char *ip_date); void set_string_date(char *ip_date); }; };6Dale RobertsAn Example of a ClassAn Example of a Class// date.cpp// date.cpp#include “date.h”#include “date.h”/* Member functions of the "date" class */ /* Member functions of the "date" class */ void date::set_date(int ip_day,int void date::set_date(int ip_day,int ip_month,int ip_year) ip_month,int ip_year) {day = ip_day; month = ip_month; year = {day = ip_day; month = ip_month; year = ip_year;} ip_year;} void date::set_string_date(char *ip_date) void date::set_string_date(char *ip_date) {string_date = new char[size]; {string_date = new char[size]; strcpy(string_date, ip_date);}strcpy(string_date, ip_date);}7Dale RobertsAn Example of a ClassAn Example of a Class// client.cpp// client.cpp#include “date.h”#include “date.h”/* "today" and "yesterday" are instances of /* "today" and "yesterday" are instances of "date" */ "date" */ main(){ main(){ date today, yesterday; date today, yesterday; today.set_date(19, 06, 1995); today.set_date(19, 06, 1995); today.set_string_date("June 19, 1995"); today.set_string_date("June 19, 1995"); yesterday.set_date(18, 06, 1995); yesterday.set_date(18, 06, 1995); yesterday.set_string_date("June 18, 1995"); yesterday.set_string_date("June 18, 1995");}}Compile with Compile with % % g++ client.cpp date.cppg++ client.cpp date.cppLater, we’ll learn to write a make file.Later, we’ll learn to write a make file.8Dale RobertsClass - Example 2Class - Example 2// student.h// student.h/* A Simple Class to Represent a Student *//* A Simple Class to Represent a Student */#define size 50#define size 50class Student{class Student{ private: private:char *name;char *name;char *id;char *id;char *email;char *email; public: public:void setName(char *studentName);void setName(char *studentName);void setId(char *studentId);void setId(char *studentId);void setEmail(char *studentEmail);void setEmail(char *studentEmail);};};9Dale RobertsClass – Example 2 (cont)Class – Example 2 (cont)// student.cpp// student.cpp#include “student.h”#include “student.h”/* Member functions of the “Student" class *//* Member functions of the “Student" class */void Student::setName(char *studentName)void Student::setName(char *studentName){name = new char[size]; strcpy(name, studentName);}{name = new char[size]; strcpy(name, studentName);} void


View Full Document

IUPUI CS 265 - Classes

Download Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?