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

25
C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    0

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

C++ Programming for Graphics

Lecture 4

Overloading, Default Values and Inline

Introduction

• Multiple Constructors.

• Default values.

• Overloading methods.

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

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

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

Function overloading

int fnCalcSum(int nA, int nB)

double fnCalcSum(int nA, int nB)

Not acceptable

Only return types differ

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.

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.

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?

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.

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.

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

}

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

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.

Reviewing prototyping

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

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

• This is important.

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

Constructor with Defaults

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

{

dAccountBalance = dBal;

dAccountIntRate = dInt;

nAccountNumber = nAcNum;

}

Example – in principal only

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.

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.

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.

A different example

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

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

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

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

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.

Summary

• Overloading– Functions– Methods– Constructors

• Default value

• Inline option– Pros and cons