Keyword: const const class Bank { public: Money - - PowerPoint PPT Presentation

keyword const
SMART_READER_LITE
LIVE PREVIEW

Keyword: const const class Bank { public: Money - - PowerPoint PPT Presentation

Keyword: const const class Bank { public: Money AccountBalance(int id) const; int Withdraw(int id, const Money &money); private: const unsigned int ACCOUNTS; //Not used const static unsigned int MAX_ACCOUNTS = 10; }; //Not used const


slide-1
SLIDE 1

Keyword: const

slide-2
SLIDE 2

const

  • Constants can take many different forms...

class Bank { public: Money AccountBalance(int id) const; int Withdraw(int id, const Money &money); private: const unsigned int ACCOUNTS; //Not used const static unsigned int MAX_ACCOUNTS = 10; }; //Not used const int baz = 5; int* const foo = &baz; const int* bar = &baz;

slide-3
SLIDE 3

Constant “Variable”

  • Data stored can not be changed
  • Useful for naming literal values (e.g. PI=3.14...)
  • When applied to objects, the object can only call

Constant Member Functions

class Bank { public: Money AccountBalance(int id) const; int Withdraw(int id, const Money &money); private: const unsigned int ACCOUNTS; //Not used const static unsigned int MAX_ACCOUNTS = 10; }; //Not used const int baz = 5; int* const foo = &baz; const int* bar = &baz;

slide-4
SLIDE 4

Constant Pointer

  • Pointer always points to the same location
  • But data pointed to can change
  • Useful for protecting buffer locations when passing

to functions

class Bank { public: Money AccountBalance(int id) const; int Withdraw(int id, const Money &money); private: const unsigned int ACCOUNTS; //Not used const static unsigned int MAX_ACCOUNTS = 10; }; //Not used const int baz = 5; int* const foo = &baz; const int* bar = &baz;

slide-5
SLIDE 5

Pointer to a Constant

  • Pointer can be updated to point to other addresses
  • But data pointed to can not be changed
  • Useful for marking read-only data when passing to

functions

class Bank { public: Money AccountBalance(int id) const; int Withdraw(int id, const Money &money); private: const unsigned int ACCOUNTS; //Not used const static unsigned int MAX_ACCOUNTS = 10; }; //Not used const int baz = 5; int* const foo = &baz; const int* bar = &baz;

slide-6
SLIDE 6

Constant Reference

  • Same effect as Pointer to a Constant
  • But the syntax is as if the variable was passed

by value

class Bank { public: Money AccountBalance(int id) const; int Withdraw(int id, const Money &money); private: const unsigned int ACCOUNTS; //Not used const static unsigned int MAX_ACCOUNTS = 10; }; //Not used const int baz = 5; int* const foo = &baz; const int* bar = &baz;

slide-7
SLIDE 7

Constant Member Data

  • Same effect as Constant “Variable” except is applied to an
  • bject's scope
  • Can only be initialized within the constructor within the

initialization list

– e.g. Bank::Bank() : ACCOUNTS(10) { //Constructor }

class Bank { public: Money AccountBalance(int id) const; int Withdraw(int id, const Money &money); private: const unsigned int ACCOUNTS; //Not used const static unsigned int MAX_ACCOUNTS = 10; }; //Not used const int baz = 5; int* const foo = &baz; const int* bar = &baz;

slide-8
SLIDE 8

Constant Class Data

  • Same effect as Constant “Variable” except is applied to

an classes' scope

  • This means you don't need an object to access it

– e.g. Math::PI

class Bank { public: Money AccountBalance(int id) const; int Withdraw(int id, const Money &money); private: const unsigned int ACCOUNTS; //Not used const static unsigned int MAX_ACCOUNTS = 10; }; //Not used const int baz = 5; int* const foo = &baz; const int* bar = &baz;

slide-9
SLIDE 9

Constant Member Functions

  • Signals that the function does not modify any

member data

  • Useful in preventing accessor functions from

accidentally modifying data

class Bank { public: Money AccountBalance(int id) const; int Withdraw(int id, const Money &money); private: const unsigned int ACCOUNTS; //Not used const static unsigned int MAX_ACCOUNTS = 10; }; //Not used const int baz = 5; int* const foo = &baz; const int* bar = &baz;