Module 8: more on methods #1 2000/01Scientific Computing in OOCourse code 3C59 Module 8: More on methods: In this module we will cover: Overloading of.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
More on Classes Inheritance and Polymorphism
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Road Map Introduction to object oriented programming. Classes
Enhancing classes Visibility modifiers and encapsulation revisited
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
C++ fundamentals.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
CMP-MX21: Lecture 6 Objects & Methods 1 Steve Hordley.
Module 2: User Data types #1 2000/01Scientific Computing in OOCourse code 3C59 Module 2: User defined Data Types & Operations upon them In this module.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Module 10: Simple Inheritance #1 2000/01Scientific Computing in OOCourse code 3C59 Module 10: Simple Inheritance In this module we will cover Inheritance.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Lecture 10 Inheritance “Absolute C++” Chapter 14.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Object-Oriented Programming in C++
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 CS161 Introduction to Computer Science Topic #9.
1 CSC241: Object Oriented Programming Lecture No 25.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Module 4: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 4: I/O In this module we will cover Keyboard/screen input and output.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
1 CS161 Introduction to Computer Science Topic #16.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.
OOP Basics Classes & Methods (c) IDMS/SQL News
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Module 11: Polymorhism #1 2000/01Scientific Computing in OOCourse code 3C59 Module 11: Polymorphism and virtual methods In this module we will cover Polymorphism.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Module 20/21/22: The Standard Template Library
Programming with ANSI C ++
Templates.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Chapter 5 Classes.
Pointers and Pointer-Based Strings
Object Oriented Programming COP3330 / CGS5409
Java-An introduction In this module we will cover The class in Java
Programs and Classes A program is made up from classes
Method of Classes Chapter 7, page 155 Lecture /4/6.
Pointers and Pointer-Based Strings
Chapter 9 Inheritance.
Java Programming Language
Presentation transcript:

Module 8: more on methods #1 2000/01Scientific Computing in OOCourse code 3C59 Module 8: More on methods: In this module we will cover: Overloading of methods Private methods and self messaging const methods const arguments

Module 8: more on methods #2 2000/01Scientific Computing in OOCourse code 3C59 Aims of this module We have previously introduced the concept of a “class” as a construct which combines member variables and methods in a formal way. So far, however, we have only looked at very simple aspects of methods of a class. In particular we have only written the methods using simple features (i.e those which you might be familiar with from simple C or Fortran) In this module you will learn about some features of C++ which allow you to use methods of objects in a much more flexible and controlled way.

Module 8: more on methods #3 2000/01Scientific Computing in OOCourse code 3C59 8.1: Overloading of methods You can have several methods of a class having the same name provided that their argument list is different. This feature is very simple but very useful As example consider you are designing some code to administer Library stock What sort if items do you have in a library ? Books Magazines Journals Videos...etc

Module 8: more on methods #4 2000/01Scientific Computing in OOCourse code 3C59 Each of these is characterised in a different way: Books : Subject code, Author, Title, ISBN.... Magazines: Title, Year, Volume, Issue number You would probably decide that it made sense to define a different class to represent each of these. class Book { int subject ; string author ; string title ; int isbn ; }; class Magazine { string title ; int year; int vol ; int issueno ; }; class Journal {... };

Module 8: more on methods #5 2000/01Scientific Computing in OOCourse code 3C59 Next you might decide that you need a class called Catalogue which must store all library items. What services (i.e. methods) should Catalogue provide ? -Presumably you want to be able to add different items to the catalogue. Therefore you might write the Catalogue class like this: class Catalogue { private: vector booksInStock; // Vecotr to hold books vector ferretWeekly; // To hold issues of FW vector arcWeldersWorld; // To hold issues of AWW public: void addABook( Book ) ; void addAMagazine( Magazine ) ; };

Module 8: more on methods #6 2000/01Scientific Computing in OOCourse code 3C59 // Here are the Catalogue methods // Add a book method void Catalogue::addABook( Book newBook ) { booksInStock.push_back( newBook ) ; } // Add a magazine method void Catalogue::addAMagazine( Magazine mag ) { if( mag.title() == “ferretWeekly” ) { ferretWeekly.push_back( mag ); } else if( mag.title() == “arcWeldersWorld” ) { arcWeldersWorkd.push_back( mag ) ; } You need one method name to add a Book: and a different method name to add a Magazine:

Module 8: more on methods #7 2000/01Scientific Computing in OOCourse code 3C59 Why do you need two different method names ? As far as the user of a Catalogue is concerned : - they only care that they want to add something to the catalogue - they don’t care about the details of what Catalogue does about it Thus a user only really wants a single method called add(..)

Module 8: more on methods #8 2000/01Scientific Computing in OOCourse code 3C59 In OO languages this is allowed ! class Catalogue { private: vector booksInStock; // Vecotr to hold books vector ferretWeekly; // To hold issues of FW vector arcWeldersWorld; // To hold issues of AWW public: void add( Book ) ; void add( Magazine ) ; }; You declare your class like this:

Module 8: more on methods #9 2000/01Scientific Computing in OOCourse code 3C59...and here is the add method for a Book:... and the add method for a Magazine: Note that they have the same name. The compiler is smart enough to work out which one you want from the argument you pass to it // Here are the Catalogue methods // Add a book method void Catalogue::add( Book newBook ) { booksInStock.push_back( newBook ) ; } // Add a magazine method void Catalogue::add( Magazine mag ) { if( mag.title() == “ferretWeekly” ) { ferretWeekly.push_back( mag ); } else if( mag.title() == “arcWeldersWorld” ) { arcWeldersWorkd.push_back( mag ) ; }

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 // Program fragment to demonstrate overloaded // methods // Make a Catalogue Catalogue ugLibrary; // Make a book and set its author..etc Book b ; b.initialise( “Ian M Banks”, ) ; // Make a magazine Magazine m ; m.initialise( “ferretWeekly”, 2000, 6 ) ; // Now add them to the catalogue uglibrary.add( b ) ; ugLibrary.add( m ) ; Here is a program which shows how we would use these two different methods

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 This feature is called: “method overloading” You may have as many methods with the same name as you like, provided their argument list is different. The compiler works out which method to actually invoke. It is used extensively to keep user code simple. In fact we have already seen this used for constructors. Remember that you can have many constructors with different argument lists. This is possible as the constructors are overloaded.

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 8.2: Private methods & self messaging We have already covered this but re-cover it here just for completeness. It may well be (very likely) that within some method of a class you need to do something which uses another method of the same class. //Method of BankAccount: statusCheck void BankAccount::statusCheck( ) { if( availableFunds( ) < 0.0 ) { // It seems to have exceeded the limit cout << "\n\n Dear Mr/Ms } return ; } We saw this in BankAccount Here the method statusCheck(..) uses the method availablefunds(..)

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 This is known as “self messaging” This is when an Object sends a message to itself (I.e. calls a method of itself) statusCheck(..) availableFunds(..)Now in this case availablefunds(..) is a public method anyway. However it may be that you want to write a method purely for internal use. I.e you don’t want anyone outside the object using it. Suppose we don’t want an outsider to be able to find out the available funds. Then we can make this a private method

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 class BankAccount { private: string holderName ; float currentBalance ; float overdraftLimit ; bool jointAccount ; int yearsHeld ; void availableFunds( ) ; public: bool statusCheck( ) ; other methods }; This is what this bit of the icon symbolises We do it simply by declaring it in the private section of the class like this:

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 Use of this-> for self messaging //Method of BankAccount: statusCheck void BankAccount::statusCheck( ) { if( this->availableFunds( ) < 0.0 ) { // It seems to have exceeded the limit cout << "\n\n Dear Mr/Ms } return ; } The following is a "good style" recommendation: When you self message in a piece of code, always invoke the method using this-> Doing so makes it explicitly clear that you are calling a method of the object, rather than some external function. You soon get very used to doing this.

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 8.3: const methods Just as you can have const items you can have const methods What does this mean ? It means that you are telling the compiler that this method promises not to change the state of the object in any way. This is useful to: (i) ensure that anyone writing or modifying the code of the method doesnt accidentally change anything (it will fail to compile) (ii) help the compiler generate efficient code

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 Here is an example of a method which is (and should be! ) const //const Method of BankAccount float BankAccount::availableFunds( ) const { return (currentBalance + overdraftLimit) ; } Here is an example of a method which cannot be const //non const Method of BankAccount void BankAccount::deposit( float amount ) { currentBalance += amount ; return ; }

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 If you tried this it would generate a compiler error: void BankAccount::deposit( float amount ) const { currentBalance += amount ; return ; }

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 It is very good practice to methods const wherever possible

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 8.4: const arguments You can also tell the compiler that an argument which you are going to pass will not be changed by the method. This also helps the compiler and protects from user programming errors. Here is an example of a const argument // Method of BankAccount void BankAccount::deposit( const float amount ) { currentBalance += amount ; return ; }

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 // Method of BankAccount void BankAccount::deposit( const float amount ) { amount += 500 ; currentBalance += amount ; return ; } If you tried this it would generate a compiler error:

Module 8: more on methods # /01Scientific Computing in OOCourse code 3C59 Summary of Module 8: More on methods: In this module we have covered the following topics. Overloading of methods You can have many different methods with the same name provided they have different argument lists Private methods and self messaging If you want to write a method purely for internal use by the class, then you should make it a private method Methods of a class can call other methods of the class. const methods If a method does not change the state of an object then you should always make it const const arguments If a method does not change an argument then make the argument const