A Deeper Look at Classes

Size: px
Start display at page:

Download "A Deeper Look at Classes"

Transcription

1 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 Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) 1

2 const Objects & const Member Functions Principle of Least Privilege Allow access to data only when absolutely needed Fundamental principle of good software engineering const objects Keyword const Specifies that an object is not modifiable Attempts to modify const object compilation errors Example const Time noon (12, 0, 0); 2

3 const Member Functions Only const member functions can be called for const objects Member functions declared const may not modify the object in any way A function is specified as const both prototype and definition. Constructors and destructors are not const By definition! 3

4 const Example 1 // Fig. 21.1: Time.h 2 // Definition of class Time. 3 // Member functions defined in Time.cpp. 4 #ifndef TIME_H 5 #define TIME_H 6 7 class Time 8 { 9 public: 10 Time( int = 0, int = 0, int = 0 ); // default constructor // set functions 13 void settime( int, int, int ); // set time 14 void sethour( int ); // set hour 15 void setminute( int ); // set minute 16 void setsecond( int ); // set second // get functions (normally declared const) 19 int gethour() const; // return hour 20 int getminute() const; // return minute 21 int getsecond() const; // return second A promise that these functions do not modify the object at all 4

5 const Example (continued) // print functions (normally declared const) 24 void printuniversal() const; // print universal time 25 void printstandard(); // print standard time (should be const) 26 private: 27 int hour; // 0-23 (24-hour clock format) 28 int minute; // int second; // }; // end class Time #endif 5

6 const Example (continued) 1 // Fig. 21.2: Time.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> 4 using std::cout; 5 6 #include <iomanip> 7 using std::setfill; 8 using std::setw; 9 10 #include "Time.h" // include definition of class Time // constructor function to initialize private data; 13 // calls member function settime to set variables; 14 // default values are 0 (see class definition) 15 Time::Time( int hour, int minute, int second ) 16 { 17 settime( hour, minute, second ); 18 } // end Time constructor // set hour, minute and second values 21 void Time::setTime( int hour, int minute, int second ) 22 { 23 sethour( hour ); 24 setminute( minute ); 25 setsecond( second ); 26 } // end function settime 6

7 const Example (continued) // set hour value 29 void Time::setHour( int h ) 30 { 31 hour = ( h >= 0 && h < 24 )? h : 0; // validate hour 32 } // end function sethour // set minute value 35 void Time::setMinute( int m ) 36 { 37 minute = ( m >= 0 && m < 60 )? m : 0; // validate minute 38 } // end function setminute // set second value 41 void Time::setSecond( int s ) 42 { 43 second = ( s >= 0 && s < 60 )? s : 0; // validate second 44 } // end function setsecond // return hour value 47 int Time::getHour() const // get functions should be const 48 { 49 return hour; 50 } // end function gethour const keyword in function definition, as well as in function prototype 7

8 const Example (continued) // return minute value 53 int Time::getMinute() const 54 { 55 return minute; 56 } // end function getminute // return second value 59 int Time::getSecond() const 60 { 61 return second; 62 } // end function getsecond // print Time in universal-time format (HH:MM:SS) 65 void Time::printUniversal() const 66 { 67 cout << setfill( '0' ) << setw( 2 ) << hour << ":" 68 << setw( 2 ) << minute << ":" << setw( 2 ) << second; 69 } // end function printuniversal // print Time in standard-time format (HH:MM:SS AM or PM) 72 void Time::printStandard() // note lack of const declaration 73 { 74 cout << ( ( hour == 0 hour == 12 )? 12 : hour % 12 ) 75 << ":" << setfill( '0' ) << setw( 2 ) << minute 76 << ":" << setw( 2 ) << second << ( hour < 12? " AM" : " PM" ); 77 } // end function printstandard 8

9 Purpose of const To enlist the aid of the compiler in detecting unwanted changes to objects Widely used in large programming projects Helps maintain sanity, teamwork, etc. Get used to using const everywhere! For methods, parameters, etc. 9

10 Questions? 10

11 Default Memberwise Assignment Absolute C++, 8.3 Assignment operator (=) Can be used to assign an object to another object of the same type. Each data member of the right object is assigned to the same data member in the left object. Not usually want you want to do! Usually causes serious problems when data members contain pointers to dynamically allocated memory!! Because pointers are simply copied 11

12 Default Memberwise Assignment Example 1 // Fig : Date.h 2 // Declaration of class Date. 3 // Member functions are defined in Date.cpp 4 5 // prevent multiple inclusions of header file 6 #ifndef DATE_H 7 #define DATE_H 8 9 // class Date definition 10 class Date 11 { 12 public: 13 Date( int = 1, int = 1, int = 2000 ); // default constructor 14 void print(); 15 private: 16 int month; 17 int day; 18 int year; 19 }; // end class Date #endif Default initialization of data members 12

13 Default Memberwise Assignment (continued) 1 // Fig : fig20_19.cpp 2 // Demonstrating that class objects can be assigned 3 // to each other using default memberwise assignment. 4 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 #include "Date.h" // include definition of class Date from Date.h 9 10 int main() 11 { 12 Date date1( 7, 4, 2004 ); 13 Date date2; // date2 defaults to 1/1/ cout << "date1 = "; 16 date1.print(); 17 cout << "\ndate2 = "; 18 date2.print(); date2 = date1; // default memberwise assignment cout << "\n\nafter default memberwise assignment, date2 = "; 23 date2.print(); 24 cout << endl; 25 return 0; 26 } // end main date1 = 7/4/2004 date2 = 1/1/2000 After default memberwise assignment, date2 = 7/4/2004 memberwise assignment assigns data members of date1 to date2 date2 now stores the same date as date1 14

14 But What happens if we use the default assignment operator on TreeNode? class TreeNode { public:... private: const string word; int count; TreeNode *left, *right; }; // class TreeNode 15

15 Default Memberwise Assignment (concluded) Many classes must provide their own assignment operator To intelligently assign one object to another Need to overload the = operator 16

16 Questions? 17

17 Copy Constructor Definition A constructor that has a single parameter of the form ClassName(ClassName &objecttobecopied); Normally A constructor that has a single constant parameter of the form ClassName(const ClassName &objecttobecopied); 18

18 Purpose of Copy Constructor To make a new object just like the previous object i.e., to make a copy of the previous object Intelligently E.g., string S1("Now is the time "); S1 contains an internal array of char string S2(S1); S2 now contains its own, separate internal array of char 19

19 Copy Constructors are used widely in programs are used implicitly by compiler whenever objects are passed by value! Passing objects to parameters Returning results from functions Initializing uninitialized memory from previous values 20

20 Copy Constructor A constructor that copies another object of the same type e.g.: class TreeNode { public:... /* other methods */ TreeNode(const TreeNode &nodetobecopied); // Copy Constructor private: const string word; int count; TreeNode *left, *right; }; 21

21 Default Copy Constructor Compiler provides a default copy constructor Copies each member of original object into corresponding member of new object i.e., memberwise assignment Enables pass-by-value for objects Used to copy original object s values into new object to be passed to a function or returned from a function Not usually want you want to do! Often causes serious problems when data members contain pointers to dynamically allocated memory!! Just like default memberwise assignment 22

22 Copy Constructor Many classes must provide an explicit Copy Constructor To intelligently copy one object to another Example: string(const string &stringtobecopied); Allocates new memory for the new string Copies characters from one to other Does not blindly copy members (including internal pointers, etc.) 23

23 Usage of Copy Constructor In Initializer list E.g.: TreeNode::TreeNode(const string &newword) :word(newword), //initialize word count(1), //initialize count left(null), right(null) { /* rest of constructor body */ } // TreeNode constructor 24

24 Questions? 25

25 The this Pointer Member functions know which object s data members to manipulate Every object has access to own address through a pointer called this (a C++ keyword) Object s this pointer is not part of object itself this pointer is passed (by the compiler) as an implicit argument to each of object s non-static member functions CS-2303, A-Term 2010 A Deeper Look at Classes 26

26 Using the this Pointer Objects may use the this pointer implicitly or explicitly this is used implicitly when accessing members directly It is used explicitly when using keyword this Type of the this pointer depends on type of the object whether member function is declared const CS-2303, A-Term 2010 A Deeper Look at Classes 27

27 Example using this 1 // Fig : fig21_17.cpp 2 // Using the this pointer to refer to object members. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 class Test 8 { 9 public: 10 Test( int = 0 ); // default constructor 11 void print() const; 12 private: 13 int x; 14 }; // end class Test // constructor 17 Test::Test( int value ) 18 : x( value ) // initialize x to value 19 { 20 // empty body 21 } // end constructor Test CS-2303, A-Term 2010 A Deeper Look at Classes 28

28 Example using this (continued) // print x using implicit and explicit this pointers; 24 // the parentheses around *this are required 25 void Test::print() const 26 { 27 // implicitly use the this pointer to access the member x 28 cout << " x = " << x; // explicitly use the this pointer and the arrow operator 31 // to access the member x 32 cout << "\n this->x = " << this->x; // explicitly use the dereferenced this pointer and 35 // the dot operator to access the member x 36 cout << "\n(*this).x = " << ( *this ).x << endl; 37 } // end function print int main() 40 { 41 Test testobject( 12 ); // instantiate and initialize testobject testobject.print(); 44 return 0; 45 } // end main x = 12 this->x = 12 (*this).x = 12 Directly accessing member x Explicitly using the this pointer to access member x Using the dereferenced this pointer and the dot operator CS-2303, A-Term 2010 A Deeper Look at Classes 29

29 Another Example Using this class ExtendTreeNode { public:... /* other methods */ ExtendTreeNode(const string &newword, ExtendTreeNode *parent); //constructor private: const string word; int count; ExtendTreeNode *left, *right ExtendTreeNode *const myparent; }; CS-2303, A-Term 2010 A Deeper Look at Classes 30

30 Example Using this (continued) ExtendTreeNode::ExtendTreeNode(const string &newword, ExtendTreeNode *parent) :word(newword), //initialize word count(1), //initialize count left(null), right(null), myparent(parent) //point back to myparent { /* rest of constructor body */ } // ExtendTreeNode constructor CS-2303, A-Term 2010 A Deeper Look at Classes 31

31 Example Using this (continued) ExtendTreeNode *ExtendTreeNode::AddNode(const string &newword){ if (newword == word){ count++; return this; } else if (newword < word) { if (left) return left->addnode(newword); else return left = new ExtendTreeNode(newWord, this); } else { /* same for right */ } } // AddNode Constructor of ExtendTreeNode with pointer back to parent node! CS-2303, A-Term 2010 A Deeper Look at Classes 32

32 Cascaded member-function calls Multiple functions are invoked in the same statement. Enabled by member functions returning the dereferenced this pointer. Example t.setminute( 30 ).setsecond( 22 ); Calls t.setminute( 30 ) and returns reference to t Then calls t.setsecond( 22 ); CS-2303, A-Term 2010 A Deeper Look at Classes 33

33 Cascaded calls (continued) Absolute C++, 8.3 (end) Instead of void setminute (const int ); Use Time& setminute (const int ); Implementation Time& Time::setMinute (const int m){ minute = m; return *this; } CS-2303, A-Term 2010 A Deeper Look at Classes 34

34 Reference Result A function may return a reference to an object (instead of a value) Value copy of an object Reference object itself Reference result must not be automatic variable Compiler checks Reference result may be used wherever an object of same type/class may be used Subject to const rules Especially useful for overloaded operators CS-2303, A-Term 2010 A Deeper Look at Classes 35

35 Questions? CS-2303, A-Term 2010 A Deeper Look at Classes 36

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

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

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>

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> CISC11 Introduction to Computer Science Dr. McCoy Lecture 20 November, 2009. Using Default Arguments with Constructors Constructors Can specify default arguments Default constructors Defaults all arguments

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Classes, Constructors, etc., in C++

Classes, Constructors, etc., in C++ Classes, Constructors, etc., in C++ 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

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

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

! 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

! 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

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts Templates (again) 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 Walter

More information

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2

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

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 2014 Jill Seaman - Perhaps using arrays and structs. Program is a collection of functions that perform

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

! 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

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 2015 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

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

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

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

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

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

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

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

Your first C and C++ programs

Your first C and C++ programs Your first C and C++ programs 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++,

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

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

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts Strings and Streams 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 Walter

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ 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 Walter

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

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

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

Exception Handling in C++

Exception Handling in C++ Exception Handling in C++ 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

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

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

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

Programming Assignment #4 Binary Trees in C++

Programming Assignment #4 Binary Trees in C++ Programming Assignment #4 Binary Trees in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie,

More information

Derived Classes in C++

Derived Classes in C++ Derived Classes in C++ 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 Walter

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

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

Iterators. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Iterators. Professor Hugh C. Lauer CS-2303, System Programming Concepts Iterators 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 Walter Savitch,

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

Linked Lists in C and C++

Linked Lists in C and C++ Linked Lists in C and C++ 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

Containers and the Standard Template Library (STL)

Containers and the Standard Template Library (STL) Containers and the Standard Template Library (STL) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and

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

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

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

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

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

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

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

C How to Program, 6/e, 7/e

C How to Program, 6/e, 7/e C How to Program, 6/e, 7/e prepared by SENEM KUMOVA METİN modified by UFUK ÇELİKKAN and ILKER KORKMAZ The textbook s contents are also used 1992-2010 by Pearson Education, Inc. All Rights Reserved. Two

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

Classes and Objects: A Deeper Look

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

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

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

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

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

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

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

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Review: C++ Basic Concepts. Dr. Yingwu Zhu Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

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 www.thestudycampus.com Classes and Objects:A Deeper Look O b j e c t i v e s In this chapter you ll learn: Encapsulation and data hiding. To use keywordthis. To usestatic variables and methods. To importstatic

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

SFU CMPT Topic: Classes

SFU CMPT Topic: Classes SFU CMPT-212 2008-1 1 Topic: Classes SFU CMPT-212 2008-1 Topic: Classes Ján Maňuch E-mail: jmanuch@sfu.ca Friday 15 th February, 2008 SFU CMPT-212 2008-1 2 Topic: Classes Encapsulation Using global variables

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

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

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

C++ As A Better C Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan. C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2013 Fall Outline 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.5

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

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

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

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Dynamic Memory Allocation (and Multi-Dimensional Arrays) Dynamic Memory Allocation (and Multi-Dimensional Arrays) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan

More information