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.
2. 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
3. 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
4. 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.
5. 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.
6. 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.
7. Static Example
class 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();
};
9. 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.
10. Modified Example
class 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();
};
11. 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;
}
12. 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.
13. 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.
14. Method Scope - Without
int 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;
}
15. Method Scope - With
int 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;
}
16. 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.
17. 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.
18. 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.
19. 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.
20. ++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;
}
21. 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;
}
22. 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.
23. 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.