Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.

Similar presentations


Presentation on theme: "Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading."— Presentation transcript:

1 Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading

2 Introduction Last lecture we looked at functions –syntax, overloading, default arguments, inline expansion header files –need for prototypes, compiling multiple source files This lecture we will look at more aspects of functions –constants, parameter passing and operator overloading introduce reference types

3 Constants sometimes we want to use constants in a program –data that cannot be changed once defined –use the const keyword const double pi = 3.14159; pi = 45.2; // illegal error C3892: 'pi' : you cannot assign to a variable that is const

4 Constant objects objects can also be marked constant –then none of their attributes are allowed to be changed once initialised a constant Account object should have its balance frozen –you can't withdraw or deposit money, or add interest const Account myAccount(45, 2.4); double bal = myAccount.getBalance(); however, the Account class we have written does not allow ANY member functions to be called on a constant Account object error C2662: 'Account::getBalance' : cannot convert 'this' pointer from 'const Account' to 'Account &'

5 Constant methods it should be OK to get the balance of a constant account object –as long as we don't change the balance enable this by marking the getBalance() method with the const keyword both the method declaration (prototype) and the definition must be const double getBalance() const; double Account::getBalance() const {return balance;}

6 Constant methods make sure the constant method does not attempt to alter a member variable! double Account::getBalance()const{ balance -= 0.05; // not allowed return balance; } error C3490: 'balance' cannot be modified because it is being accessed through a const object

7 Reference types a reference type is like an alias for a data object int myAge = 25; int & yourAge = myAge; yourAge++; // it's your birthday cout << "You are "<< yourAge << " and I am " << myAge << endl; output: You are 26 and I am 26 we must be twins!

8 Reference types yourAge and myAge reference the same data which is stored at the memory location associated with myAge if myAge goes out of scope, yourAge refers to invalid data myAge knows nothing about yourAge –so is not affected if yourAge goes out of scope

9 Reference types note: the address values given are arbitrary examples AddressNamevalue 1235012yourAge 1245000myAge25

10 Parameter passing in C++, by default all parameters are passed to functions by value a local copy is made of each parameter when the function is called –the local copy goes out of scope when the function returns if the function changes the local copy, the original data in the calling function is unchanged how can we implement a swap function?

11 Swap function – pass by value void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x =3, y = 5; swap(x, y); cout << "After swap function, x = " << x << " and y = " << y << endl; } After swap function, x = 3 and y = 5

12 Pass by reference we can't do the swap passing by value we can only return one value from the function, not two the solution is to pass reference types instead the function will alter the data stored by the variables they alias when the function returns, x and y will contain the new values

13 Swap function – pass by reference void swap(int & a, int & b) { int temp = a; a = b; b = temp; } int main() { int x =3, y = 5; swap(x, y); cout << "After swap function, x = " << x << " and y = " << y << endl; } After swap function, x = 5 and y = 3

14 Pass by reference when the swap function is called, the parameters a and b are initialised as aliases to x and y void swap(int & a, int & b) initialises int & a = x; int & b = y; when swap returns, the aliases go out of scope and the values of x and y have been changed

15 Why pass by reference? passing by reference is useful when you: want to change one or more function parameters would like to return more than one value from a function –pass one of them in as a reference, and change it inside the function a parameter is very large, so making a local copy in the function would be wasteful –3D model or image object –when a reference is passed, only the address is copied

16 Constant references if you pass an object by reference, the function could change it –even if the intention was just to be more efficient using the keyword const ensures that the referenced object can be passed safely the function cannot change the object –can only call its const functions void outputAccount(const Account & a); void outputAccount(const Account & a) { cout << a.getBalance() << endl; }

17 Pass by value or reference? in Java, the programmer has no choice –primitive types are passed by value the function uses a local copy of the data –Strings, arrays and objects are passed by reference the address is passed to the function the function can manipulate the data in C, the programmer has a choice –passing by value ensures the original data isn't changed –passing by reference allows the data to be updated –passing by value is inefficient with larger data types making a local copy of the data uses memory and take time –can safely pass by constant reference

18 Operator overloading we have already looked at overloading functions –same name, different parameter list we have seen that common operators are overloaded + - * / > we can write our own operator overloads

19 Overloading the << operator sends bytes from the variable on the right to the output stream on the left cout << "Hello!" << endl; defined to send bytes from strings, ints, doubles… we could overload the << operator to work with an Account object Account myAccount(45, 2.4); cout << myAccount << endl; Account balance: 45 Interest rate: 2.4

20 Overloading << << is not a member of the Account or ostream class – define it outside the Account class ostream& operator<< (ostream& os, const Account acc) { os << "Account balance: " << acc.getBalance() << endl << "Interest rate: " << acc.getInterestRate() << endl; return os; } note the use of a different overload of << within the function

21 Friend functions and operators functions and operators can be declared friends of one or more classes the function has access to the private members of its friends we could declare << to be a friend of Account –in the public section of Account friend ostream& operator<< (ostream& os, const Account acc); and rewrite the operator definition to use private member variables of Account ostream& operator<< (ostream& os, const Account acc) { os << "Account balance: " << acc.balance << endl << "Interest rate: " << acc.interestRate << endl; return os; } is this a good idea? using the friend keyword ties the implementation of this overload of << to the internal implementation of Account

22 Summary In this lecture we have: looked at more aspects of functions –constants, parameter passing and operator overloading introduced reference types In the tutorial we will split the Account definintion to use header files practice using different types of functions –overloads, constant, friend, operator… experiment with passing by reference and value


Download ppt "Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading."

Similar presentations


Ads by Google