C Class Members Class Definition class Name public

C++ Class Members Class Definition class Name { public: constructor(s) destructor function members data members protected: function members data members private: function members data members }; 1

C++ Class Members Class Definition - Data Members It is important to note …. . Each class instance gets distinct copies of all data members 2

C++ Class Members Class Definition - Data Members class My. Class { public: My. Class() { my. Value = 10; } int get. My. Value () { return my. Value; } int set. My. Value (int a. Value) { my. Value = a. Value; } private: int my. Value; }; int main(void) { My. Class inst 0, inst 1; inst 1. set. My. Value(20); cout << inst 0. get. My. Value() << endl << inst 1. get. My. Value() << endl; return 0; } 3

C++ Class Members Class Definition - Function Members Unlike data members Only one instance of each function member exists for a class Unless the function is declared inline 4

C++ Class Members Class Definition - Function Members If only a single instance…. . How are data members which function manipulates bound to the function instance? ? ? What is not shown The member function has a hidden argument The arg is a pointer to the object that was sent the message Pointer is identified by the keyword this 5

C++ Class Members Class Definition - Function Members The this pointer · Has type Class. Type · Contains the address of the class instance through which the member function was invoked · Starting address is same as the address of the first variable in the class structure 6

C++ Class Members Class Definition - Function Members Every nonstatic function member has a pointer implicitly declared Class. Name * const this; this is initialized to point to the object for which the member function was invoked Because this is declared const It cannot be changed Object pointed to can 7

C++ Class Members Class Definition - Function Members Since this is a pointer to the beginning of the class structure One can select elements using pointer notation this -> my. Element To refer to the entire instance…. . *this return *this Will produce a copy of the instance as the return value 8

C++ Class Members Class Definition - Function Members class Link { public: void append (Link* a. Link); void set. Prev (Link* a. Link); private: Link* next; Link* prev; }; Next Li Li+1 Previous this void Link : : append(Link* a. Link) { next = a. Link; a. Link -> set. Prev(this); return; } 9

C++ Class Members Managing Storage - Object Creation When a C++ object is created 2 events occur……. . Storage is allocated for the object The constructor is called to initialize the storage Storage allocation occurs in several ways and at different times 10

C++ Class Members Managing Storage - Object Creation Static Storage Allocated when the program begins Constant for the life of the program Stack Storage allocated on the stack when an execution point is reached - Opening curly brace. Released at the complementary execution point - closing curly brace. Heap - Free Storage is dynamically allocated from the heap at runtime 11

C++ Class Members Managing Storage - Object Creation Declaration Introduces a name Tells the compiler what the name means Usually go into header files Definition Allocates storage for the name 12

C++ Class Members Managing Storage - Object Creation Variable · The compiler determines the variable size and generates space · If there is an initializing value, the value entered into that storage · Compile time storage usually allocated at the opening brace · Constructor not called until sequence point where object defined · Auto storage will be allocated on the stack Function · Compiler generates code and allocates storage to hold the code · Storage for function has an address that can be produced using · Function name with no arg list · Address of operator · Usually go into implementation files 13

C++ Class Members Managing Storage - Linkage Describes storage to represent an identifier as seen by linker Internal Storage created for identifier for file being compiled Name may be used in other files without conflict External Single piece of storage for all files being compiled Accessed from other files by keyword extern Variables defined outside functions Inline function definitions default to external linkage Automatic (local) Variables Exist on the stack while function being called Auto variables have no linkage 14

C++ Class Members Managing Storage - Inline Functions C++ permits specification of inline functions Identified by keyword inline … the definition of function follows syntax inline type function. Name (argsi) { body } When inline definition encountered No code generated Code remembered and substituted for each function call 15

C++ Class Members Static Data Members A class is a type not a data object class M char a static int b When an instance of a class is defined…. . The instance gets a copy of all data members If data member declared static Single copy Shared by all instances of the class m 0 char a m 2 char a int b m 1 char a Smalltalk Pool variables 16 m 3 char a

C++ Class Members Static Data Members syntax Declaration static type var. Name; Definition type Class. Name : : var. Name < = value > Static member exists even if no class instances exist Remain in scope of class Can only be accessed from outside if declared public 17

C++ Class Members Static Data Members · Storage for a static variable Must be explicitly reserved and initialized · Static can be declared within a class Definition and initialization must occur outside Class Can be only single definition · Static (nonclass) variables Initialized to 0 by default · Static class variables Cannot be initialized by the constructor 18

C++ Class Members Initializing Static Data Members. . . class Simple. Class { public: Simple. Class () {} Simple. Class (int a. Value){my. Value = a. Value; } // static char my. Char = 'c'; This is illegal static char my. Char; private: static int my. Value; }; // Define and initialize the static variables int Simple. Class: : my. Value = 10; char Simple. Class: : my. Char = 'a'; Copyright 2006 Oxford Consulting, Ltd 19 1 February 2006

C++ Class Members Static Data Member Can appear as a default arg to a member function Non-static Data Member Cannot appear as a default arg to a member function Within a class body…. . int a; static int b; int f 1 ( int my. Value 0 = a ); // illegal int f 2 ( int my. Value 1 = b ); // ok 20

C++ Class Members Static Data Members A static data member can be an instance of the class of which it is a member…. . A non-static can only point to an instance of it’s class. Within a class body class F 1 { public: F 1 *a; static F 1 b; F 1 c; }; // ok // illegal 21

C++ Class Members Const Data Members Class data members can be specified as const Consequence Storage always allocated and must be initialized at point of definition Initialization must occur in the constructor Implemented via the initialization list Storage not allocated until instance created…. . . At that time, the const must be Created Initialized Const variable persists for the lifetime of the instance 22

C++ Class Members Static Function Members Function members can be declared static A static function has no this pointer static function can access Directly static members enums and typedefs Indirectly Regular members via. or -> through an explicit object 23

C++ Class Members Static Function Members class Simple. Class { public: //static int get. My. Number() {return my. Number; } requires an object static int get. My. Number() { return my. Value; } static int get. My. Stuff(Simple. Class *my. Class) {return my. Class->my. Number; } static int get. My. Val() {return my. Value; } static char my. Char; char my. Letter; private: static int my. Value; int my. Number; }; int Simple. Class: : my. Value=10; char Simple. Class: : my. Char = 'a'; Copyright 2006 Oxford Consulting, Ltd 24 1 February 2006

C++ Class Members Static Function Members int main() { Simple. Class my. Class, your. Class, *my. Class. Ptr = &my. Class ; cout << Simple. Class: : get. My. Stuff(my. Class. Ptr) << endl; cout << my. Class. my. Char << " " << Simple. Class: : my. Char << endl; return 0; } Copyright 2006 Oxford Consulting, Ltd 25 1 February 2006

C++ Class Members Const Function Members The keyword const can be placed after argument list of member function syntax return. Type function. Name ( argsi ) const { function body } · Tells compiler that function Can only read data members Cannot write them · Member functions declared const Can be called for non-const objects · Non-const member functions Cannot be called for const objects. 26

C++ Class Members Overloading Function Members Functions can use same name in the same scope If signature is unique Name plus signature Uniquely identify function Same holds true for member functions Same rules applied to disambiguate Must provide Implementation for each overloaded function 27

C++ Class Members Operator Function Members C++ defined operators can be overloaded to work on Class type operands Must take at least one class type argument syntax operator X (args) { body } X is the operator symbol being overloaded args - as appropriate for implementation body - implements the function 28

C++ Class Members Operator Function Members Invocation Assume + is overloaded Infix expression syntax A+B A, B can be member or non-member instances Function call syntax A. operator+ (B) 29

C++ Class Members Operator Function Members Rules Because operator functions are invoked using the same syntax Operator functions. . . 1. Must have the same number of arguments as language defined versions 2. Must have the same precedence as the builtin operators…This cannot be overridden 3. Cannot have default parameters 4. Cannot override the predefined meaning 30

C++ Class Members Operator Function Members Limitations Because of the operator function syntax 1. Certain user defined operators cannot behave analogously to built in versions Prefix version operator++ () {} Postfix version operator++ (int) {} 2. Some operators cannot be overloaded. , ? : : : . * 3. Some operators must be members only =, [ ] , ( ), -> 31

C++ Class Members Overloading Operators class Complex { public: int im, re; Complex(int r=0, int i=0): re(r), im(i) { } Complex operator+ (Complex a 1) {return Complex(re+a 1. re, im+a 1. im); } Complex& operator++ () { re += 1, im += 1; cout << “prefix" << endl; return Complex(re -1, im -1); } Complex& operator++ (int) { re+=1, im+=1; cout << "postfix" << endl; return *this; }; } Copyright 2006 Oxford Consulting, Ltd 32 1 February 2006

C++ Class Members Operator Function Members Member Left hand operand must be an instance of class overloading the operator Unary x. operator@() x is class instance Binary x. operator(y) x operator@ y x is class instance 33

C++ Class Members Operator Function Members Non-Member If operator requires left hand operand of a different type…. . · Must use non-member syntax · Must be declared friend if the operator requires access to protected or private members Unary operator@(x) Binary operator@(x, y) x operator@ y 34

C++ Class Members Operators in Action The Copy Constructor and the Assignment Operator There are many occasions when a copy of an object is needed · Object passed by value x. func(y) A copy of y is made and passed · Object returned by value · Object initialized by another object Class. Name x 1 = x 2 A copy of x 2 is made · Object assigned to another object x 1 = x 2 is copied to x 1 and x 1 is destroyed 35

C++ Class Members Operators in Action The Copy Constructor A constructor that…. . · Takes a reference to an object of the same class as an argument · Performs initialization on newly created objects · Used by compiler to copy objects by value syntax class X ( const class X& ) class X ( class X& ) 36

C++ Class Members Operators in Action The Copy Constructor · If a copy constructor is not defined Shallow bitwise copy performed - memcpy · If a copied object uses new to allocate memory when created One must write a copy constructor · Copy constructor need only be written If class object is passed by value · To prevent pass by value Define a private copy constructor 37

C++ Class Members Operators in Action The Assignment Operator - operator=() · Called when one initialized object is to be assigned to a second. · The operator must serve as a destructor to the assignee object. · Can have a return value… – The most common is *this · Is not called for the declarations such as – Class. Name object 1 = object 2 38

C++ Class Members - Friends A class Definition…. Provides 3 levels of access to data and function members · public · protected · private Closed 39 Open

C++ Class Members - Friends C++ provides means for more liberal access to non-public members under specified set of conditions…. . Nonmember designated as a friend of the class Class friend is designated by Keyword friend Followed by class name 40

C++ Class Members - Friends Once an object is designated as a friend…. It has access to non-public members as if they were public If B is designated as friend of A B can access A’s non-public members A cannot access B’s 41

C++ Class Members - Friends Friend may be: 1. Class friend class X; 2. A particular function friend int sub (int y); 3. A particular class member friend int Mult: : get(); 42

C++ Class Members - Friends A friend does not have a this pointer unless it is a member function in its own right Question: If it is a member function which this pointer does it have Class A a; Class B b; Let B be declared as a friend of A The this pointer of a function b. Funct() contains the address of the class instance b 43

C++ Class Members - Friends Caveats and Problems, , , · Friends can have access to everything … this defeats data hiding. · Introduces extra coupling between classes. · Permits change of internal state from the outside. · Should use member functions not friends to change state. 44
- Slides: 44