KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II KOM3191 Object-Oriented Programming
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Function Pointers A pointer to a function contains the address of the function in memory The name of an array is actually the address in memory of the first element of the array The name of a function is actually the starting address in memory of the code that performs the function's task Pointers to functions can be passed to functions, returned from functions, stored in arrays and assigned to other function pointers. Array names are pointers Function names are function pointers int (*operationptr)(int,int); operationptr is a function pointer variable and can point out the starting address of a function code.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 3 Arrays of Function Pointers int addition (int a, int b) { return (a+b); } int subtraction (int a, int b) { return (a-b); } int operation (int x, int y, int (*functionptr)(int,int)) { int g = (*functionptr)(x,y); return (g); } int main () { int m,n; int (*operationptr)(int,int); operationptr= addition; cout<< (*operationptr)(7,5)<<endl; //12 cout<< (*addition)(7,5)<<endl; //12 cout<< addition(7,5)<<endl; //12 m = operation (7, 5, operationptr); cout << m <<endl; //12 operationptr= subtraction; cout<< (*operationptr)(7,5)<<endl; //2 n = operation (7, 5, operationptr); cout << n <<endl; //2 return 0; } www.cplusplus.com
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 4 Arrays of Function Pointers int function0 (int a, int b) { return (a+b); } int function1 (int a, int b) { return (a-b); } int main () { int m,n; int (* operationptr[2])(int,int)={function0, function1} } for (int i=0; i<2; i++) cout<< (*operationptr[i])(7,5)<<endl; return 0; www.cplusplus.com
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 5 Pointer-Based String Processing char color[] = "blue"; const char *colorptr = "blue"; The first declaration creates a five-element array color containing the characters 'b', 'l', 'u', 'e' and '\0'. The second declaration creates pointer variable colorptr that points to the letter b in the string "blue" (which ends in '\0') somewhere in memory. When declaring a character array to contain a string, the array must be large enough to store the string and its terminating null character. The preceding declaration determines the size of the array, based on the number of initializers provided in the initializer list. Not allocating sufficient space in a character array to store the null character that terminates a string is an error. C++ allows strings of any length to be stored. If a string is longer than the character array in which it is to be stored, characters beyond the end of the array will overwrite data in memory following the array, leading to logic errors.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 6 String-manipulation functions char name[ 8 ]; cin>> name; //super_man cout<<name<<endl; //*** stack smashing detected ***: char name[ 8 ]; cin>>setw(8)>>name; cout<<name<<endl; //super_man //output is super_m char name[ 8 ]; cin.getline(name, 8, '\n' ); cout<<name<<endl; // super_man // output is super_m The function stops reading characters when the delimiter character '\n' is encountered, when the endof-file indicator is entered or when the number of characters read so far is one less than the length specified in the second argument. cin.getline( sentence, 8 ); same as cin.getline(name, 8, '\n' );
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 7 String-manipulation functions String-manipulation functions of the string-handling library. <cstring> header file char *strcpy( char *s1, const char *s2 ); char *strncpy( char *s1, const char *s2, size_t n ); char *strcat( char *s1, const char *s2 ); char *strncat( char *s1, const char *s2, size_t n ); int strcmp( const char *s1, const char *s2 ); int strncmp( const char *s1, const char *s2, size_t n ); char *strtok( char *s1, const char *s2 ); size_t strlen( const char *s );
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 8 OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 9 A class example Data members of a class cannot be initialized where they are declared Static data members are known as class variables classname::static_data_member "preprocessor wrapper If the header has not been included previously in a file, the name TIME_H is defined by the #define directive and the header file statements are included.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 10 A class example There can be several overloaded constructors, initialization of the data members through constructors are highly recommended When setting the values of the data members there should be a validation. A consistent value should used. fixed, setprecision( ), setfill( ) : sticky settings setw( ), left, right only effects the next value.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 11 A class example Standard Time 11:59 pm 23:59 12:00 am (midnight) 00:00 12:01 am 00:01 01:00 am 01:00 02:00 am 02:00 10:00 am 10:00 11:00 am 11:00 11:59 am 11:59 12:00 pm (noon) 12:00 12:01 pm 12:01 01:00 pm 13:00 02:00 pm 14:00 10:00 pm 22:00 11:00 pm 23:00
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 12 A class example Possible ways to instantiate a Class An object Time sunset; A reference to an object Time &dinnertime=sunset; An array of objects Time arrayoftimes[5]; A pointer to an object Time *timeptr=&sunset;
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 13 Member functions vs Global functions void Time::printUniversal(){...} void Time::printStandard() {...} printuniversal and printstandard member functions take no arguments. The member functions of a class implicitly know that they are to use data members of the particular class object for which they are invoked. This can make member function calls more concise than conventional function calls in procedural programming. Member functions are usually shorter than functions in non-object-oriented programs, because the data stored in data members have ideally been validated by a constructor or by member functions that store new data. Because the data is already in the object, the member-function calls often have no arguments or at least have fewer arguments than typical function calls in non-objectoriented languages. Thus, the calls are shorter, the function definitions are shorter and the function prototypes are shorter. This facilitates many aspects of program development.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 14 Class Scope Member functions declared in a class definition are within the class s scope. Member functions are tied to the class scope via scope resolution operator :: Member function defined in the body of a class definition C++ compiler attempts to inline calls to the member function. Only the simplest and most stable member functions (i.e., whose implementations are unlikely to change) should be defined in the class header. Data members: Variables declared in the class definition. Member functions: Functions declared in the class definition. are in the class scope Non-member functions are defined at file scope. Local variables declared within each member function have block scope Hiding a class-scope variable can be prevented with use of ::
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 15 Class Scope Within a class s scope Class members can be used by all member functions. Outside a class s scope public class members are referenced through a handle: An object name A reference to an object A pointer to an object. Dot member selection operator (.) Used with an object s name or with a reference to an object. Arrow member selection operator (->) Accesses the object s members. Time sunset; sunset.printuniversal(); Time *timeptr=&sunset; timeptr printuniversal();
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 16 size of an object Objects contain only data, so objects are much smaller than if they also contained member functions. Applying operator sizeof to a class name or to an object of that class will report only the size of the class s data members. The compiler creates one copy (only) of the member functions separate from all objects of the class. All objects of the class share this one copy. Each object, of course, needs its own copy of the class s data, because the data can vary among the objects. The function code is non-modifiable and, hence, can be shared among all objects of one class.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 17 Access and utility functions of a class Access functions read or display data test the truth or falsity of conditions. e.g isempty function Utility functions (also called helper functions) private member functions that support the operation of the class s public member functions. Not part of a class s public interface Not intended to be used by clients of a class.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 18 Access and utility functions of a class
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 19 Access and utility functions of a class
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 20 Constructor with default arguments //Time.h Time( int = 0, int = 0, int = 0 ); // default constructor //Time.cpp Time::Time( int hr, int min, int sec ) { settime( hr, min, sec ); // validate and set time } //test_time.cpp int main() { Time t1; // all arguments defaulted Time t2( 2 ); // hour specified; minute and second defaulted Time t3( 21, 34 ); // hour and minute specified; second defaulted Time t4( 12, 25, 42 ); // hour, minute and second specified Time t5( 27, 74, 99 ); // all bad values specified... }