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>

Size: px
Start display at page:

Download "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>"

Transcription

1 CISC11 Introduction to Computer Science Dr. McCoy Lecture 20 November, Using Default Arguments with Constructors 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 // Fig..: time2.h 2 // Declaration of class Time. 3 // Member functions defined in time2.cpp. 3 1 // Fig..13: time2.cpp 2 // Member-function definitions for class Time. // prevent multiple inclusions of header file #ifndef TIME2_H #define TIME2_H time2.h (1 of 1) using std::cout; #include <iomanip> time2.cpp (1 of 3) 9 // Time abstract data type definition Default constructor specifying class Time { all arguments. public: 13 Time( int = 0, int = 0, int = 0); // default constructor 1 void settime( int, int, int ); // set hour, minute, second 1 void printuniversal(); // print universal-time format 1 void printstandard(); // print standard-time format 1 1 private: 19 int hour; // 0-23 (2-hour clock format) 20 int minute; // int second; // using std::setfill; using std::setw; // include definition of class Time from time2.h 13 #include "time2.h" 1 Constructor calls settime 1 // Time constructor initializes each data member to zero; to validate passed (or default) 1 // ensures all Time objects start in a consistent state values. 1 Time::Time( int hr, int min, int sec ) 1 { 19 settime( hr, min, sec ); // validate and set time } // end Time constructor 23 }; // end class Time 2 2 #endif 23 // set new Time value using universal time, perform validity 2 // checks on the data values and set invalid values to zero 2 void Time::setTime( int h, int m, int s ) 2 { 2 hour = ( h >= 0 && h < 2 )? h : 0; 2 minute = ( m >= 0 && m < 0 )? m : 0; 29 second = ( s >= 0 && s < 0 )? s : 0; time2.cpp (2 of 3) 2 // print Time in standard format 3 void Time::printStandard() { cout << ( ( hour == 0 hour == )? : hour % ) << ":" << setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 ) << second << ( hour <? " AM" : " PM" ); time2.cpp (3 of 3) } // end function settime 9 0 } // end function printstandard 33 // print Time in universal format 3 void Time::printUniversal() 3 { 3 cout << setfill( '0' ) << setw( 2 ) << hour << ":" 3 << setw( 2 ) << minute << ":" 3 << setw( 2 ) << second; 39 0 } // end function printuniversal 1 1

2 1 // Fig..1: fig0_1.cpp 2 // Demonstrating a default constructor for class Time. using std::cout; using std::endl; // include definition of class Time from time2.h 9 #include "time2.h" fig0_1.cpp (1 of 2) 2 cout << "\n\nhour specified; default minute and second:\n "; 2 t2.printuniversal(); // 02:00:00 2 cout << "\n "; 2 t2.printstandard(); // 2:00:00 AM cout << "\n\nhour and minute specified; default second:\n "; 31 t3.printuniversal(); // 21:3:00 cout << "\n "; 33 t3.printstandard(); // 9:3:00 PM fig0_1.cpp (2 of 2) int main() { 13 Time t1; // all arguments defaulted 1 Time t2( 2 ); // minute and second defaulted 1 Time t3( 21, 3 ); // second defaulted 1 Time t(, 2, 2 // all values specified ); 1 Time t( 2,, 99 ); // all bad values specified 1 19 cout << "Constructed with:\n\n" 20 << "all default arguments:\n "; 21 t1.printuniversal(); // 00:00:00 cout << "\n "; 23 t1.printstandard(); // :00:00 AM 2 Initialize Time objects using default arguments. Initialize Time object with invalid values; validity checking will set values to cout << "\n\nhour, minute, and second specified:\n "; 3 t.printuniversal(); // :2:2 3 cout << "\n "; 3 t.printstandard(); // :2:2 PM 39 t constructed with invalid 0 cout << "\n\nall invalid values specified:\n "; arguments; values set to 0. 1 t.printuniversal(); // 00:00:00 2 cout << "\n "; 3 t.printstandard(); // :00:00 AM cout << endl; return 0; // end main } Constructed with: all default arguments: 00:00:00 :00:00 AM hour specified; default minute and second: 02:00:00 2:00:00 AM hour and minute specified; default second: 21:3:00 9:3:00 PM hour, minute, and second specified: :2:2 :2:2 PM all invalid values specified: 00:00:00 :00:00 AM fig0_1.cpp output (1 of 1) 9.1 Using Set and Get Functions Set functions Perform validity checks before modifying private data Notify if invalid values Indicate with return values Get functions Query functions Control format of data returned 1 // Fig..1: time3.h 2 // Declaration of class Time. 3 // Member functions defined in time3.cpp // prevent multiple inclusions of header file #ifndef TIME3_H #define TIME3_H time3.h (1 of 2) 2 void printuniversal(); // output universal-time format 2 void printstandard(); // output standard-time format 2 2 private: 29 int hour; // 0-23 (2-hour clock format) 30 int minute; // int second; // 0-9 time3.h (2 of 2) 9 class Time { 33 }; // end clas Time public: Time( int = 0, int = 0, int = 0 ); // default constructor 13 1 // set functions 1 void settime( int, int, int ); // set hour, minute, second 1 void sethour( int ); // set hour 1 void setminute( int ); // set minute 1 void setsecond( int ); // set second // get functions 21 int gethour(); // return hour int getminute(); // return minute 23 int getsecond(); // return second Set functions. Get functions. 3 3 #endif 2 2

3 1 // Fig..19: time3.cpp 2 // Member-function definitions for Time class. using std::cout; #include <iomanip> 9 using std::setfill; using std::setw; // include definition of class Time from time3.h 13 #include "time3.h" 1 1 // constructor function to initialize private data; 1 // calls member function settime to set variables; 1 // default values are 0 (see class definition) 1 Time::Time( int hr, int min, int sec ) 19 { 20 settime( hr, min, sec ); 21 } // end Time constructor time3.cpp (1 of ) 13 2 // set hour, minute and second values 2 void Time::setTime( int h, int m, int s ) 2 { 2 sethour( h ); time3.cpp (2 of ) 2 setminute( m ); 29 setsecond( s ); 30 Call set functions to perform 31 } // end function settime validity checking. 33 // set hour value 3 void Time::setHour( int h ) 3 { 3 hour = ( h >= 0 && h < 2 )? h : 0; 3 3 } // end function sethour 39 Set functions perform validity 0 // set minute value checks before modifying data. 1 void Time::setMinute( int m ) 2 { 3 minute = ( m >= 0 && m < 0 )? m : 0; } // end function setminute 1 23 // set second value void Time::setSecond( int s ) 9 { 0 second = ( s >= 0 && s < 0 )? s : 0; 1 2 } // end function setsecond 3 // return hour value int Time::getHour() { return hour; 9 } // end function gethour 0 1 // return minute value 2 int Time::getMinute() 3 { return minute; } // end function getminute Set function performs validity checks before modifying data. Get functions allow client to read data. time3.cpp (3 of ) 1 // return second value 9 int Time::getSecond() 0 { 1 return second; 2 3 } // end function getsecond Get function allows client to read data. // print Time in universal format void Time::printUniversal() { cout << setfill( '0' ) << setw( 2 ) << hour << ":" 9 << setw( 2 ) << minute << ":" 0 << setw( 2 ) << second; 1 2 } // end function printuniversal 3 // print Time in standard format void Time::printStandard() { cout << ( ( hour == 0 hour == )? : hour % ) << ":" << setfill( '0' ) << setw( 2 ) << minute 9 << ":" << setw( 2 ) << second 90 << ( hour <? " AM" : " PM" ); time3.cpp ( of ) } // end function printstandard 1 // Fig..20: fig0_20.cpp 2 // Demonstrating the Time class set and get functions using std::cout; using std::endl; // include definition of class Time from time3.h 9 #include "time3.h" void incrementminutes( Time &, const int ); // prototype 13 int main() 1 { 1 Time t; // create Time object 1 1 // set time individual set functions using 1 t.sethour( 1 ); // set hour to valid value 19 t.setminute( 3 ); // set minute to valid value 20 t.setsecond( 2 ); // set second to valid value Invoke set functions to set valid values. fig0_20.cpp (1 of 3) 1 // use get functions to obtain hour, minute and second 23 cout << "Result of setting all valid values:\n" 2 << " Hour: " << t.gethour() Attempt to set invalid values 2 << " Minute: " << t.getminute() using set functions. fig0_20.cpp 2 << " Second: " << t.getsecond(); (2 of 3) 2 2 // set time using individual set functions 29 t.sethour( 23 ); // invalid hour set to 0 30 t.setminute( 3 ); // set minute to valid value 31 t.setsecond( 33 ); // invalid second set to 0 Invalid values result in setting data members to // display hour, minute and second after setting 3 // invalid hour and second values 3 cout << "\n\nresult of attempting to set invalid hour and" 3 << " second:\n Hour: " << t.gethour() Modify data members using 3 << " Minute: " << t.getminute() function settime. 3 << " Second: " << t.getsecond() << "\n\n"; 39 0 t.settime(,, 0 ); // set time 1 incrementminutes( t, 3 ); // increment t's minute by return 0; } // end main 3

4 // add specified number of minutes to a Time object void incrementminutes( Time &tt, const int count ) 9 { 0 cout << "Incrementing minute " << count 1 << " times:\nstart time: "; 2 tt.printstandard(); 3 for ( int i = 0; i < count; i++ ) { tt.setminute( ( tt.getminute() + 1 ) % 0 ); if ( tt.getminute() == 0 ) tt.sethour( ( tt.gethour() + 1 ) % 2); 9 0 cout << "\nminute + 1: "; 1 tt.printstandard(); fig0_20.cpp Using get functions (3 of to 3) read data and set functions to modify data. 19 Result of setting all valid values: Hour: 1 Minute: 3 Second: 2 Result of attempting to set invalid hour and second: Hour: 0 Minute: 3 Second: 0 Incrementing minute 3 times: Start time: ::00 AM Attempting to set data minute + 1: :9:00 AM minute + 1: :00:00 PM members with invalid values minute + 1: :01:00 PM results in error message and members set to 0. fig0_20.cpp output (1 of 1) } // end for cout << endl; } // end function incrementminutes.1 Default Memberwise Assignment Assigning objects Assignment operator (=) Can assign one object to another of same type Default: memberwise assignment Each right member assigned individually to left member Passing, returning objects Objects passed as function arguments Objects returned from functions Default: pass-by-value Copy of object passed, returned Copy constructor Copy original values into new object 21 1 // Fig..2: fig0_2.cpp 2 // Demonstrating that class objects can be assigned 3 // to each other using default memberwise assignment. #include <iostream> using std::cout; using std::endl; 9 // class Date definition class Date { public: 13 Date( int = 1, int = 1, int = 1990 ); // default constructor 1 void print(); 1 1 private: 1 int month; 1 int day; 19 int year; }; // end class Date fig0_2.cpp (1 of 3) 23 // Date constructor with no range checking 2 Date::Date( int m, int d, int y ) 2 { 2 month = m; 2 day = d; 2 year = y; } // end Date constructor 31 // print Date in the format mm-dd-yyyy 33 void Date::print() 3 { 3 cout << month << '-' << day << '-' << year; 3 3 } // end function print 3 39 int main() 0 { 1 Date date1(,, 2002 ); 2 Date date2; // date2 defaults to 1/1/1990 fig0_2.cpp (2 of 3) 23 cout << "date1 = "; date1.print(); cout << "\ndate2 = "; Default memberwise date2.print(); assignment assigns each 9 date2 = date1; member of date1 // default memberwise individually assignment to each member 0 1 cout << "\n\nafter of date2. default memberwise assignment, date2 = "; 2 date2.print(); 3 cout << endl; return 0; } // end main date1 = date2 = After default memberwise assignment, date2 = fig0_2.cpp (3 of 3) fig0_2.cpp output (1 of 1) 2 3

5 1.1 Introduction 2 Dynamic (Linked) Data Structures (Chapter 1) Fixed-size data structures Arrays, structs Dynamic data structures Grow and shrink as program runs Linked lists Insert/remove items anywhere Stacks Insert/remove from top of stack Queues Like a line, insert at back, remove from front Binary trees High-speed searching/sorting of data Self-Referential Classes Self-Referential Classes 2 Self-referential class Has pointer to object of same class Link together to form useful data structures Lists, stacks, queues, trees Terminated with NULL pointer 1 Sample code class Node { public: Node( int ); void setdata( int ); int getdata() const; void setnextptr( Node * ); const Node *getnextptr() const; private: int data; Node *nextptr; }; Data member and pointer NULL pointer (points to nothing) Pointer to object called a link nextptr points to a Node 1.3 Dynamic Memory Allocation and Data Structures Dynamic Memory Allocation and Data Structures 30 Dynamic memory allocation Obtain and release memory during program execution Create and remove nodes Operator new Takes type of object to create Returns pointer to newly created object Node *newptr = new Node( ); Returns bad_alloc if not enough memory is the node's object data Operator delete delete newptr; Deallocates memory allocated by new, calls destructor Memory returned to system, can be used in future newptr not deleted, only the space it points to

6 1. Linked Lists Linked Lists Linked list Collection of self-referential class objects (nodes) connected by pointers (links) Accessed using pointer to first node of list Subsequent nodes accessed using the links in each node Link in last node is null (zero) Indicates end of list Data stored dynamically Nodes created as necessary Node can have data of any type... H D Q 1. Linked Lists Linked Lists 3 Linked lists vs. arrays Arrays can become full Allocating "extra" space in array wasteful, may never be used Linked lists can grow/shrink as needed Linked lists only become full when system runs out of memory Linked lists can be maintained in sorted order Insert element at proper position Existing elements do not need to be moved Selected linked list operations Insert node at front Insert node at back Remove node from front Remove node from back In following illustrations List has and (a) is before, (b) is after Insert at front 3 Insert at back 3 a) a) newptr newptr b) b) newptr newptr = newptr If list empty, then = = newptr newptr->nextptr = ->nextptr = newptr = newptr If list empty, then = = newptr

7 a) Remove from front 3 Remove from back "Walk" list until get next-to-last node, until currentptr->nextptr = a) 3 b) b) currentptr tempptr = tempptr = ->next If there are no more nodes, = = 0 tempptr = tempptr = currentptr delete tempptr delete tempptr

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

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator CISC11 Introduction to Computer Science Dr. McCoy Lecture 1 October 29, 2009 Pointer Assignments Pointer variables can be "assigned": int *p1, *p2; p2 = p1; Assigns one pointer to another "Make p2 point

More information

Chapter 6: Classes and Data Abstraction

Chapter 6: Classes and Data Abstraction Chapter 6: Classes and Data Abstraction 1 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing Structure Members 6.4 Implementing a User-Defined Type Time with a struct 6.5 Implementing a Time Abstract

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Jan 20, 2004 Quiz 1 2 Average: about 3.8 More than half obtained: 4 + Highest is 8 Need more work/practice! Quiz 1

More information

57:017, Computers in Engineering C++ Classes

57:017, Computers in Engineering C++ Classes 57:017, Computers in Engineering C++ Classes Copyright 1992 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Introduction Object-oriented programming (OOP) Encapsulates

More information

Computer Programming with C++ (21)

Computer Programming with C++ (21) Computer Programming with C++ (21) Zhang, Xinyu Department of Computer Science and Engineering, Ewha Womans University, Seoul, Korea zhangxy@ewha.ac.kr Classes (III) Chapter 9.7 Chapter 10 Outline Destructors

More information

A Deeper Look at Classes

A Deeper Look at Classes A Deeper Look at Classes Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by

More information

Chapter 16: Classes and Data Abstraction

Chapter 16: Classes and Data Abstraction Chapter 16: Classes and Data Abstraction 16.1 Introduction 16.2 Implementing a Time Abstract Data Type with a Class 16.3 Class Scope and Accessing Class Members 16.4 Separating Interface from Implementation

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3: Classes May 24, 2004 2 Classes Structure Definitions 3 Structures Aggregate data types built using elements of other

More information

Classes: A Deeper Look

Classes: A Deeper Look Classes: A Deeper Look 1 Introduction Implementing a Time Abstract Data Type with a class Class Scope and Accessing Class Members Separating Interface from Implementation Controlling Access to Members

More information

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

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls 1 W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls 2 7.4 friend Functions and friend Classes friend function and friend classes Can access private

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 5: Classes September 14, 2004 Structure Definitions 2 Structures Aggregate data types built using elements of other types

More information

Classes: A Deeper Look. Systems Programming

Classes: A Deeper Look. Systems Programming Classes: A Deeper Look Systems Programming Deeper into C++ Classes const objects and const member functions Composition Friendship this pointer Dynamic memory management new and delete operators static

More information

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

9.1 Introduction. Integrated Time class case study Preprocessor wrapper Three types of handles on an object. Class functions 1 9 Classes: A Deeper Look, Part 1 OBJECTIVES In this chapter you ll learn: How to use a preprocessor p wrapper to prevent multiple definition errors caused by including more than one copy of a header

More information

Introduction to Programming session 24

Introduction to Programming session 24 Introduction to Programming session 24 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel sslides Sharif Universityof Technology Outlines Introduction

More information

Computer Programming Class Members 9 th Lecture

Computer Programming Class Members 9 th Lecture Computer Programming Class Members 9 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2015 Eom, Hyeonsang All Rights Reserved Outline Class

More information

Classes: A Deeper Look, Part 2

Classes: A Deeper Look, Part 2 10 But what, to serve our private ends, Forbids the cheating of our friends? Charles Churchill Instead of this absurd division into sexes they ought to class people as static and dynamic. Evelyn Waugh

More information

More C++ Classes. Systems Programming

More C++ Classes. Systems Programming More C++ Classes Systems Programming C++ Classes Preprocessor Wrapper Time Class Case Study Class Scope and Assessing Class Members Using handles to access class members Access and Utility Functions Destructors

More information

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

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 CHAPTER 7 CLASSES: PART II 1 Illustrations List (Main Page) 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 Using a Time class with const objects and const member

More information

Chapter 17 - C++ Classes: Part II

Chapter 17 - C++ Classes: Part II Chapter 17 - C++ Classes: Part II 17.1 Introduction 17.2 const (Constant) Objects and const Member Functions 17.3 Composition: Objects as Members of Classes 17.4 friend Functions and friend Classes 17.5

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Chapter 17 - C++ Classes: Part II 17.1 Introduction 17.2 const (Constant) Objects and const Member Functions 17.3 Composition: Objects as Members of Classes 17.4 friend

More information

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

Angela Chih-Wei Tang Visual Communications Lab Department of Communication Engineering National Central University JhongLi, Taiwan. C++ Classes: Part II Angela Chih-Wei Tang Visual Communications Lab Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 17.2 const (Constant) Objects and

More information

Fundamentals of Programming Session 24

Fundamentals of Programming Session 24 Fundamentals of Programming Session 24 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Classes and Data Abstraction. Topic 5

Classes and Data Abstraction. Topic 5 Classes and Data Abstraction Topic 5 Classes Class User Defined Data Type Object Variable Data Value Operations Member Functions Object Variable Data Value Operations Member Functions Object Variable Data

More information

Chapter 9 Classes : A Deeper Look, Part 1

Chapter 9 Classes : A Deeper Look, Part 1 Chapter 9 Classes : A Deeper Look, Part 1 C++, How to Program Deitel & Deitel Fall 2016 CISC1600 Yanjun Li 1 Time Class Case Study Time Class Definition: private data members: int hour; int minute; int

More information

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

Preview 11/1/2017. Constant Objects and Member Functions. Constant Objects and Member Functions. Constant Objects and Member Functions Preview Constant Objects and Constant Functions Composition: Objects as Members of a Class Friend functions The use of Friend Functions Some objects need to be modifiable and some do not. A programmer

More information

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

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++ 1 Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++ Objectives To be able to use Cygwin, a UNIX simulator. To be able to use a text editor to create C++ source files To be able to use GCC to compile

More information

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 1 9 Classes: A Deeper Look, Part 1 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition errors caused by including more than one copy of a header file

More information

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II 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

More information

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

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++ Preview Object Oriented Programming with C++ Class Members Inline Functions vs. Regular Functions Constructors & Destructors Initializing class Objects with Constructors C++ Programming Language C Programming

More information

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

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class. Classes and Objects Week 5 Gaddis:.2-.12 14.3-14.4 CS 5301 Fall 2016 Jill Seaman The Class! A class in C++ is similar to a structure.! A class contains members: - variables AND - functions (often called

More information

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

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class. Classes and Objects Week 5 Gaddis:.2-.12 14.3-14.4 CS 5301 Spring 2018 Jill Seaman The Class l A class in C++ is similar to a structure. l A class contains members: - variables AND - functions (often called

More information

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

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class. Classes and Objects Week 5 Gaddis:.2-.12 14.3-14.4 CS 5301 Spring 2015 Jill Seaman The Class! A class in C++ is similar to a structure.! A class contains members: - variables AND - functions (often called

More information

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

clarity. In the first form, the access specifier public : is needed to { Structure Time CSC 211 Intermediate Programming Classes in C++ // Create a structure, set its members, and print it. struct Time // structure definition int hour; // 0-23 int minute; // 0-59 int second;

More information

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

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver. Classes and Objects Week 5 Gaddis:.2-.12 14.3-14.4 CS 5301 Fall 2014 Jill Seaman The Class A class in C++ is similar to a structure. A class contains members: - variables AND - functions (often called

More information

! 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 Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Spring 2013 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

! 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 Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Fall 2012 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

Classes and Data Abstraction. Topic 5

Classes and Data Abstraction. Topic 5 Classes and Data Abstraction Topic 5 Introduction Object-oriented programming (OOP) Encapsulates data (attributes) and functions (behavior) into packages called classes The data and functions of a class

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Intro to Data Structures Vectors Linked Lists Queues Stacks C++ has some built-in methods of storing compound data in useful ways, like arrays and structs.

More information

Chapter 7: Classes Part II

Chapter 7: Classes Part II 1 Chapter 7: Classes Part II 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions 7.3 Composition: Objects a s Members of Cla sses 7.4 friend Func tions a nd friend Cla sse s 7.5 Using

More information

! 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 Ch 13: Introduction to Classes 13.1 Procedural Programming Data is stored in variables CS 2308 Spring 2014 Jill Seaman - Perhaps using arrays and structs. Program is a collection of functions that perform

More information

Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü

Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü 1 C LECTURE 12 ++ Data Structures 2 Data Structures 17.1

More information

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

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 Introduction to Classes 13.2 The Class Unit 4 Chapter 13 CS 2308 Fall 2016 Jill Seaman 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

More information

! 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 Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Spring 2015 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

Classes and Data Abstraction

Classes and Data Abstraction 6 Classes and Data Abstraction Objectives To understand the software engineering concepts of encapsulation and data hiding. To understand the notions of data abstraction and abstract data types (ADTs).

More information

! 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 Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Fall 2013 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

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

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 Introduction to Classes 13.2 The Class Unit 4 Chapter 13 CS 2308 Spring 2017 Jill Seaman 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

More information

CS 1337 Computer Science II Page 1

CS 1337 Computer Science II Page 1 Source File: ~/1337/65/lab65.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 3 The purpose of this assignment is to add to the implementation

More information

Lecture 5. Function Pointers

Lecture 5. Function Pointers Lecture 5 Pointers to functions Complicated declarations Allocating and deallocating memory Classes const member functions Constructors and destructor this pointer static members Templates Lec 5 Programming

More information

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

Classes and Objects. Types and Classes. Example. Object Oriented Programming Classes and Objects Types and Classes Chapter 6 Object Oriented program development. Read this Chapter 7 Classes Read this Chapter 8 More About Classes Some sections will be assigned later Chapter 9 Inheritance.

More information

1 OBJECT-BASED PROGRAMMING CHAPTER 8

1 OBJECT-BASED PROGRAMMING CHAPTER 8 1 OBJECT-BASED PROGRAMMING CHAPTER 8 8 Object-Based Programming 1 // Fig. 8.1: Time1.java 2 // Time1 class definition 3 import java.text.decimalformat; // used for number formatting 4 5 // This class maintains

More information

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

COMP26120: Linked List in C (2018/19) Lucas Cordeiro COMP26120: Linked List in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Linked List Lucas Cordeiro (Formal Methods Group) lucas.cordeiro@manchester.ac.uk Office: 2.28 Office hours: 10-11 Tuesday,

More information

Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look 1 8 Classes and Objects: A Deeper Look 1 // Fig. 8.1: Time1.java 2 // Time1 class declaration maintains the time in 24-hour format. 4 public class Time1 6 private int hour; // 0 2 7 private int minute;

More information

Fundamentals of Programming Session 25

Fundamentals of Programming Session 25 Fundamentals of Programming Session 25 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

W8.2 Operator Overloading

W8.2 Operator Overloading 1 W8.2 Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream Insertion and Extraction

More information

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer this Pointer, Constant Functions, Static Data Members, and Static Member Functions 3/2/07 CS250 Introduction to Computer Science II 1 this Pointer (11.1) functions - only one copy of each function exists

More information

Introduction. W8.2 Operator Overloading

Introduction. W8.2 Operator Overloading W8.2 Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream Insertion and Extraction

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism 1 Inheritance extending a clock to an alarm clock deriving a class 2 Polymorphism virtual functions and polymorphism abstract classes MCS 360 Lecture 8 Introduction to Data

More information

Chapter 18 - C++ Operator Overloading

Chapter 18 - C++ Operator Overloading Chapter 18 - C++ Operator Overloading Outline 18.1 Introduction 18.2 Fundamentals of Operator Overloading 18.3 Restrictions on Operator Overloading 18.4 Operator Functions as Class Members vs. as friend

More information

Operator Overloading

Operator Overloading Operator Overloading Introduction Operator overloading Enabling C++ s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is

More information

Operator Overloading in C++ Systems Programming

Operator Overloading in C++ Systems Programming Operator Overloading in C++ Systems Programming Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. Global Functions Overloading

More information

Pointers and Strings. Adhi Harmoko S, M.Komp

Pointers and Strings. Adhi Harmoko S, M.Komp Pointers and Strings Adhi Harmoko S, M.Komp Introduction Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings Pointer Variable Declarations and

More information

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pointers and Strings Prentice Hall, Inc. All rights reserved. Pointers and Strings 1 Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using const with Pointers Selection Sort Using Pass-by-Reference 2

More information

Operator Overloading

Operator Overloading Operator Overloading Introduction Operator overloading Enabling C++ s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is

More information

Cpt S 122 Data Structures. Data Structures

Cpt S 122 Data Structures. Data Structures Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following

More information

Operator Overloading

Operator Overloading Steven Zeil November 4, 2013 Contents 1 Operators 2 1.1 Operators are Functions.... 2 2 I/O Operators 4 3 Comparison Operators 6 3.1 Equality Operators....... 11 3.2 Less-Than Operators...... 13 1 1 Operators

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Templates Lecture 10 March 23, 2004 Introduction 2 Templates Function templates Specify entire range of related (overloaded) functions Function-template specializations

More information

Object-Based Programming (Deitel chapter 8)

Object-Based Programming (Deitel chapter 8) Object-Based Programming (Deitel chapter 8) 1 Plan 2 Introduction Implementing a Time Abstract Data Type with a Class Class Scope Controlling Access to Members Referring to the Current Object s Members

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Spring 2013 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. Outline Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. 1 Arrays I Array One type of data structures. Consecutive group of memory locations

More information

Encapsulation. Contents. Steven Zeil. July 17, Encapsulation Encapsulation in C Classes 4. 3 Hiding Attributes 8

Encapsulation. Contents. Steven Zeil. July 17, Encapsulation Encapsulation in C Classes 4. 3 Hiding Attributes 8 Steven Zeil July 17, 2013 Contents 1 Encapsulation 2 1.1 Encapsulation in C++........................................................ 2 2 Classes 4 3 Hiding Attributes 8 4 Inline Functions 11 4.1 How Functions

More information

2. It is possible for a structure variable to be a member of another structure variable.

2. It is possible for a structure variable to be a member of another structure variable. FORM 1(put name, form, and section number on scantron!!!) CS 162 Exam I True (A) / False (B) (2 pts) 1. What value will the function eof return if there are more characters to be read in the input stream?

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

Arrays. Week 4. Assylbek Jumagaliyev

Arrays. Week 4. Assylbek Jumagaliyev Arrays Week 4 Assylbek Jumagaliyev a.jumagaliyev@iitu.kz Introduction Arrays Structures of related data items Static entity (same size throughout program) A few types Pointer-based arrays (C-like) Arrays

More information

Object-Based Programming

Object-Based Programming Object-Based Programming 8.1 Introduction 8.2 Implementing a Time Abstract Data Type with a Class 8.3 Class Scope 8.4 Controlling Access to Members 8.5 Creating Packages 8.6 Initializing Class Objects:

More information

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1 C How to Program, 6/e 1 Structures : Aggregate data types are built using elements of other types struct Time { int hour; int minute; Members of the same structure must have unique names. Two different

More information

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

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm G205 Fundamentals of Computer Engineering CLASS 3, Wed. Sept. 15 2004 Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm Function Calls A function is called via its name Actual parameters are passed for the

More information

Introduction & Review

Introduction & Review CPSC 250 Data Structures Introduction & Review Dr. Yingwu Zhu What to learn? ADT design & implementation using C++ class Algorithm efficiency analysis Big-O ADTs: Binary search trees, AVL trees, Heaps,

More information

Introduction to Data Structures. Systems Programming

Introduction to Data Structures. Systems Programming Introduction to Data Structures Systems Programming Intro to Data Structures Self-referential Structures Dynamic Memory Allocation A Simple malloc Example Linear Lists Linked Lists Insertion Example Linked

More information

Introduction to C++ Introduction to C++ 1

Introduction to C++ Introduction to C++ 1 1 What Is C++? (Mostly) an extension of C to include: Classes Templates Inheritance and Multiple Inheritance Function and Operator Overloading New (and better) Standard Library References and Reference

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Multiple Inheritance July 26, 2004 22.9 Multiple Inheritance 2 Multiple inheritance Derived class has several base classes Powerful,

More information

Classes and Objects: A Deeper Look

Classes and Objects: A Deeper Look 8 Instead of this absurd division into sexes, they ought to class people as static and dynamic. Evelyn Waugh Is it a world to hide virtues in? William Shakespeare But what, to serve our private ends, Forbids

More information

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

More information

CIS 190: C/C++ Programming. Classes in C++

CIS 190: C/C++ Programming. Classes in C++ CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 Introduction 2 IS 0020 Program Design and Software Tools Exception Handling Lecture 12 November 23, 200 Exceptions Indicates problem occurred in program Not common An "exception" to a program that usually

More information

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil More About Classes Gaddis Ch. 14, 13.3 CS 2308 :: Fall 2015 Molly O'Neil Pointers to Objects Just like pointers to structures, we can define pointers to objects Time t1(12, 20, true); Time *tptr; tptr

More information

Chapter 12 - Templates

Chapter 12 - Templates Chapter 12 - Templates O utline 12.1 Introd uction 12.2 Function Te m plate s 12.3 Ove rload ing Te m plate Functions 12.4 Class Te m p late s 12.5 Class Te m plate s and Non-type Param e te rs 12.6 Te

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

An inline function is one in which the function code replaces the function call directly. Inline class member functions

An inline function is one in which the function code replaces the function call directly. Inline class member functions Inline Functions An inline function is one in which the function code replaces the function call directly. Inline class member functions if they are defined as part of the class definition, implicit if

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Storage Classes Scope Rules Functions with Empty Parameter Lists Inline Functions References and Reference Parameters Default Arguments Unary Scope Resolution Operator Function

More information

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples. Outline Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples. 1 Introduction A pointer is a variable that contains a memory address Pointers

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 14 for ENCM 339 Fall 2015 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2015 SN s ENCM 339 Fall 2015 Slide Set 14 slide

More information

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack 1 12 C Data Structures 12.5 Stacks 2 Stack New nodes can be added and removed only at the top Similar to a pile of dishes Last-in, first-out (LIFO) Bottom of stack indicated by a link member to NULL Constrained

More information

Software Development With Java CSCI

Software Development With Java CSCI Software Development With Java CSCI-3134-01 D R. R A J S I N G H Outline Week 8 Controlling Access to Members this Reference Default and No-Argument Constructors Set and Get Methods Composition, Enumerations

More information

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

Practice Problems CS2620 Advanced Programming, Spring 2003

Practice Problems CS2620 Advanced Programming, Spring 2003 Practice Problems CS2620 Advanced Programming, Spring 2003 April 9, 2003 1. Explain the results of each vector definition: string pals[] = "pooh", "tigger", "piglet", "eeyore", "kanga" a) vector

More information

ADT: Design & Implementation

ADT: Design & Implementation CPSC 250 Data Structures ADT: Design & Implementation Dr. Yingwu Zhu Abstract Data Type (ADT) ADT = data items + operations on the data Design of ADT Determine data members & operations Implementation

More information

Constructor - example

Constructor - example Constructors A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class name. The constructor is invoked whenever

More information