Fundamentals of Programming Session 24 Instructor: Reza - - PowerPoint PPT Presentation

fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

Fundamentals of Programming Session 24 Instructor: Reza - - PowerPoint PPT Presentation

Fundamentals of Programming Session 24 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitels slides Sharif University of Technology Outlines Data Members, set Functions


slide-1
SLIDE 1

Fall 2013

Instructor: Reza Entezari-Maleki

Email: entezari@ce.sharif.edu

Sharif University of Technology

1

Fundamentals of Programming

Session 24

These slides have been created using Deitel’s slides

slide-2
SLIDE 2

Outlines

 Data Members, set Functions and get Functions  Constructors  Destructors  When Constructors and Destructors Are Called

2

slide-3
SLIDE 3

3

Data Members, set Functions and get Functions …

slide-4
SLIDE 4

4

Data Members, set Functions and get Functions …

slide-5
SLIDE 5

5

Data Members, set Functions and get Functions …

slide-6
SLIDE 6

 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)  Class instance: object

6

Data Members, set Functions and get Functions …

slide-7
SLIDE 7

 Classes

 Model objects

 Attributes (data members)  Behaviors (member functions)

 Defined using keyword class  Member functions

 Methods  Invoked in response to messages

 Member access specifiers

 public:

 Accessible wherever object of class in scope

 private:

 Accessible only to member functions of class

 protected:

7

Data Members, set Functions and get Functions …

slide-8
SLIDE 8

8

1 classTime { 2 3 public: 4 5 void setTime( int, int, int ); // set hour, minute, second 6 void printUniversal(); // print universal-time format 7 void printStandard(); // print standard-time format 8 9 private: 10 int hour; // 0 - 23 (24-hour clock format) 11 int minute; // 0 - 59 12 int second; // 0 - 59 13 14 }; // end class Time

slide-9
SLIDE 9

 Class scope

 Data members, member functions  Within class scope

 Class members

 Immediately accessible by all member functions  Referenced by name

 Outside class scope

 Referenced through handles

 Object name, reference to object, pointer to object

 File scope

 Nonmember functions

9

Data Members, set Functions and get Functions …

slide-10
SLIDE 10

 Function scope

 Variables declared in member function  Only known to function  Variables with same name as class-scope variables

 Class-scope variable “hidden”

 Access with scope resolution operator (::)

ClassName::classVariableName  Variables only known to function they are defined in  Variables are destroyed after function completion

10

Data Members, set Functions and get Functions …

slide-11
SLIDE 11

 Operators to access class members

 Identical to those for structs  Dot member selection operator (.)

 Object  Reference to object

 Arrow member selection operator (->)

 Pointers

11

Data Members, set Functions and get Functions …

slide-12
SLIDE 12

12

1 // Demonstrating the class member access operators . and -> 2 // CAUTION: IN FUTURE EXAMPLES WE AVOID PUBLIC DATA! 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 // class Count definition 7 class Count { 8 public: 9 int x; 10 void print() 11 { 12 cout << x << endl; 13 } 14 }; // end class Count

slide-13
SLIDE 13

13

15 int main() 16 { 17 Count counter; // create counter object 18 Count *counterPtr = &counter; // create pointer to counter 19 Count &counterRef = counter; // create reference to counter 20 cout << "Assign 1 to x and print using the object's name: "; 21 counter.x = 1; // assign 1 to data member x 22 counter.print(); // call member function print 23 cout << "Assign 2 to x and print using a reference: "; 24 counterRef.x = 2; // assign 2 to data member x 25 counterRef.print(); // call member function print 26 cout << "Assign 3 to x and print using a pointer: "; 27 counterPtr->x = 3; // assign 3 to data member x 28 counterPtr->print(); // call member function print 29 return 0; 30 } // end main

Assign 1 to x and print using the object's name: 1 Assign 2 to x and print using a reference: 2 Assign 3 to x and print using a pointer: 3

slide-14
SLIDE 14

 Class member access

 Default private  Explicitly set to private, public, protected

 Access to class’s private data

 Controlled with access functions (accessor methods)

 Get function

 Read private data

 Set function

 Modify private data

14

Data Members, set Functions and get Functions …

slide-15
SLIDE 15

 Constructors

 Initialize data members

 Or can set later

 Same name as class  No return type

 Initializers

 Passed as arguments to constructor  In parentheses to right of class name before semicolon Class-type ObjectName( value1,value2,…);

15

Constructors

slide-16
SLIDE 16

16

1 // Member functions for class SalesPerson. 2 #include <iostream> 3 #include <iomanip> 4 using namespace std; 5 // class definition 6 class SalesPerson { 7 public: 8 SalesPerson(); // constructor 9 void getSalesFromUser(); // input sales from keyboard 10 void setSales( int, double ); // set sales for a month 11 void printAnnualSales(); // summarize and print sales 12 private: 13 double totalAnnualSales(); // utility function 14 double sales[ 12 ]; // 12 monthly sales figures 15 }; // end class SalesPerson

slide-17
SLIDE 17

17

16 SalesPerson::SalesPerson() 17 { 18 for ( int i = 0; i < 12; i++ ) 19 sales[ i ] = 0.0; 20 } // end SalesPerson constructor 21 // get 12 sales figures from the user at the keyboard 22 void SalesPerson::getSalesFromUser() 23 { 24 double salesFigure; 25 for ( int i = 1; i <= 12; i++ ) { 26 cout << "Enter sales amount for month " << i << ": "; 27 cin >> salesFigure; 28 setSales( i, salesFigure ); 29 } // end for 30 } // end function getSalesFromUser 31 // set one of the 12 monthly sales figures; function subtracts 32 // one from month value for proper subscript in sales array

slide-18
SLIDE 18

18

33 void SalesPerson::setSales( int month, double amount ) 34 { 35 // test for valid month and amount values 36 if ( month >= 1 && month <= 12 && amount > 0 ) 37 sales[ month - 1 ] = amount; // adjust for subscripts 0-11 38 else // invalid month or amount value 39 cout << "Invalid month or sales figure" << endl; 40 } // end function setSales 41 // print total annual sales (with help of utility function) 42 void SalesPerson::printAnnualSales() 43 { 44 cout << setprecision( 2 ) << fixed 45 << "\nThe total annual sales are: $" 46 << totalAnnualSales() << endl; // call utility function 47 } // end function printAnnualSales 48 // private utility function to total annual sales

slide-19
SLIDE 19

19

49 double SalesPerson::totalAnnualSales() 50 { 51 double total = 0.0; // initialize total 52 for ( int i = 0; i < 12; i++ ) // summarize sales results 53 total += sales[ i ]; 54 return total; 55 } // end function totalAnnualSales 56 int main() 57 { 58 SalesPerson s; // create SalesPerson object s 59 s.getSalesFromUser(); // note simple sequential code; no 60 s.printAnnualSales(); // control structures in main 61 return 0; 62 } // end main

slide-20
SLIDE 20

20

Enter sales amount for month 1: 5314.76 Enter sales amount for month 2: 4292.38 Enter sales amount for month 3: 4589.83 Enter sales amount for month 4: 5534.03 Enter sales amount for month 5: 4376.34 Enter sales amount for month 6: 5698.45 Enter sales amount for month 7: 4439.22 Enter sales amount for month 8: 5893.57 Enter sales amount for month 9: 4909.67 Enter sales amount for month 10: 5123.45 Enter sales amount for month 11: 4024.97 Enter sales amount for month 12: 5923.92 The total annual sales are: $60120.59

slide-21
SLIDE 21

 Constructors

 Can specify default arguments  Default constructors

 Defaults all arguments

OR

 Explicitly requires no arguments  Can be invoked with no arguments  Only one per class

21

Constructors …

slide-22
SLIDE 22

22

1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 5 classTime { 6 public: 7 Time( int = 0, int = 0, int = 0); // default constructor 8 void setTime( int, int, int ); // set hour, minute, second 9 void printUniversal(); // print universal-time format 10 void printStandard(); // print standard-time format 11 private: 12 int hour; // 0 - 23 (24-hour clock format) 13 int minute; // 0 - 59 14 int second; // 0 - 59 15 }; // end class Time

slide-23
SLIDE 23

23

// ensures all Time objects start in a consistent state 16 Time::Time( int hr, int min, int sec ) 17 { 18 setTime( hr, min, sec ); // validate and set time 19 } // end Time constructor 20 // set new Time value using universal time, perform validity 21 // checks on the data values and set invalid values to zero 22 voidTime::setTime( int h, int m, int s ) 23 { 24 hour = ( h >= 0 && h < 24 ) ? h : 0; 25 minute = ( m >= 0 && m < 60 ) ? m : 0; 26 second = ( s >= 0 && s < 60 ) ? s : 0; 27 } // end function setTime 28 // print Time in universal format 29 voidTime::printUniversal() 30 { 31 cout << setfill( '0' ) << setw( 2 ) << hour << ":" 32 << setw( 2 ) << minute << ":" 33 << setw( 2 ) << second; 34 } // end function printUniversal

slide-24
SLIDE 24

24

30 // print Time in standard format 31 voidTime::printStandard() 32 { 33 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) 34 << ":" << setfill( '0' ) << setw( 2 ) << minute 35 << ":" << setw( 2 ) << second 36 << ( hour < 12 ? " AM" : " PM" ); 37 38 } // end function printStandard

slide-25
SLIDE 25

25

7 int main() 8 { 9 Time t1; // all arguments defaulted 10 Time t2( 2 ); // minute and second defaulted 11 Time t3( 21, 34 ); // second defaulted 12 Time t4( 12, 25, 42 ); // all values specified 13 Time t5( 27, 74, 99 ); // all bad values specified 14 cout << "Constructed with:\n\n" 15 << "all default arguments:\n "; 16 t1.printUniversal(); // 00:00:00 17 cout << "\n "; 18 t1.printStandard(); // 12:00:00 AM

slide-26
SLIDE 26

26

19 cout << "\n\nhour specified; default minute and second:\n "; 20 t2.printUniversal(); // 02:00:00 21 cout << "\n "; 22 t2.printStandard(); // 2:00:00 AM 23 cout << "\n\nhour and minute specified; default second:\n "; 24 t3.printUniversal(); // 21:34:00 25 cout << "\n "; 26 t3.printStandard(); // 9:34:00 PM 27 cout << "\n\nhour, minute, and second specified:\n "; 28 t4.printUniversal(); // 12:25:42 29 cout << "\n "; 30 t4.printStandard(); // 12:25:42 PM 31 cout << "\n\nall invalid values specified:\n "; 32 t5.printUniversal(); // 00:00:00 33 cout << "\n "; 34 t5.printStandard(); // 12:00:00 AM 35 cout << endl; 36 return 0; 37 } // end main

slide-27
SLIDE 27

27

Constructed with: all default arguments: 00:00:00 12:00:00 AM hour specified; default minute and second: 02:00:00 2:00:00 AM hour and minute specified; default second: 21:34:00 9:34:00 PM hour, minute, and second specified: 12:25:42 12:25:42 PM all invalid values specified: 00:00:00 12:00:00 AM

slide-28
SLIDE 28

 Destructors

 Special member function  Same name as class

 Preceded with tilde (~)

 No arguments  No return value  Cannot be overloaded  No explicit destructor

 Compiler creates “empty” destructor

28

Destructors

slide-29
SLIDE 29

 Constructors and destructors

 Called implicitly by compiler

 Order of function calls

 Depends on order of execution

 When execution enters and exits scope of objects

 Generally, destructor calls reverse order of constructor calls

29

Destructors …

slide-30
SLIDE 30

 Order of constructor, destructor function calls

 Global scope objects

 Constructors

 Before any other function (including main)

 Destructors

 When main terminates (or exit function called)  Not called if program terminates with abort

 Automatic local objects

 Constructors

 When objects defined

  • Each time execution enters scope

 Destructors

 When objects leave scope

  • Execution exits block in which object defined

 Not called if program ends with exit or abort

30

When Constructors and Destructors Are Called

slide-31
SLIDE 31

 Order of constructor, destructor function calls

 static local objects

 Constructors

 Exactly once  When execution reaches point where object defined

 Destructors

 When main terminates or exit function called  Not called if program ends with abort

31

When Constructors and Destructors Are Called …

slide-32
SLIDE 32

32

1 #include <iostream> 2 using namespace std; 3 // Definition of class CreateAndDestroy. 4 class CreateAndDestroy { 5 public: 6 CreateAndDestroy( int, char * ); // constructor 7 ~CreateAndDestroy(); // destructor 8 private: 9 int objectID; 10 char *message; 11 }; // end class CreateAndDestroy 12 // constructor 13 CreateAndDestroy::CreateAndDestroy( 14 int objectNumber, char *messagePtr ) 15 { 16 objectID = objectNumber; 17 message = messagePtr; 18 cout << "Object " << objectID << " constructor runs " 19 << message << endl; 20 } // end CreateAndDestroy constructor

slide-33
SLIDE 33

33

21 // destructor 22 CreateAndDestroy::~CreateAndDestroy() 23 { 24 // the following line is for pedagogic purposes only 25 cout << ( objectID == 1 || objectID == 6 ? "\n" : "" ); 26 27 cout << "Object " << objectID << " destructor runs " 28 << message << endl; 29 30 } // end ~CreateAndDestroy destructor 31 32 void create( void ); // prototype 33 // global object 34 CreateAndDestroy first( 1, "(global before main)" ); 35 int main() 36 { 37 cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl; 38 CreateAndDestroy second( 2, "(local automatic in main)" ); 39 static CreateAndDestroy third( 40 3, "(local static in main)" );

slide-34
SLIDE 34

34

41 create(); // call function to create objects 42 cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl; 43 CreateAndDestroy fourth( 4, "(local automatic in main)" ); 44 cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl; 45 return 0; 46 } // end main 47 // function to create objects 48 void create( void ) 49 { 50 cout << "\nCREATE FUNCTION: EXECUTION BEGINS" << endl; 51 CreateAndDestroy fifth( 5, "(local automatic in create)" ); 52 static CreateAndDestroy sixth( 53 6, "(local static in create)" ); 54 CreateAndDestroy seventh( 55 7, "(local automatic in create)" ); 56 cout << "\nCREATE FUNCTION: EXECUTION ENDS\" << endl; 57 } // end function create

slide-35
SLIDE 35

35

Object 1 constructor runs (global before main) MAIN FUNCTION: EXECUTION BEGINS Object 2 constructor runs (local automatic in main) Object 3 constructor runs (local static in main) CREATE FUNCTION: EXECUTION BEGINS Object 5 constructor runs (local automatic in create) Object 6 constructor runs (local static in create) Object 7 constructor runs (local automatic in create) CREATE FUNCTION: EXECUTION ENDS Object 7 destructor runs (local automatic in create) Object 5 destructor runs (local automatic in create) MAIN FUNCTION: EXECUTION RESUMES Object 4 constructor runs (local automatic in main) MAIN FUNCTION: EXECUTION ENDS Object 4 destructor runs (local automatic in main) Object 2 destructor runs (local automatic in main) Object 6 destructor runs (local static in create) Object 3 destructor runs (local static in main) Object 1 destructor runs (global before main)