Starting Out with C++, 3 rd Edition 1 Chapter 14: more about classes Static Members If a member variable is declared static, all objects of that class.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Operator Overloading Fundamentals
Class and Objects.
Chapter 14 More About Classes. CS SJAllan Chapter 14 2 Instance and Static Members Instance variable:  A member variable in a class  Each object.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 11 More.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
More About Classes. C++: Classes & Objects -2 2 Instance and Static Members Each instance of a class has its own copies of the class’s instance (member)
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
Chapter 8 Operator Overloading, Friends, and References.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
CS Object Oriented Programming Using C++
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
OPERATOR OVERLOADING WEEK 4-5 CHAPTER 19. class Money {private:int lira; int kurus; public: Money() {}; Money(int l, int k) { lira=l+ k/100; kurus=k%100;
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 5 1.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 CSE 2341 Object Oriented Programming with C++ Note Set #7.
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Yan Shi CS/SE 2630 Lecture Notes
Chapter 14: More About Classes.
Operator Overloading; String and Array Objects
Overloading Operator MySting Example
Object-Oriented Design (OOD) and C++
Constructor & Destructor
Chapter 14: More About Classes.
Inheritance Using work from:
Operator Overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
9-10 Classes: A Deeper Look.
Operator Overloading; String and Array Objects
Chapter 18 - Operator Overloading
Chapter 14 – More About Classes
9-10 Classes: A Deeper Look.
Presentation transcript:

Starting Out with C++, 3 rd Edition 1 Chapter 14: more about classes Static Members If a member variable is declared static, all objects of that class have access to a single instance of that variable. If a member function is declared static, it may be called before any instances of the class are defined and it may only use static variables.

Starting Out with C++, 3 rd Edition 2 Member Variable is Static int Y; static int X;

Starting Out with C++, 3 rd Edition 3 budget.h – Program #ifndef BUDGET_H #define BUDGET_H class Budget {private: static float corpBudget; float divBudget; public: Budget ( void ) { divBudget = 0; } void addBudget ( float b ) { divBudget += b; corpBudget += divBudget; } float getDivBudget ( void ) { return divBudget; } float getCorpBudget ( void ) { return corpBudget; } }; // Budget #endif budget.cpp – odd way to initialize float Budget::corpBudget = ;

Starting Out with C++, 3 rd Edition 4 Static Member Functions The syntax for a static member function is: static ( ); –A class’s static member variables come into existence before any instances of the class are created. –The static member functions use only static variables and are callable before any instances of the class are created..

Starting Out with C++, 3 rd Edition 5 A New budget.h – Program #ifndef BUDGET_H #define BUDGET_H class Budget { private: static float corpBudget; float divBudget; public: … static void mainOffice ( float ); }; // Budget #endif

Starting Out with C++, 3 rd Edition 6 budget.cpp – Program #include "budget.h" float Budget::corpBudget = 0; void Budget::mainOffice ( float moffice ) {corpBudget += moffice; } // Budget::mainOffice

Starting Out with C++, 3 rd Edition 7 Using budget.h – Program #include #include "budget.h" void main ( void ) {float amount; cout << "Enter the main office's budget request: "; cin >> amount; Budget::mainOffice(amount); …

Starting Out with C++, 3 rd Edition 8 Friends of Classes A friend is a function that is not a member of a class, but has access to the private members of the class. The syntax of a friend function is as follows and is included in the.h file for the class: friend ( );

Starting Out with C++, 3 rd Edition 9 auxil.h – Program #ifndef AUXIL_H #define AUXIL_H class Budget; // Forward declaration of Budget class class Aux { private: float auxBudget; public: Aux ( void ) { auxBudget = 0; } void addBudget ( float, Budget & ); float getDivBudget ( void ) { return auxBudget; } }; // Aux #endif

Starting Out with C++, 3 rd Edition 10 budget.h – Program #ifndef BUDGET_H #define BUDGET_H #include "auxil.h" class Budget { private: static float corpBudget; float divBudget; public: Budget ( void ) { divBudget = 0; } void addBudget ( float b ) { divBudget += b; corpBudget += divBudget; } float getDivBudget ( void ) { return divBudget; } float getCorpBudget ( void ) { return corpBudget; } static void mainOffice ( float ); friend void Aux::addBudget ( float, Budget & ); }; // Budget #endif

Starting Out with C++, 3 rd Edition 11 Friend Classes As mentioned before, it is possible to make an entire class a friend of another class. The Budget class could make the Aux class its friend with the following declaration: friend class Aux; Note – generally a “friend” is bad as it violates the encapsulation principle.

Starting Out with C++, 3 rd Edition 12 Memberwise Assignment The = operator may be used to assign one object to another, or to initialize one object with another object’s data. By default, each member of one object is copied to its counterpart in the other object.

Starting Out with C++, 3 rd Edition 13 Copy Constructors A copy constructor is a special constructor, called whenever a new object is created and initialized with another object’s data. Assume the class Set has a member variable as follows: bool *member which is dynamically allocated. Consider the following declarations: –Set x(5,15); // 15 possible members, 5 is a member –Set y = x; // Copy constructor is called but – is only a shallow copy, so when set y changes, set x does to

Starting Out with C++, 3 rd Edition 14 The Default Copy Constructor If a class doesn’t have a copy constructor, C++ automatically creates a default copy constructor. The default copy constructor performs a memberwise assignment.

Starting Out with C++, 3 rd Edition 15 The this Pointer *this is a special built-in pointer that is available in any member function. *this contains the address of the object that called the member function. It is the name for ME.

Starting Out with C++, 3 rd Edition 16 Operator Overloading C++ allows you to redefine how standard operators work when used with class objects.

Starting Out with C++, 3 rd Edition 17 Issues of Operator Overloading You can change an operator’s entire meaning when you overload it. (But don’t.) You cannot change the number of operands taken by an operator. For example, the + symbol must always be a binary operator. Likewise, ++ and -- must always be unary operators.

Starting Out with C++, 3 rd Edition 18 Operators That can be Overloaded

Starting Out with C++, 3 rd Edition 19 Because our unionSet has same arguments and return value as +, we can simply make a name change: Set & Set::operator+(Set & arg2){ Set *res; int min; if(memberMax>arg2.memberMax){ min=arg2.memberMax; res = new Set(*this); } else { res = new Set(arg2); min = memberMax; } for (int i=0; i < min; i++) res->member[i]= arg2.member[i] || member[i]; return *res; };

Starting Out with C++, 3 rd Edition 20 Overloading the Prefix ++ Operator Set & Set::operator++(void){ bool * member1 = new bool[memberMax+1]; for (int i=0; i < memberMax; i++) member1[i] = member[i]; delete [] member; member = member1; member[memberMax]=false; memberMax++; return *this; }

Starting Out with C++, 3 rd Edition 21 Overloading the Postfix ++ Operator Set &Set::operator++(int x){// parameter is never used! bool * member1 = new bool[memberMax+1]; for (int i=0; i < memberMax; i++) member1[i] = member[i]; delete [] member; member = member1; member[memberMax]=false; memberMax++; return *this; }

Starting Out with C++, 3 rd Edition 22 Overloading Relational Operators bool Set::operator<(Set & arg2){// subset for us int min = memberMax; if (arg2.memberMax < min) min = arg2.memberMax; for(int i=0; i <min;i++) if (member[i] && !arg2.member[i]) return false; if (memberMax >min) for (i=min; i < memberMax;i++) if (member[i]) return false; return true; }

Starting Out with C++, 3 rd Edition 23 Overloading the == Operator bool Set::operator==(Set & arg2){ if (memberMax!=arg2.memberMax) return false; for (int i=0; i < memberMax;i++) if (member[i]!=arg2.member[i]) return false; return true; }

Starting Out with C++, 3 rd Edition 24 Overloading the << Operator Note – this cannot be a member function of Set as the first argument is not a Set ostream &operator<< ( ostream &strm, Set &obj ) { strm << “Whatever I want”; strm<< “ – but be careful about member variables”; strm << “Since I’m not a member, I can’t access them”; } // operator<<

Starting Out with C++, 3 rd Edition 25 Overloading the >> Operator istream &operator>> ( istream &strm, Set &obj ) {int ele; strm >> ele; Set.addToSet(x); //or make friend or use input() member } // operator>>

Starting Out with C++, 3 rd Edition 26 Overloading the [] Operator In addition to the traditional operators, C++ allows you to change the way the [] (subscript) symbols work.

Starting Out with C++, 3 rd Edition 27 intarry.h – Program #ifndef INTARRY_H #define INTARRY_H Class IntArray { private: int *aptr; int arraySize; void memError ( void ); void subError ( void ); public: IntArray ( int ); IntArray ( const IntArray & ); ~IntArray ( void ); int size ( void ) { return arraySize }; int &operator[] ( const int & ); }; // IntArray #endif

Starting Out with C++, 3 rd Edition 28 intarray.cpp – Program IntArray::IntArray ( int s ) { arraySize = s; aptr = new int [s]; if ( aptr == NULL ) memError(); for ( int count = 0; count < arraySize; count++ ) *( aptr + count ) = 0; } // IntArray::IntArray IntArray::IntArray ( const IntArray &obj ) { arraySize = obj.arraySize; aptr = new int [arraySize]; if ( aptr == NULL ) memError(); for ( count = 0; count < arraySize; count ++ ) *( aptr + count ) = *( obj.aptr + count ); } // IntArray::IntArray

Starting Out with C++, 3 rd Edition 29 intarray.cpp – Program (cont) IntArray::~IntArray ( void ) { if ( arraySize > 0 ) delete [] aptr; } // IntArray::~IntArray void IntArray::memError ( void ) { cout << "ERROR: Cannot allocate memory.\n"; exit( 1 ); } IntArray::memError

Starting Out with C++, 3 rd Edition 30 intarray.cpp – Program (cont) void IntArray::subError ( void ) { cout << "ERROR: Subscript out of range.\n"; exit( 1 ); } // IntArray::subError int &IntArray::operator[] ( const int &sub ) { if ( sub arraySize ) subError(); return aptr[sub]; } // IntArray::operator[]

Starting Out with C++, 3 rd Edition 31 Object Conversion via overloading Special operator functions may be written to convert a class object to any other type. FeetInches::operator float ( void ) { float temp = feet; temp += (inches / 12.0); return temp; } // FeetInches::operator float

Starting Out with C++, 3 rd Edition 32 Note: No return type is specified in the function header for the previous example. Because it is a FeetInches -to- float conversion function, it will always return a float.