Protection Levels and Constructors The 'const' Keyword

Similar documents
Object Oriented Programming COP3330 / CGS5409

Introduction to Programming Using Java (98-388)

Lab5. Wooseok Kim

Constructors for classes

And Even More and More C++ Fundamentals of Computer Science

Constants, References

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Advanced Systems Programming

Wednesday, October 15, 14. Functions

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

Lab5. Wooseok Kim

B16 Object Oriented Programming

QUIZ. What is wrong with this code that uses default arguments?

Operator overloading. Conversions. friend. inline

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

Fundamental Concepts and Definitions

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

More on Functions. Lecture 7 COP 3014 Spring February 11, 2018

Object Oriented Design

ADTs & Classes. An introduction

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

B16 Object Oriented Programming

VARIABLES AND TYPES CITS1001

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Array Elements as Function Parameters

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Short Notes of CS201

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Constructors.

Lecture 18 Tao Wang 1

CS201 - Introduction to Programming Glossary By

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

COEN244: Class & function templates

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

PIC 10A. Lecture 17: Classes III, overloading

CS 162, Lecture 25: Exam II Review. 30 May 2018

CMPE-013/L. Introduction to C Programming. Unit testing

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Friend Functions and Friend Classes

CPSC 427a: Object-Oriented Programming

Bruce Merry. IOI Training Dec 2013

Lecture 04 Introduction to pointers

APCS Semester #1 Final Exam Practice Problems

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

ECE 461 Fall 2011, Final Exam

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

CMSC 132: Object-Oriented Programming II

G Programming Languages - Fall 2012

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

OBJECT ORIENTED PROGRAMMING USING C++

CS24 Week 3 Lecture 1

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Object-Oriented Principles and Practice / C++

CS24 Week 4 Lecture 1

10. Functions (Part 2)

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

1 Shyam sir JAVA Notes

a data type is Types

Chapter 11: Compiler II: Code Generation

Intermediate Programming, Spring 2017*

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Lecture 7. Log into Linux New documents posted to course webpage

Exercise 7 References, Arrays, Vectors

C++ (Non for C Programmer) (BT307) 40 Hours

Chapter 7 Constructors and Other Tools. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Semantic Analysis. Lecture 9. February 7, 2018

Cpt S 122 Data Structures. Templates

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Chapter 10 Introduction to Classes

C++ (classes) Hwansoo Han

COMP322 - Introduction to C++ Lecture 07 - Introduction to C++ classes

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

C++ Important Questions with Answers

Object-Oriented Programming, Iouliia Skliarova

by Pearson Education, Inc. All Rights Reserved. 2

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

by Pearson Education, Inc. All Rights Reserved.

Computer Components. Software{ User Programs. Operating System. Hardware

CSC 210, Exam Two Section February 1999

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Transcription:

Protection Levels and Constructors The 'const' Keyword

Review: const Keyword Generally, the keyword const is applied to an identifier (variable) by a programmer to express an intent that the identifier should not be used to alter data it refers to in some context (scope). The compiler enforces this intent, for example: In the above example, x is an identifier that refers to integer data in memory and the compiler enforces that x should not be used in a way that could result in a change to that data. Another example: In this example, the compiler does not allow the 'int *' x_ptr to point to the address of x (which is of type 'const int *'). This is because x_ptr could then be used to change the data stored for x.

Review: const parameters Recall that there are two methods of passing arguments to functions in c++: By Value A copy of the argument is made and the function acts upon the copy By Reference No copy of the argument is made, the function parameter is an identifier that refers to the same data as the argument (like an alias for the argument). The method used to pass arguments is indicated by the function's parameters: void foo(int x); //x is passed by value void foo(int &x); //x is passed by reference and can add const to either: void foo(const int x); //the copy x cannot be altered (no use for this) void foo(const int &x); //the data identifier x refers to cannot be altered by function foo Passing by const reference can be very useful to the runtime overhead of copying data while still assuring that a function will not change the original.

Passing Objects By const Reference Objects can also be passed by const reference to avoid copy overhead: friend Fraction Add(const Fraction& f1, const Fraction& f2); Just like with other types, the compiler will enforce that an object passed by const reference will not be used in a way that may change it's member data.

Const Member Functions Remember that any call to a member function has a calling object Fraction f1; /* instantiate a fraction object */ f1.evaluate(); /* f1 is the calling object */ Since a member function has access to the calling objects data, we may want to make sure the calling object is never altered by a member function. We call this a const member function, and it is indicated by using the const keyword after the member function declaration AND definition.

Declaring const Objects Declaring primitive type variables as const is easy. Remember that they must be initialized on the same line: const int SIZE = 10; const double PI = 3.1415; Objects can be declared as const oin a similar fashion. The constructor will always run to intialize the object, but after that, the object's member data cannot be changed const Fraction ZERO; // this fraction is fixed at 0/1 const Fraction FIXED(3,4); // this fraction is fixed at 3/4 To ensure that a const object cannot be changed, the compiler enforces that a const object may only call const member functions. See const_fraction example.

Const Member Data Member data of a class can also be declared const. This is a little tricky, because of certain syntax rules. Remember, when a variable is declared with const in a normal block of code, it must be initialized on the same line: const int SIZE = 10; However, it is NOT legal to initialize the member data variables on their declaration lines in a class declaration block: But a const declaration cannot be split up into a regular code block. This attempt at a constructor definition would also not work, if Z were const:

Initialization List We can use a special area of a constructor called an initialization list to overcome the problem of initializing const object members. Initialization lists have the following format: The initialization list above will set member_var1 to 10 and member_var2 to the value passed as p1 to the constructor. See init_list.cpp example.