OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Similar documents
Chapter 9 Classes : A Deeper Look, Part 1

More C++ Classes. Systems Programming

Classes: A Deeper Look

Introduction to Programming session 24

IS 0020 Program Design and Software Tools

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Computer Programming with C++ (21)

Object Oriented Design

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

Fundamentals of Programming Session 24

A Deeper Look at Classes

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

IS 0020 Program Design and Software Tools

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

Classes: A Deeper Look. Systems Programming

IS 0020 Program Design and Software Tools

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

Classes and Data Abstraction

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

9.1 Introduction. Integrated Time class case study Preprocessor wrapper Three types of handles on an object. Class functions

Chapter 6: Classes and Data Abstraction

Classes and Data Abstraction. Topic 5

C++ Programming Chapter 7 Pointers

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Computer Programming Class Members 9 th Lecture

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Preview 11/1/2017. Constant Objects and Member Functions. Constant Objects and Member Functions. Constant Objects and Member Functions

57:017, Computers in Engineering C++ Classes

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Chapter 5 - Pointers and Strings

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Chapter 5 - Pointers and Strings

by Pearson Education, Inc. All Rights Reserved.

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES III

BITG 1113: Array (Part 2) LECTURE 9

IS 0020 Program Design and Software Tools

1 Pointer Concepts. 1.1 Pointer Examples

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Fig. 7.1 Fig. 7.2 Fig. 7.3 Fig. 7.4 Fig. 7.5 Fig. 7.6 Fig. 7.7 Fig. 7.8 Fig. 7.9 Fig. 7.10

Short Notes of CS201

Object Oriented Design

CS201 - Introduction to Programming Glossary By

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Chapter 16: Classes and Data Abstraction

Lecture 18 Tao Wang 1

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Built-in Functions for NTCAs.

Fast Introduction to Object Oriented Programming and C++

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Chapter 10 Introduction to Classes

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Chapter 17 - C++ Classes: Part II

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Lecture 5. Function Pointers

Classes: A Deeper Look, Part 2

Classes and Objects. Types and Classes. Example. Object Oriented Programming

C-String Library Functions

Object Oriented Programming COP3330 / CGS5409

Angela Chih-Wei Tang Visual Communications Lab Department of Communication Engineering National Central University JhongLi, Taiwan.

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

III. Classes (Chap. 3)

EE 355 Lab 4 - Party Like A Char Star

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++

clarity. In the first form, the access specifier public : is needed to {

Introduction to Classes

CS201 Latest Solved MCQs

OBJECT ORIENTED PROGRAMMING USING C++

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

Abstraction in Software Development

Come and join us at WebLyceum

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Class. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Classes and Data Abstraction. Topic 5

Chapter 8 - Characters and Strings

Come and join us at WebLyceum

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Introduction & Review

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

CS3157: Advanced Programming. Outline

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Cpt S 122 Data Structures. Introduction to C++ Part II

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Scientific Programming in C V. Strings

Interview Questions of C++

Transcription:

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... }