2cpp18 - modifiers

23
C++ MODIFIERS Michael Heron

Upload: michael-heron

Post on 01-Nov-2014

103 views

Category:

Software


1 download

DESCRIPTION

This is an intermediate conversion course for C++, suitable for second year computing students who may have learned Java or another language in first year.

TRANSCRIPT

Page 1: 2CPP18 - Modifiers

C++ MODIFIERSMichael Heron

Page 2: 2CPP18 - Modifiers

Introduction• Modifiers are used in C++ to change default behaviour of

functions and variables.• Powerful tools, but to be used with caution.

• In this lecture we are going to talk about the way in which some of these work.• You’ve already seen some of them.

• Public• Private

• Both of these are modifiers for visibility

Page 3: 2CPP18 - Modifiers

Static• Static methods and attributes are a common part of C#

and Java• They indicate that the function or attribute belongs to a class rather

than an object.

• Each object that is instantiated makes use of the same static variables.• Change it on object, and you change it in all of them.

• Only one copy of a static field is ever created,• All objects get their own bit of memory for everything else.

• instance variables

Page 4: 2CPP18 - Modifiers

Static• Static methods similarly are methods that belong to a

class.• They do not require the context of an object in order to be used.• Often used to provide ‘utility methods’ that do not require object

instantiation.• Integer.parseInt for example

• Must indicate the method to be called by the class name.• Not an object name.

Page 5: 2CPP18 - Modifiers

Static• Static methods are limited.

• They can only make use of other static methods and static variables.

• In Java the main method is a static method.• It should serve to bootstrap the program by instantiating the

appropriate objects.

• Programming around this can be awkward.• You may have seen this yourself in previous years.

Page 6: 2CPP18 - Modifiers

Static in C++• Static in C++ works conceptually identically.• Minor variations

• Use the scope resolution operator to access static methods in a class.

• Static variables cannot be set an initial value in a class declaration.• Must be done in the accompanying CPP file.

Page 7: 2CPP18 - Modifiers

Static Exampleclass Account {private: int balance; int overdraft; static float interest_rate;public: int query_balance(); void set_balance (int); int query_overdraft(); void set_overdraft (int); float query_interest_rate(); };

Page 8: 2CPP18 - Modifiers

Static Example#include "Account.h"#include <iostream>#include <fstream>

using namespace std;

float Account::interest_rate = 0.05;

void Account::set_balance (int v) { balance = v;}int Account::query_balance() { return balance;}void Account::set_overdraft (int v) { overdraft = v;}int Account::query_overdraft() { return overdraft;}float Account::query_interest_rate() { return interest_rate;}

Page 9: 2CPP18 - Modifiers

Static• Instance methods can make use of static fields.

• Not a great benefit

• Better to make the method static and call it from the class context.• No need for the overhead of object instantiation.

• Can still be called as an instance method.• But no need for it to be done so.

Page 10: 2CPP18 - Modifiers

Modified Exampleclass Account {private: int balance; int overdraft; static float interest_rate;public: int query_balance(); void set_balance (int); int query_overdraft(); void set_overdraft (int); static float query_interest_rate(); };

Page 11: 2CPP18 - Modifiers

Modified Example#include <iostream>#include <fstream>#include <string>#include <iomanip>#include "Account.h"

using namespace std;

int main() { Account *ob;

ob = new Account();

cout << Account::query_interest_rate() << endl; cout << ob->query_interest_rate() << endl;

return 0;}

Page 12: 2CPP18 - Modifiers

Static in C++• When the program starts, and before an objects are

instantiated, all static fields are initialized.• When an object is declared, it gets copies of instance

attributes.• But no copy of the static data fields.

• This system is equivalent in Java and C++ and C#• Transferable concept.

Page 13: 2CPP18 - Modifiers

Static in C++• C++ also gives us a little extra somethingsomething.

• We can declare static variables that are local to methods.• They get created when the method is first called.• Have visibility only to the method in which they are defined.• Destroyed when the program ends.

• Not when the method ends

• This is called method scope.

Page 14: 2CPP18 - Modifiers

Method Scope - Withoutint test_static (int tmp) { int y = tmp; return y;}

int main() { int ans;

ans = test_static (10);

cout << ans << endl;

ans = test_static (12);

cout << ans << endl;

return 0;}

Page 15: 2CPP18 - Modifiers

Method Scope - Withint test_static (int tmp) { int y = tmp; return y;}

int main() { int ans;

ans = test_static (10);

cout << ans << endl;

ans = test_static (12);

cout << ans << endl;

return 0;}

Page 16: 2CPP18 - Modifiers

Const• One of the most useful modifiers we have available is the

const modifier.• It lets us deal with some of the problems caused by relentless

pointer passing.• It’s also one of the messiest

• It compensates for missing language features.

• It has several key roles in an object oriented program.

Page 17: 2CPP18 - Modifiers

Const• First and simplest use is to declare a constant, non-

changing value.• Declare something as constant so it cannot be changed.

• Works much like a define in terms of where it is used.• However, it is syntactically interpreted by the compiler.• Defines just get search and replaced into your code.

Page 18: 2CPP18 - Modifiers

Const• const int num = 20;

• Attempting to change this after it has been defined will result in a compile time error.

• Can be used to make sure there are no side-effects when working with shared, global variables.• Or class-wide variables.

• It also works with pointers.• But in two different ways.

• const int* blah;• Pointer to a constant integer

• int *const blah;• Constant pointer to an integer.

Page 19: 2CPP18 - Modifiers

Const• Can also use const to indicate a return type in a function.

• Indicates to the compiler that the return value of a function should never be altered.

• Returning a constant pointer is a good way to ensure fidelity of memory.• You can’t change the value of the pointer once it comes out of the

function.

Page 20: 2CPP18 - Modifiers

++OUT OF CHEESE ERROR++using namespace std;

const int *memory_const (int x) { return &x;}

int main() { int *ptr; int y = 10;

ptr = memory_const (10);

ptr = &y;

return 0;}

Page 21: 2CPP18 - Modifiers

Const in Parameters• We can also use const in parameter passing.

• Lets us have pass by reference but also ensures we can’t change the values sent in.

const int *memory_const (const int x) { x = 20; return &x;}

Page 22: 2CPP18 - Modifiers

Const in Object Orientation• We can use const in methods of objects to bar them from

editing instance variables.• One of the features of class-wide scope is that any method with

appropriate visibility can impact on any attribute.

• By adding const to a method declaration, we prohibit it from accessing method attributes.• We saw this when we looked at pure virtual methods.

Page 23: 2CPP18 - Modifiers

Const and Virtual Methods• When we talked about virtual methods, we used the

following format:• void function() const = 0;

• And then to override, we use the following:• void function() const

• This is a precautionary technique.• The const may be omitted in both conditions.• This is often used to protect from careless side-effects.