Today’s Topic Const Ref:

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
11 Introduction to Object Oriented Programming (Continued) Cats.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
1 Lecture 3 More about C++. 2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 CSC241: Object Oriented Programming Lecture No 08.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
CS31 Discussion Jie(Jay) Wang Week8 Nov.18.
Chapter 1.2 Introduction to C++ Programming
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?
C++ Templates.
OBJECT ORIENTED PROGRAMMING
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.
Motivation and Overview
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?
Chapter 5 Classes.
Pointers and Pointer-Based Strings
Pointers Psst… over there.
Global & Local Identifiers
Andy Wang Object Oriented Programming in C++ COP 3330
Today’s Topic Dynamic allocation Slides for CS 31 discussion session
Pointers Psst… over there.
Const in Classes CSCE 121 J. Michael Moore.
Name: Rubaisha Rajpoot
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Pointers & Functions.
Classes Short Review of Topics already covered Constructor
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Pointers Call-by-Reference CSCI 230
Object Oriented Programming Using C++
How Classes Work with Memory Diagram
Namespaces How Shall I Name Thee?.
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Class.
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Pointers and dynamic objects
Pointers & Functions.
COP 3330 Object-oriented Programming in C++
Object Oriented Programming (OOP) Lecture No. 11
CS31 Discussion 1D Winter18: week 6
CS31 Discussion 1H Winter19: week 9
CS31 Discussion 1D Winter19: week 4
CS31 Discussion 1D Fall18: week 2
CS31 Discussion 1H Fall18: week 9
Constructors and Deconstructor
How Classes Work with Memory Diagram
Class Circle { Float xc, yc, radius;
Object Oriented Programming (OOP) Lecture No. 12
Classes Member Qualifiers
Presentation transcript:

Today’s Topic Const Ref: http://www.learncpp.com/cpp-tutorial/2-9-symbolic- constants-and-the-const-keyword/ http://www.learncpp.com/cpp-tutorial/810-const-class- objects-and-member-functions/ Slides for CS 31 discussion session TA: Hsiao-Yun (Katie) Tseng tsenghy@g.ucla.edu Credit to former TA Bo-Jhang Ho (bojhang@cs.ucla.edu), CS31 Discussion 1E, Spring 17’ Credit to former TA Chelsea Ju (chelsea.ju@cs.ucla.edu)

Constant variables To make a variable constant, simply put the const keyword either before or after the variable type Declaring a variable as const prevents us from inadvertently changing its value: Defining a const variable without initializing it will also cause a compile error: const double gravity = 9.8; // preferred use of const before type int const sidesInSquare = 4 ; // okay, but not preferred const double gravity = 9.8; gravity = 9.9; // not allowed, this will cause a compile error const double gravity; // compiler error, must be initialized upon definition

Pointers to Constant Variables We can change pointer to point to any other variable We cannot change value of object pointed by ptr const int *ptr; // preferred use of const before type int const *ptr; // okay, but not preferred int main() { int x = 100, y = 200; const int * ptr = &x; // ptr is a pointer pointed to a “const int” ptr = &y; // okay. ptr itself is not constant *ptr = 20; // error. ptr is pointed to a “const int”, which is a // constant that cannot be changed return 0; }

Constant Pointers to Variables We cannot change pointer to point to any other variable We can change value of object pointed by ptr int * const ptr = &x; // must be initialized int main() { int x = 100, y = 200; int * const ptr = &x; // ptr is a const pointer pointed to a “int” ptr = &y; // error. ptr itself is a constant pointer *ptr = 20; // ok. ptr is pointed to a “int”, which can be changed return 0; }

Constant Pointers to Constant Variables We cannot change pointer to point to any other variable We can change value of object pointed by ptr const int * const ptr = &x; // must be initialized int main() { int x = 100, y = 200; const int * const ptr = &x; // ptr is a const pointer pointed to a “const int” ptr = &y; // error. ptr itself is a constant pointer *ptr = 20; // error. ptr is pointed to a “const int”, which cannot be changed return 0; }

* const int * ptr; int * const ptr; const int * const ptr; Const and pointers Description of the variable it pointed to * Description of pointert itself const int * ptr; int * const ptr; const int * const ptr;

Constant references Reference must have the same type as the variable it reference to Reference is always a constant; we cannot make a reference refer to a different object We cannot change the object referred by a const reference int main() { const int x = 100; int& ref = &x; // error. Different type const int& ref = &x; // okay ref = 20; // error. Constant value cannot be change return 0; }

Description of the variable it refers to Const and references Description of the variable it refers to & Only reference’s name const int & ref; int & ref;

Constant classes Can this be compiled? #include <iostream> using namespace std; class Date { public: Date(){} Date(int y, int m, int d) : m_year(y), m_month(m), m_date(d) {} void setYear(int y) {m_year = y;} void printMonth() cout << m_month << endl; } void printDate() const cout << m_date << endl; int m_year; private: int m_month; int m_date; }; int main() { const Date date1; date1.m_year = 2011; date1.setYear(2012); const Date date2(2010,10,11); date2.printMonth(); date2.printDate(); return 0; } Can this be compiled?

Constant classes Can this be compiled? #include <iostream> using namespace std; class Date { public: Date(){} Date(int y, int m, int d) : m_year(y), m_month(m), m_date(d) {} void setYear(int y) {m_year = y;} void printMonth() cout << m_month << endl; } void printDate() const cout << m_date << endl; int m_year; private: int m_month; int m_date; }; int main() { const Date date1; date1.m_year = 2011; date1.setYear(2012); const Date date2(2010,10,11); date2.printMonth(); date2.printDate(); return 0; } Can this be compiled?

Constant classes Instantiated class objects can also be made const by using the const keyword Once a const class object has been initialized via constructor, any attempt to modify the member variables of the object is disallowed: const class objects can only call const member functions const Date date1; // initialize using default constructor const Date date2(2010,10,11); // initialize using parameterized constructor date1.m_year = 2011; // compiler error: violates const date1.setYear(2012); // compiler error: violates const date2.printMonth(); // compiler error: violates const date2.printDate(); // compiler ok: printDate is a const member function

Const member functions A const member function is a member function that guarantees it will not modify the object or call any non- const member functions (as they may modify the object) class Date { public: Date(){} void printMonth() {cout << m_month << endl;} void printDate() const {cout << m_date << endl;} void printAll() const m_year = 1111; // const member function cannot modify the object printMonth(); // const member function cannot call non-const member functions printDate(); } private: int m_year; int m_month; int m_date; };