1 Introduction to Classes and Objects Outline Introduction

1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data Members Defining a class with a member function Defining a member function with a parameter Data members, set and get functions Initializing objects with Constructors Placing a class in a separate file for reusability Separating interface from implementation 2003 Prentice Hall, Inc. All rights reserved.

2 Introduction • previous chapter: all executable statements were located in function main # include <filename> // preprocessor directives using std: : cout; … int main() { // variable declarations ……; // statements ……; Typically, any C++ program consists of a function main and one or more classes return 0; Each class contains data members and } member functions 2003 Prentice Hall, Inc. All rights reserved.

3 Introduction • Object-oriented programming (OOP) – Encapsulates data (attributes) and functions (behavior) into packages called classes • Information hiding – Class objects communicate across well-defined interfaces – Implementation details hidden within classes themselves • User-defined (programmer-defined) types: classes – Data (data members) – Functions (member functions or methods defined for classes) – Similar to blueprints – reusable – Class instance: object 2003 Prentice Hall, Inc. All rights reserved.

Classes, Objects, Member Functions and Data Members • Classes (e. g. , engineering drawing for a car) – Model objects (e. g. , cars) • Attributes (data members, e. g. , car’s color, nb. of doors, etc. ) • Behaviors (member functions) – Defined using keyword class – Member functions • Methods (e. g. , accelerating the car, slowing down, etc. ) • Invoked in response to messages and perform corresponding tasks (when you press the accelerator pedal, car accelerates) – Note: you as a user/driver, are not concerned of the complex mechanism that actually makes the car goes faster • Accelerator pedal is the interface you use 2003 Prentice Hall, Inc. All rights reserved. 4

Classes, Objects, Member Functions and Data Members • An object: – must be created (from a class) before a program can perform the tasks the class describes (therefore the name OOP). • E. g. , you cannot drive an engineering drawing of a car (i. e. , class), you can however drive a car (the object) – Many objects can be built from one class • e. g. : many cars (objects) can be created from the same engineering drawing (class)! • Member access specifiers – public: • Accessible wherever object of class in scope – private: • Accessible only to member functions of class 2003 Prentice Hall, Inc. All rights reserved. 5

Classes, Objects, Member Functions and Data Members • e. g. , a class represents a bank account – Member function to deposit money – Member function to withdraw money – Member function to view balance • Many objects (accounts) can be created from the class bank account • Every object maintains its own attributes – Attributes are specified by the class’s data members – Example: each bank account object knows the balance in the account it represents, and not the balances of other accounts in the bank 2003 Prentice Hall, Inc. All rights reserved. 6

7 Defining a class with a member function 1 2 3 4 5 6 // Fig. 3. 1: fig 03_01. cpp // define class Grade. Book with a member function display. Message. // create a Grade. Book object and call its display. Message function. #include <iostream> using std: : cout; using std: : cin; Class Grade. Book (a grade book used by instructor) that maintains student test 7 scores 8 // Grade. Book Class definition 9 class Grade. Book define a class 10 { 11 public: define member function 12 // Function that displays a welcome message to the Grade. Book user 13 void display. Message() create an object 14 { 15 16 17 cout << “Welcome to the Grade Book!" <<endl; } // end function display. Message }; // end class Grade. Book 18 19 20 21 22 23 24 25 // function main begins program execution int main() { Grade. Book my. Grade. Book; // create a Grade. Book object named my. Grade. Book. display. Message (); // call object’s display. Message function return 0; // indicate that program ended successfully } // end main 2003 Prentice Hall, Inc. All rights reserved.

8 Defining a member function with a parameter • A member function may require one or more parameters that represent additional data it needs to perform its task – example: class Bank. Account • deposit. Amount member function may require the amount of money to be deposited to be passed by the calling function. 2003 Prentice Hall, Inc. All rights reserved.

9 Defining a member function with a parameter 1 // Fig. 3. 3: fig 03_03. cpp 2 // define class Grade. Book with a member function that takes a parameter; 3 // create a Grade. Book object and call its display. Message function. 4 #include <iostream> 5 using std: : cout; 6 using std: : cin; 7 using std: : endl; 8 9 10 11 #include <string> // C++ standard string class using std: : string; using std: : getline; 12 13 // Grade. Book Class definition 14 Class Grade. Book 15 { 16 public: 17 // Function that displays a welcome message to the Grade. Book user 18 void display. Message(string course. Name) 19 { 20 cout << “Welcome to the Grade Book forn" << course. Name << "!“ 21 << endl; 22 } // end function display. Message 23 }; // end class Grade. Book 24 2003 Prentice Hall, Inc. All rights reserved.

10 Defining a member function with a parameter 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 // function main begins program execution int main() { string name. Of. Course; // string of characters to store the course name Grade. Book my. Grade. Book; // create a Grade. Book object named my. Grade. Book // prompt the user to input course name cout << “Please enter the course name: " << endl; getline (cin, name. Of. Course); // read a course name with blanks cout << endl; // output a blank line // call my. Grade. Book’s display. Message function // and pass name. Of. Course as an argument my. Grade. Book. display. Message(name. Of. Course ); return 0; // indicate that program ended successfully } // end main Please enter the course name: MECH 215 Introduction to C++ Programming Welcome to the grade book for MECH 215 Introduction to C++ Programming! 2003 Prentice Hall, Inc. All rights reserved.

Functions with more arguments and parameters • A function typically has a parameter list to indicate that it takes additional information to execute its task. • The parameter list may contain any number of parameters, including none at all. • Each parameter should specify a “type” (e. g. , string) and an “identifier” (e. g. , course. Name). • A calling function passes these parameters to a called function. • Multiple parameters in the function parameter list should be separated by a comma. • The number and order of arguments in a “function call” must match those of the called function. • More about functions in subsequent chapters! 2003 Prentice Hall, Inc. All rights reserved. 11

12 Data members, set and get functions • Local variables: – Declared in the function’s body – Can only be used in the function itself and cannot be accessed outside the function – When a function terminates, the values of its local variables are lost • Note, the attributes of an object of a particular class exist throughout the life of the object – These attributes (or data members) are represented as variables in a class definition – They are declared in the class body but outside the body of member function definitions – Each object maintains its own copy of its attributes in the memory • (remember two accounts (objects) have different balances) 2003 Prentice Hall, Inc. All rights reserved.

13 Data members, set and get functions 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 3. 5: fig 03_05. cpp // define class Grade. Book that contains a course. Name data member // and member function to get and set its value; // create and manipulate a Grade. Book object with these functions. #include <iostream> using std: : cout; using std: : cin; using std: : endl; #include <string> // C++ standard string class using std: : string; using std: : getline; 13 14 // Grade. Book Class definition 15 Class Grade. Book 16 { 17 public: 18 // Function that sets the course name 19 void set. Course. Name(string name) 20 { 21 course. Name = name; // store the course name in the object 22 } // end function set. Course. Name 23 2003 Prentice Hall, Inc. All rights reserved.

14 Data members, set and get functions 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 // Function that gets the course name string get. Course. Name() { return course. Name; // store the object’s course name } // end function get. Course. Name // Function that displays a welcome message void display. Message() { // this statement calls get. Course. Name to get the // name of the course this Grade. Book represents. cout << “Welcome to the grade book forn" << get. Course. Name()<<"!“ <<endl; } // end function display. Message private: string course. Name; // course name of this Grade. Book }; // end class Grade. Book 41 2003 Prentice Hall, Inc. All rights reserved.

15 Data members, set and get functions 42 // function main begins program execution 43 int main() 44 { 45 string name. Of. Course; // string of characters to store the course name 46 Grade. Book my. Grade. Book; // create a Grade. Book object named my. Grade. Book 47 48 49 50 // display initial value of course. Name cout << “Initial course name is: " << my. Grade. Book. get. Course. Name () << endl; 51 52 53 54 55 // prompt for input and set course name cout << “n. Please enter the course name: " << endl; getline (cin, name. Of. Course); // read a course name with blanks my. Grade. Book. set. Course. Name(name. Of. Course ); // set the course name 56 57 58 59 60 cout<< endl; // output a blank line my. Grade. Book. display. Message (); // display message with new course name return 0; // indicate that program ended successfully } // end main 2003 Prentice Hall, Inc. All rights reserved.

16 Data members, set and get functions Initial course name is: Please enter the course name: MECH 215 Introduction to C++ Programming Welcome to the grade book for MECH 215 Introduction to C++ Programming! 2003 Prentice Hall, Inc. All rights reserved.

17 Initializing Objects with Constructors • When an object is created, its data members (e. g. , course. Name) are by default initialized to empty. • Constructors can be used to initialize an object when it is created • A constructor is a special member function that is defined with the same name as a class – The compiler knows how to distinguish a constructor form other class member functions – A constructor does not return any value, so a return type cannot be specified (not even void) – Constructor are usually defined as public – Default constructor are provided when no one is specified (a default constructor does not take any parameters) • C++ requires a constructor call for each created object – Hence, each object is initialized properly before it is used – The constructor call occurs implicitly when the object is created 2003 Prentice Hall, Inc. All rights reserved.

18 Initializing Objects with Constructors 1 2 3 4 5 6 7 8 9 10 // Fig. 3. 7: fig 03_07. cpp // Instantiating multiple objects of the Grade. Book class and using // the Grade. Book constructor to specify the course name when each // Grade. Book object is created. #include <iostream> using std: : cout; using std: : endl; #include <string> // C++ standard string class using std: : string; 11 12 // Grade. Book Class definition 13 Class Grade. Book 14 { 15 public: 16 // constructor initializes course. Name with string supplied as argument 17 Grade. Book(string name) 18 { 19 set. Course. Name(name); // call set function to initialize course. Name 20 } // end Grade. Book constructor 21 2003 Prentice Hall, Inc. All rights reserved.

19 Initializing Objects with Constructors 22 23 24 25 26 // Function that sets the course name void set. Course. Name(string name) { course. Name = name; // store the course name in the object } // end function set. Course. Name 27 28 29 30 31 32 // Function that gets the course name string get. Course. Name() { return course. Name; // store the object’s course name } // end function get. Course. Name 33 34 35 36 37 38 39 40 41 42 43 // Function that displays a welcome message void display. Message() { // call get. Course. Name to get the course. Name cout << “Welcome to the grade book forn" << get. Course. Name()<<"!“ <<endl; } // end function display. Message private: string course. Name; // course name of this Grade. Book }; // end class Grade. Book 44 2003 Prentice Hall, Inc. All rights reserved.

20 Initializing Objects with Constructors 45 // function main begins program execution 46 int main() 47 { 48 // create two Grade. Book objects 49 Grade. Book grade. Book 1( "Mech 215 Introduction to C++ Programming" ); 50 Grade. Book grade. Book 2("Mech 216 Advanced Programming" ); 51 52 53 54 55 56 57 // display initial value of course. Name for each Grade. Book object cout << "grade. Book 1 created for course: " << grade. Book 1. get. Course. Name() << "ngrade. Book 2 created for course: " << grade. Book 2. get. Course. Name() << endl; return 0; // indicate that program ended successfully } // end main grade. Book 1 created for course: MECH 215 Introduction to C++ Programming grade. Book 2 created for course: MECH 216 Advanced Programming 2003 Prentice Hall, Inc. All rights reserved.

Placing a Class in a Separate File for Reusability • Class reusability – e. g. , C++ standard library type “string” can be used in any program by including the header <string> in the program – Class Grade. Book from the previous example cannot be reused because the file we defined earlier cannot simply be included in another file since it contains a main function. • Every program contains only one main function • Files with more than one main function will result in a compile error • Placing a main in the same file with a class definition prevents that class from being reused by other programs – When building an OO C++ program, classes are best defined in “. h” (header file) filename extension to be reusable 2003 Prentice Hall, Inc. All rights reserved. 21

Placing a Class in a Separate File for Reusability • Class reusability – Programs generally use #include preprocessor directives to include header files and take advantage of reusable software components (e. g. , type string) as well as user defined classes (e. g. , Grade. Book). – A program can be separated into two files: filename. h and filename. cpp • main function is defined in the filename. cpp • Classes are defined in the filename. h • filename. h does not execute by itself because it does not contain a main function. 2003 Prentice Hall, Inc. All rights reserved. 22

Placing a Class in a Separate File for Reusability 1 2 3 4 5 6 7 8 // Fig. 3. 9: Grade. Book. h // Grade. Book class definition in a separate file from main #include <iostream> using std: : cout; using std: : endl; #include <string> // C++ standard string class using std: : string; 9 10 // Grade. Book Class definition 11 Class Grade. Book 12 { 13 public: 14 // constructor initializes course. Name with string supplied as argument 15 Grade. Book(string name) 16 { 17 set. Course. Name(name); // call set function to initialize course. Name 18 } // end Grade. Book constructor 19 20 21 22 23 24 // Function that sets the course name void set. Course. Name(string name) { course. Name = name; // store the course name in the object } // end function set. Course. Name 25 2003 Prentice Hall, Inc. All rights reserved. 23

Placing a Class in a Separate File for Reusability 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 // Function that gets the course name string get. Course. Name() { return course. Name; // store the object’s course name } // end function get. Course. Name // Function that displays a welcome message void display. Message() { // call get. Course. Name to get the course. Name cout << “Welcome to the grade book forn" << get. Course. Name()<<"!“ <<endl; } // end function display. Message private: string course. Name; // course name of this Grade. Book }; // end class Grade. Book 2003 Prentice Hall, Inc. All rights reserved. 24

Placing a Class in a Separate File for Reusability • Fundamental data types (e. g. , int, double, etc. ) are known to the compiler; however – Grade. Book when used in the main program, the compiler does not know its definition because it is user-defined type – The compiler does not even know the classes defined in the standard library • e. g. , we must include <string> header file so that the compiler knows the type string when used in the program. – This enable the compiler to determine the amount of memory that it must reserve for each object of the class – Ensures that the program calls the member functions of the class correctly 2003 Prentice Hall, Inc. All rights reserved. 25

Placing a Class in a Separate File for Reusability • When we create two (or more) objects of the same class: – C++ compiler creates only one copy of class member functions and share them among all objects – Each object needs its own copy of data members because their contents vary among different objects • e. g. : two (objects) different bank accounts (of bank account class) with different balance data members. • Member functions (such as deposit, withdraw, etc. ) can be shared among multiple objects. – The size of memory allocated for an object depends on the class data members • Including filename. h in the program gives the compiler access to the information it needs in order to allocate memory 2003 Prentice Hall, Inc. All rights reserved. 26

Placing a Class in a Separate File for Reusability 1 2 3 4 5 6 7 // Fig. 3. 10: fig 03_10. cpp // Including class Grade. Book from file Grade. Book. h for use in main #include <iostream> using std: : cout; using std: : endl; #include “Grade. Book. h” // include definition of class Grade. Book 8 9 // function main begins program execution 10 int main() 11 { 12 // create two Grade. Book objects 13 Grade. Book grade. Book 1( "Mech 215 Introduction to C++ Programming" ); 14 Grade. Book grade. Book 2("Mech 216 Advanced Programming" ); 15 16 17 18 19 20 21 // display initial value of course. Name for each Grade. Book object cout << "grade. Book 1 created for course: " << grade. Book 1. get. Course. Name() << "ngrade. Book 2 created for course: " << grade. Book 2. get. Course. Name() << endl; return 0; // indicate that program ended successfully } // end main grade. Book 1 created for course: MECH 215 Introduction to C++ Programming grade. Book 2 created for course: MECH 216 Advanced Programming 2003 Prentice Hall, Inc. All rights reserved. 27

28 Separating Interface from Implementation • The interface of a class – Describes what services a class’s clients can use – Describes how to request those services – Does not describe how the class carries out those services • Separating Interface from Implementation – Before, all member functions and data members were all defined within the body of the class definition – It is better to define member functions outside the class definition • implementation details are hidden from the client code 2003 Prentice Hall, Inc. All rights reserved.

29 Separating Interface from Implementation 1 2 3 4 5 6 // Fig. 3. 11: Grade. Book. h // Grade. Book class definition. This file represents Grade. Book’s public // interface without revealing the implementations of Grade. Book’s member // functions, which are defined in Grade. Book. cpp #include <string> using std: : string; 7 8 // Grade. Book Class definition 9 Class Grade. Book 10 { 11 public: 12 Grade. Book(string); // constructor initializes course. Name 13 void set. Course. Name(string); // Function that sets the course name 14 string get. Course. Name(); // Function that gets the course name 15 void display. Message(); // Function that displays a welcome message 16 private: 17 string course. Name; // course name of this Grade. Book 18 }; // end class Grade. Book 2003 Prentice Hall, Inc. All rights reserved.

30 Separating Interface from Implementation 1 2 3 4 5 6 // Fig. 3. 12: Grade. Book. cpp // Grade. Book member function definitions. This file contains // implementations of the member functions prototyped in Grade. Book. h. #include <iostream> using std: : cout; using std: : endl; 7 8 #include “Grade. Book. h” // include definition of class Grade. Book 9 10 11 12 13 14 // constructor initializes course. Name with string supplied as argument Grade. Book: : Grade. Book(string name) { set. Course. Name(name); // call set function to initialize course. Name } // end Grade. Book constructor 15 16 17 18 19 20 // Function that sets the course name void Grade. Book: : set. Course. Name(string name) { course. Name = name; // store the course name in the object } // end function set. Course. Name 21 2003 Prentice Hall, Inc. All rights reserved.

31 Separating Interface from Implementation 22 23 24 25 26 // Function that gets the course name String Grade. Book: : get. Course. Name () { return course. Name; // store the object’s course name } // end function get. Course. Name 27 28 29 30 31 32 33 34 // Function that displays a welcome message void Grade. Book: : display. Message () { // call get. Course. Name to get the course. Name cout << “Welcome to the grade book forn" << get. Course. Name()<<"!“ <<endl; } // end function display. Message 2003 Prentice Hall, Inc. All rights reserved.

32 Separating Interface from Implementation 1 2 3 4 5 6 // Fig. 3. 13: fig 03_13. cpp // Grade. Book class demonstration after separating // its interface from its implementation #include <iostream> using std: : cout; using std: : endl; 7 8 #include “Grade. Book. h” // include definition of class Grade. Book 9 10 // function main begins program execution 11 int main() 12 { 13 // create two Grade. Book objects 14 Grade. Book grade. Book 1( "Mech 215 Introduction to C++ Programming" ); 15 Grade. Book grade. Book 2("Mech 216 Advanced Programming" ); 16 17 18 19 20 21 22 // display initial value of course. Name for each Grade. Book object cout << "grade. Book 1 created for course: " << grade. Book 1. get. Course. Name() << "ngrade. Book 2 created for course: " << grade. Book 2. get. Course. Name() << endl; return 0; // indicate that program ended successfully } // end main grade. Book 1 created for course: MECH 215 Introduction to C++ Programming grade. Book 2 created for course: MECH 216 Advanced Programming 2003 Prentice Hall, Inc. All rights reserved.
- Slides: 32