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

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Chapter 14: Overloading and Templates
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Classes Joe Meehean. Complex Data Types What if we need more complex data types? not simple primitives like ints, float, and chars data types that are.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
More C++ Features True object initialisation
Chapter 15 C++ Function By C. Shing ITEC Dept Radford University.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Copyright  Hannu Laine C++-programming Part 9 Hannu Laine.
Structures, Classes and Objects Handling data and objects Unit - 03.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Learning Objectives Pointers as dada members
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Review: Two Programming Paradigms
This technique is Called “Divide and Conquer”.
Memberwise Assignment / Initialization
Constructors and Other Tools
Recitation Course 0603 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
C++ Class Members Class Definition class Name { public: constructor(s)
Object Oriented Programming (OOP) Lecture No. 11
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

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

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

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.)

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

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

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.

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

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

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

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

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