Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  Hannu Laine C++-programming Part 7 Hannu Laine Static members Different uses of const specifier.

Similar presentations


Presentation on theme: "Copyright  Hannu Laine C++-programming Part 7 Hannu Laine Static members Different uses of const specifier."— Presentation transcript:

1 Copyright  Hannu Laine C++-programming Part 7 Hannu Laine Static members Different uses of const specifier

2 HL1 The keyword static preceding class data member means that there is only one copy of that member that is common to all instances of the class. Static data member can be public or private. If static data member is private, a public static member function is needed to access it. Static members are accessed using the class name and the scope operator. Static data members are initialised separately in the implementation of the class like global variable. Example. Static data member n indicates how many instances of the class Class currently exists. class Class { public: Class(); static int getN(); ~Class(); private: static int n; // class variable int a; // instance variable }; void main(void) { cout << Class:: getN(); // called without obj Class c1, c2, c3; cout << Class::getN(); } Static data members

3 HL2 //Allocating and initialising static data member int Class::n = 0; //Implementation of member functions int Class::getN() { return n; } Class::Class() { n++; } Class::~Class() { n--; } Static data members (cont.)

4 HL3 Requirement for class wide constants is that they are stored only once in memory (not in each instance of the class). That’s why keyword static is used with these kind of constants. Example. class Class { public: Class(); static const int C; //static const int C = 100; void useC(); private: --- }; void main(void) { Class c; cout << Class::C; cout << c.C; } const int Class::C = 100; //Implementation of member functions void Class::useC() { cout << C; } Class wide constants Option 1 Option 2

5 HL4 Const member function Access function returning const pointer or reference Constant pointer or reference as a parameter Example. You need to display only the name of a person (age is not allowed to show up). Then you need a so called access function to get name of a person (getName). If you have to change the name of a person, you need access function again (setName). See next page. Access functions and const specifiers

6 HL5 class Person { public:... (constructors) const char *getName() const; void setName(const char *newName);... private: char name[30]; int age; } void main(void) { Person matti(”Matti”, 21); cout << matti.getName(); matti.getName()[0] = ’x’; strcpy(matti.getName(), ”xxxx”); matti.setName(”Matias”); } const char *Person::getName() const { strcpy(name, ”xxxxx”); age = 10; return name; } void Person::setName(const char *newName) { strcpy (name, newName); strcpy(newName, ”xxxx”); newName[0] = ’x’; } Using const specifier in functions Protects parameter array from modification in function body. Protects returned pointer to be used to modify the destination Protects called object from modification in function body.

7 HL6 If the member function is defined in the class definition like ReturnType memberFunction ( parameters ) const; the const specifier is like the this pointer would be defined as const ClassName* this; or rather const ClassName* const this; One more thing about const member function

8 HL7 We have seen how to define a class wide const. This means that all instances of that class can use the same constant value and this constant is stored only once in the memory. There are also situations when some member of an object needs to remain the same as long as the object exists in the memory (but this constant value can be different for each object). An example of that kind of situation is when each object has unique id number that is given when an object is created and it is not allowed to change it after that. The following example demonstrates how we can do it: class Class { public: Class ( int id0 ) ; void setValue ( int v ) ; private: const int id ; // const instance variable int a ; // non const instance variable } void main(void) { Class c1(100), c2(200) ; c1.setValue(5); } Const instance variables 1/2

9 HL8 Class::Class(int id0) : id(id0) { a = 0; // it is not possible to set id member here // id = id0; Compilation error } void Class::setValue(int v) { a = v; // it is not possible to change id member // id = 100; Compilation error } Remark. It is also possible to initialize member a in the initialisation list: Class::Class(int id0) : id(id0), a(0) { // Now the function body is empty } Const instance variables 2/2

10 HL9 Lets assume that class is defined in the following way: class Class { public: Class( int a0, int b0 ) ; void mf1() const; void mf2(); private: int a, b; }; Lets assume that we have two “global” functions: void gf1 ( const Class &c ) ; void gf2 ( Class &c ) ; Then we try to use the class in the following way with the following results from the compiler: void main(void) { Class c1(1, 2) ; const Class c2(10, 20) ; //Using c1 c1.mf1 ( ) ; // OK c1.mf2 ( ) ; // OK gf1 ( c1 ) ; // OK gf2 ( c1 ) ; // OK //Using c2 c2.mf1 ( ) ; // OK c2.mf2 ( ) ; // Syntax error gf1 ( c2 ) ; // OK gf2 ( c2 ) ; // Syntax error } Const objects 1

11 HL10 We still use the same class Class and take a look to the implementation of functions gf1 and gf2: void gf1 ( const Class &c ) { c.mf1(); // OK c.mf2();// Syntax error } void gf2 ( Class &c ) { c.mf1(); // OK c.mf2();// OK } Const objects 2


Download ppt "Copyright  Hannu Laine C++-programming Part 7 Hannu Laine Static members Different uses of const specifier."

Similar presentations


Ads by Google