Top Banner
C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline
25

C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

C++ Programming for Graphics

Lecture 4

Overloading, Default Values and Inline

Page 2: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Introduction

• Multiple Constructors.

• Default values.

• Overloading methods.

• Inline.

Page 3: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Multiple Constructors

• Why?– Allows the object to be instantiated with any

number of different initialisations.– e.g. Create an Account object

• Setting both interest rate and initial balance.• Setting balance and using a default interest rate.• Setting interest rate and using a default balance• Setting neither

– etc.

Page 4: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Function Overloading

• C++, unlike C, allows functions to be overloaded– Use the same function name repeatedly

• Differentiating– Using the arguments– NOT the return type

• Simple Example– Adding different value types– See next slide …..

Page 5: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Function Overloading

• int CalcSum(int nA, int nB){…….}

• double CalcSum (double dA, double dB)…...

• float CalcSum (float fA, float fB) …….• float CalcSum (float fA, int nB) ………• etc……

Page 6: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Function overloading

int fnCalcSum(int nA, int nB)

double fnCalcSum(int nA, int nB)

Not acceptable

Only return types differ

Page 7: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Function overloading

int fnCalcSum(int nA, int nB)

int fnCalcSum(int nB, int nA)

Not acceptable

Both arguments are ints

Think back to prototyping

Only declare the argument types.

Page 8: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Constructor overloading

• Exactly the same as function overloading

• Same rule concerning return type– Constructors do not return a value

• Same rule concerning arguments.– Different “patterns” of argument types.

Page 9: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Constructor Overloading

• Consider these options…

• Instantiate an account with one of the following possibilities.– Define both balance and interest rate.– Define balance only and use default interest rate.– Define interest rate only and use default balance.– Define neither and use default values.

• Is there a problem with this wish list?

Page 10: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Constructor Overloading

• Consider these options…

• Instantiate an account with one of the following possibilities.– Define both balance and interest rate.– Define balance only and use default interest

rate.– Define interest rate only and use default

balance.

– Define neither and use default values.

Page 11: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Constructor Overloading

• Amended wish list– Define both balance and interest rate.– Define balance only and use default interest

rate.– Define neither and use default values

• Later, we will see how we can reintroduce the removed item.

Page 12: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Constructor Overloading - CodeWithin class definition….

Account(double dBal, double dInt){

dBalance = dBal;dIntRate = dInt;

}Account(double dBal){

dBalance = dBal;dIntRate = 3.2; // could be a global etc.

}Account(void){

dBalance = 100.0; // could be a global etc.dIntRate = 3.2; // ditto

}

Page 13: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Constructor Overloading - Codeclass Account{

// Some attributes herepublic:

// Method interfacesAccount(double dBal, double dInt);Account(double dBal);

Account(void);}

// ImplementationAccount::Account(double dBal, double dInt){

dBalance = dBal;dIntRate = dInt;

}and so on……

Page 14: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Constructor Overloading

• Using overloaded constructors we can– Instantiate an object….. and at the same time– Pass arguments that can be used to initialise

the attributes.– e.g. Opening balance,

Initial interest rateAccount number

• But wait ……….

• There’s more.

Page 15: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Reviewing prototyping

• It is not necessary to include the variable names within the prototype

• Consider “OverConstr1.h” & “OverConstr1.cpp”

• This is important.

Page 16: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Default Values

• Referring back to the wish list.

• We had to remove one item as the argument types were the same as another constructor.

• We can get around that by introducing default values.

• Example on next slide

Page 17: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Constructor with Defaults

Account(dBal = 100.0, dInt = 5.3, nAcNum = 1)

{

dAccountBalance = dBal;

dAccountIntRate = dInt;

nAccountNumber = nAcNum;

}

Example – in principal only

Page 18: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Calling the constructor

• When calling can use as many values as required or desired.

• Default values will substitute missing.

• Can not “skip” values ……..

• Can only omit from the right hand end.

• Values are taken from the left.

Page 19: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Example calls

• The following are acceptable callsAccount Tripta; // Default values used for all valuesAccount Tripta(12.36, 1.0, 1); // Default values not usedAccount Tripta(12.36, 1.0); // Default account number usedAccount Tripta(12.36); // Default interest and number usedAccount Tripta(1) // Not acceptable wrong data typeAccount Tripta( , , 1) // Definitely not acceptable!

• Used correctly this technique is very useful.

Page 20: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Overloading Methods

• As constructors are a “special” method.

• It is possible to overload methods.

• And use default values.

• Use the same techniques.

• Respect the same limitations

• But remember……– Whilst can return different values.– This is not enough for differentiation.

Page 21: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

A different example

• Consider the code on the next slide – Clock.cpp

• Together with that with that within the separate file– ClockClass.h

Page 22: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Clock.cpp#include <iostream.h>#include <iomanip.h>#include "ClockClasses.h"

int main(void){

Clock myClock1;

myClock1.printStandard();myClock1.printUniversal();

Clock myClock2(5);myClock2.printUniversal();

return 0;}

Page 23: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Inline• The implementations were preceded with

a new key word….

• “inline”

• This will “ask” the compiler to include the method within the class.– The compiler does not always listen.– Ignored if implementation to long or

complicated.• e.g. Recursive

Page 24: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Inline – pros and cons

• Pro– The program can run faster as– Overheads calling and returning eliminated

• Con– Program will use more memory as….– Each object carries all of its code.

• Compromise– Limit inline to small often used functions and

methods.

Page 25: C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.

Summary

• Overloading– Functions– Methods– Constructors

• Default value

• Inline option– Pros and cons