Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline."— Presentation transcript:

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

2 Introduction Multiple Constructors. Default values. Overloading methods. Inline.

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

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

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

6 Function overloading int fnCalcSum(int nA, int nB) double fnCalcSum(int nA, int nB) Not acceptable Only return types differ

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

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

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

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

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

12 Constructor Overloading - Code Within 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 }

13 Constructor Overloading - Code class Account { // Some attributes here public: // Method interfaces Account(double dBal, double dInt); Account(double dBal); Account(void); } // Implementation Account::Account(double dBal, double dInt) { dBalance = dBal; dIntRate = dInt; } and so on……

14 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 rate Account number But wait ………. There’s more.

15 Reviewing prototyping It is not necessary to include the variable names within the prototype Consider “OverConstr1.h” & “OverConstr1.cpp” This is important.

16 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

17 Constructor with Defaults Account(dBal = 100.0, dInt = 5.3, nAcNum = 1) { dAccountBalance = dBal; dAccountIntRate = dInt; nAccountNumber = nAcNum; } Example – in principal only

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

19 Example calls The following are acceptable calls Account Tripta; // Default values used for all values Account Tripta(12.36, 1.0, 1); // Default values not used Account Tripta(12.36, 1.0);// Default account number used Account Tripta(12.36); // Default interest and number used Account Tripta(1)// Not acceptable wrong data type Account Tripta(,, 1)// Definitely not acceptable! Used correctly this technique is very useful.

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

21 A different example Consider the code on the next slide –Clock.cpp Together with that with that within the separate file –ClockClass.h

22 Clock.cpp #include #include "ClockClasses.h" int main(void) { Clock myClock1; myClock1.printStandard(); myClock1.printUniversal(); Clock myClock2(5); myClock2.printUniversal(); return 0; }

23 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

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

25 Summary Overloading –Functions –Methods –Constructors Default value Inline option –Pros and cons


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

Similar presentations


Ads by Google