Introduction to Classes and Objects CS-2303 System Programming
31 Slides700.00 KB
Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, C-Term 2010 Introduction to Classes a nd Objects 1
Reading Deitel & Deitel, Chapter 19 CS-2303, C-Term 2010 Introduction to Classes a nd Objects 2
Classes and Objects Class Definitions and Objects Member Functions Data Members – Get and Set functions – Constructors Placing Classes in Separate Files Separating Interface from Implementation Data Validation – Ensures that data in an object is in a particular format or range. CS-2303, C-Term 2010 Introduction to Classes a nd Objects 3
C Program Structure Typical C Programs consist of:– – A function main – One or more classes Each containing data members and member functions. CS-2303, C-Term 2010 Introduction to Classes a nd Objects 4
C Gradebook Example Deitel & Deitel Fig 19.1 1 // Fig. 19.1: fig19 01.cpp 2 // Define class GradeBook with a member function displayMessage; 3 // Create a GradeBook object and call its displayMessage function. 4 #include iostream 5 using std::cout; 6 using std::endl; “using”:– add the name to the current scope 7 8 // GradeBook class definition 9 class GradeBook 10 { 11 public: 12 // function that displays a welcome message to the GradeBook user 13 void displayMessage() 14 { 15 16 cout "Welcome to the Grade Book!" endl; } // end function displayMessage 17 }; // end class GradeBook 18 19 // function main begins program execution 20 int main() 21 { 22 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 23 myGradeBook.displayMessage(); // call object's displayMessage function 24 return 0; // indicate successful termination 25 } CS-2303, // end main C-Term 2010 Welcome to the Grade Book! Introduction to Classes a nd Objects 6
C Gradebook Example 1 // Fig. 19.1: fig19 01.cpp 2 // Define class GradeBook with a member function displayMessage; 3 // Create a GradeBook object and call its displayMessage function. 4 #include iostream 5 using std::cout; 6 using std::endl; 7 8 // GradeBook class definition 9 class GradeBook Beginning of class definition for class GradeBook Beginning of class body Access specifier public; makes members available to the public 10 { 11 public: 12 // function that displays a welcome message to the GradeBook user 13 void displayMessage() 14 { 15 16 cout "Welcome to the Grade Book!" endl; Member function displayMessage returns nothing } // end function displayMessage 17 }; // end class GradeBook End of class body 18 19 // function main begins program execution 20 int main() 21 { Use dot operator to call GradeBook’s member function 22 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 23 myGradeBook.displayMessage(); // call object's displayMessage function 24 return 0; // indicate successful termination 25 } CS-2303, // end main C-Term 2010 Introduction to Classes a nd Objects Systems Programming: Welcome to the Grade Book! Classes and Objects 7 7
C Gradebook Example 1 // Fig. 19.1: fig19 01.cpp 2 // Define class GradeBook with a member function displayMessage; 3 // Create a GradeBook object and call its displayMessage function. 4 #include iostream 5 using std::cout; 6 using std::endl; 7 8 // GradeBook class definition 9 class GradeBook 10 { 11 public: 12 // function that displays a welcome message to the GradeBook user 13 void displayMessage() 14 { 15 16 17 18 19 20 21 ss a l c his t in } // end function displayMessage f a o m t of jec }; // end class GradeBook e b l o b ri a an a g v n i c r i // function main begins program execution ecla utomat D int main() na a I.e., allocated on The Stack s a { cout "Welcome to the Grade Book!" endl; 22 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 23 myGradeBook.displayMessage(); // call object's displayMessage function 24 return 0; // indicate successful termination 25 } CS-2303, // end main C-Term 2010 Welcome to the Grade Book! Introduction to Classes a nd Objects 8
Member Functions with Parameters 1 // Fig. 19.3: fig19 03.cpp 2 3 // Define class GradeBook with a member function that takes a parameter; // Create a GradeBook object and call its displayMessage function. 4 #include iostream 5 6 using std::cout; using std::cin; 7 using std::endl; 8 9 #include string // program uses C standard string class Include string class definition 10 using std::string; 11 using std::getline; 12 Member function parameter 13 // GradeBook class definition 14 class GradeBook 15 { 16 public: 17 // function that displays a welcome message to the GradeBook user 18 19 void displayMessage( string courseName ) { 20 21 22 cout "Welcome to the grade book for\n" courseName "!" endl; } // end function displayMessage Use the function parameter as a variable 23 }; // end class GradeBook 24 25 // function main begins program execution 26 int main() 27 { 28 string nameOfCourse; // string of characters to store the course name 29 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 30 CS-2303, C-Term 2010 Introduction to Classes a nd Objects 9
Member Functions with Parameters (cont.) 31 // prompt for and input course name 32 cout "Please enter the course name:" endl; 33 getline( cin, nameOfCourse ); // read a course name with blanks 34 cout endl; // output a blank line 35 36 // call myGradeBook's displayMessage function 37 // and pass nameOfCourse as an argument 38 myGradeBook.displayMessage( nameOfCourse ); 39 return 0; // indicate successful termination 40 } // end main Please enter the course name: CS101 Introduction to C Programming Passing an argument to the member function Welcome to the grade book for CS101 Introduction to C Programming! CS-2303, C-Term 2010 Introduction to Classes a nd Objects 10
Useful Tidbits A string – Represents a string of characters. – An object of C Standard Library class std::string Defined in header file string . – Not a character array as in C. Library function getline – Used to retrieve input until newline is encountered – Example getline( cin, nameOfCourse ); – Inputs a line from standard input into string object nameOfCourse. Defined in header file iostream . CS-2303, C-Term 2010 Introduction to Classes a nd Objects 11
Data Members of a Class Declared in the body of the class May be public or private Exist throughout the life of the object. Stored in class object. Each object has its own copy. May be objects of any type CS-2303, C-Term 2010 Introduction to Classes a nd Objects 12
Access-specifier private Makes any member accessible only to member functions of the class. May be applied to data members and member functions Default access for class members Encourages “information hiding” CS-2303, C-Term 2010 Introduction to Classes a nd Objects 13
Public and Private Members 1 2 // Fig. 19.5: fig19 05.cpp // Define class GradeBook that contains a courseName data member 3 4 // and member functions to set and get its value; // Create and manipulate a GradeBook object with these functions. 5 6 #include iostream using std::cout; 7 8 9 using std::cin; using std::endl; 10 #include string // program uses C standard string class 11 using std::string; 12 using std::getline; 13 14 // GradeBook class definition 15 class GradeBook 16 { 17 public: 18 // function that sets the course name 19 void setCourseName( string name ) 20 { 21 courseName name; // store the course name in the object 22 } // end function setCourseName 23 24 // function that gets the course name 25 string getCourseName() 26 { 27 return courseName; // return the object's courseName 28 } // end function getCourseName 29 CS-2303, C-Term 2010 Introduction to Classes a nd Objects set function modifies private data get function accesses private data 14
Public and Private Members (continued) 30 // function that displays a welcome message 31 void displayMessage() 32 { 33 // this statement calls getCourseName to get the 34 // name of the course this GradeBook represents 35 cout "Welcome to the grade book for\n" getCourseName() "!" endl; 36 37 } // end function displayMessage 38 private: 39 string courseName; // course name for this GradeBook 40 }; // end class GradeBook 41 42 // function main begins program execution 43 int main() 44 { Use set and get functions, even within the class private members accessible only to member functions of the class 45 string nameOfCourse; // string of characters to store the course name 46 GradeBook myGradeBook; // create a GradeBook object named myGradeBook 47 48 // display initial value of courseName 49 cout "Initial course name is: " myGradeBook.getCourseName() 50 default constructor Accessing private data outside class definition endl; 51 CS-2303, C-Term 2010 Introduction to Classes a nd Objects 15
Public and Private Members (continued) 52 // prompt for, input and set course name 53 cout "\nPlease enter the course name:" endl; 54 getline( cin, nameOfCourse ); // read a course name with blanks 55 myGradeBook.setCourseName( nameOfCourse ); // set the course name 56 57 cout endl; // outputs a blank line 58 myGradeBook.displayMessage(); // display message with new course name 59 return 0; // indicate successful termination 60 } // end main Modifying private data outside class definition Initial course name is: Please enter the course name: CS101 Introduction to C Programming Welcome to the grade book for CS101 Introduction to C Programming! CS-2303, C-Term 2010 default setting from constructor is an empty string!! Introduction to Classes a nd Objects 16
Software Engineering Observation 19.1 As a rule of thumb, data members should be declared private Member functions should be declared public. – Except member functions that are accessed only by other member functions of the class. Often useful to have get and set functions – To access private members in controlled ways CS-2303, C-Term 2010 Introduction to Classes a nd Objects 17
Constructors and Destructors Constructor:– a function used to initialize the data of an object of a class – Same name as class itself – Cannot return anything, not even void – A class may define more than one constructor With different parameter lists Default constructor has no parameters Compiler provides one if you do not! Called automatically – When class object is declared as automatic variable – By new operator Compiler’s default simply calls constructors of data members of the class. CS-2303, C-Term 2010 Introduction to Classes a nd Objects 18
Constructors and Destructors (continued) Destructor:– a function used to clean up an object of a class prior to deleting that object – Class name preceeded by ' ' Compiler provides one – No parameters, no result if you do not! Called automatically – When function exits scope of automatic class object default simply calls destructors of – By deleteCompiler’s operator data members of the class. CS-2303, C-Term 2010 Introduction to Classes a nd Objects 19
Constructors and Destructors (continued) Constructors – Similar to Java Destructors – No counterpart in Java Purpose of Destructors Free dynamic storage pointed to only by members of object Reduce reference count when object disappears Safely close things – e.g., files CS-2303, C-Term 2010 Introduction to Classes a nd Objects 20
Constructor Example 1 // Fig. 19.7: fig19 07.cpp 2 // Instantiating multiple objects of the GradeBook class and using 3 // the GradeBook constructor to specify the course name 4 // when each GradeBook object is created. 5 #include iostream 6 using std::cout; 7 using std::endl; 8 9 #include string // program uses C standard string class 10 using std::string; Constructor has same name as class and no return type 11 12 // GradeBook class definition 13 class GradeBook 14 { 15 public: 16 // constructor initializes courseName with string supplied as argument 17 GradeBook( string name ) 18 { setCourseName( name ); // call set function to initialize courseName 19 20 } // end GradeBook constructor 21 22 // function to set the course name 23 void setCourseName( string name ) 24 { 25 26 Initialize data member courseName name; // store the course name in the object } // end function setCourseName 27 CS-2303, C-Term 2010 Introduction to Classes a nd Objects 21
Constructor Example 28 // function to get the course name 29 string getCourseName() 30 { 31 32 return courseName; // return object's courseName } // end function getCourseName 33 34 35 36 37 38 39 40 41 42 ject ry b o ss a mo l e c { m s thi cated o // call getCourseName to get the courseName s d allo ee n y cout "Welcome to the grade book for\n" getCourseName() l l r ica c to u m r t "!" endl; a es yn d D e } // end function displayMessage fre can private: // display a welcome message to the GradeBook user void displayMessage() string courseName; // course name for this GradeBook 43 }; // end class GradeBook 44 CS-2303, C-Term 2010 Introduction to Classes a nd Objects 22
Constructor Example 45 // function main begins program execution 46 int main() 47 { 48 // create two GradeBook objects 49 GradeBook gradeBook1( "CS101 Introduction to C Programming" ); 50 GradeBook gradeBook2( "CS102 Data Structures in C " ); 51 52 // display initial value of courseName for each GradeBook 53 cout "gradeBook1 created for course: " gradeBook1.getCourseName() 54 "\ngradeBook2 created for course: " gradeBook2.getCourseName() 55 endl; 56 return 0; // indicate successful termination 57 } // end main Creating objects implicitly calls the constructor gradeBook1 created for course: CS101 Introduction to C Programming gradeBook2 created for course: CS102 Data Structures in C CS-2303, C-Term 2010 Introduction to Classes a nd Objects 23
Class in a Separate Header File for Reusability .cpp files for source-code implemenations – Class implementations – Main programs – Test programs – Header files – Separate files in which class definitions are placed. – Allow compiler to recognize the classes when used elsewhere. – Generally have .h filename extensions Driver file – A program used to test software (such as classes). – Contains a main function so it can be executed. CS-2303, C-Term 2010 Introduction to Classes a nd Objects 24
Interfaces versus Implementation Interface – Describes what services a class’s clients can use and how to request those services. without revealing how the class carries out the services. a class definition listing only public member function prototypes. – A class’s interface consists of the class’s public member functions (services). Defined in class header file (.h) CS-2303, C-Term 2010 Introduction to Classes a nd Objects 25
Interfaces versus Implementation Implementation of member functions – In a separate source-code file for a class Use binary scope resolution operator (::) to tie each member function to the class definition. – Implementation details are hidden. Client code does not need to know the implementation. CS-2303, C-Term 2010 Introduction to Classes a nd Objects 26
Interfaces versus Implementation (Example) 1 // Fig. 19.11: GradeBook.h 2 // GradeBook class definition. This file presents GradeBook's public 3 // interface without revealing the implementations of GradeBook's member 4 // functions, which are defined in GradeBook.cpp. 5 #include string // class GradeBook uses C standard string class 6 using std::string; 7 8 // GradeBook class definition 9 class GradeBook Interface contains data members and member function prototypes 10 { 11 public: 12 GradeBook( string ); // constructor that initializes courseName 13 void setCourseName( string ); // function that sets the course name 14 string getCourseName(); // function that gets the course name 15 void displayMessage(); // function that displays a welcome message 16 private: 17 string courseName; // course name for this GradeBook 18 }; // end class GradeBook CS-2303, C-Term 2010 Introduction to Classes a nd Objects 27
Interfaces versus Implementation (continued) 1 // Fig. 19.12: GradeBook.cpp 2 // GradeBook member-function definitions. This file contains 3 // implementations of the member functions prototyped in GradeBook.h. 4 #include iostream 5 using std::cout; 6 using std::endl; GradeBook implementation is placed in a separate source-code file 7 8 #include "GradeBook.h" // include definition of class GradeBook 9 Include the header file to access the class name GradeBook 10 // constructor initializes courseName with string supplied as argument 11 GradeBook::GradeBook( string name ) 12 { 13 setCourseName( name ); // call set function to initialize courseName 14 } // end GradeBook constructor Binary scope resolution operator ties a function to its class 15 16 // function to set the course name 17 void GradeBook::setCourseName( string name ) 18 { 19 courseName name; // store the course name in the object 20 } // end function setCourseName 21 CS-2303, C-Term 2010 Introduction to Classes a nd Objects 28
Interfaces versus Implementation (continued) 22 // function to get the course name 23 string GradeBook::getCourseName() 24 { return courseName; // return object's courseName 25 26 } // end function getCourseName 27 28 // display a welcome message to the GradeBook user 29 void GradeBook::displayMessage() 30 { 31 // call getCourseName to get the courseName 32 cout "Welcome to the grade book for\n" getCourseName() 33 "!" endl; 34 } // end function displayMessage CS-2303, C-Term 2010 Introduction to Classes a nd Objects 29
Client of the Interface 1 // Fig. 19.13: fig19 13.cpp 2 // GradeBook class demonstration after separating 3 // its interface from its implementation. 4 #include iostream 5 using std::cout; 6 using std::endl; 7 8 #include "GradeBook.h" // include definition of class GradeBook 9 10 // function main begins program execution 11 int main() 12 { 13 // create two GradeBook objects 14 GradeBook gradeBook1( "CS101 Introduction to C Programming" ); 15 GradeBook gradeBook2( "CS102 Data Structures in C " ); 16 17 // display initial value of courseName for each GradeBook 18 cout "gradeBook1 created for course: " gradeBook1.getCourseName() 19 "\ngradeBook2 created for course: " gradeBook2.getCourseName() 20 endl; 21 return 0; // indicate successful termination 22 } // end main gradeBook1 created for course: CS101 Introduction to C Programming gradeBook2 created for course: CS102 Data Structures in C CS-2303, C-Term 2010 Introduction to Classes a nd Objects 30
Summary Introduced class definitions and objects – Public versus private access into class. Syntax for member functions Syntax data members – Get and Set functions – Constructors & Destructors Placing classes in separate files Separating interface from implementation CS-2303, C-Term 2010 Introduction to Classes a nd Objects 31
Questions? Reading:– Deitel & Deitel, Chapter 19 CS-2303, C-Term 2010 Introduction to Classes a nd Objects 32