QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Similar documents
Ch. 10: Name Control

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ Friends class Y;

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

Ch. 11: References & the Copy-Constructor. - continued -

Ch. 12: Operator Overloading

QUIZ. How could we disable the automatic creation of copyconstructors

QUIZ. How could we disable the automatic creation of copyconstructors

QUIZ. What are 3 differences between C and C++ const variables?

Ch. 3: The C in C++ - Continued -

Cpt S 122 Data Structures. Introduction to C++ Part II

QUIZ. Can you find 5 errors in this code?

G52CPP C++ Programming Lecture 9

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

This wouldn t work without the previous declaration of X. This wouldn t work without the previous declaration of y

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

Abstraction in Software Development

Ch02. True/False Indicate whether the statement is true or false.

QUIZ. Source:

Lecture 18 Tao Wang 1

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Computer Science 306 Study Guide

CS201 Some Important Definitions

Chapter 10 Introduction to Classes

C Programming for Engineers Functions

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Object Oriented Design

Fast Introduction to Object Oriented Programming and C++

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CS304 Object Oriented Programming Final Term

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

Object-Oriented Programming

ADTs & Classes. An introduction

Functions and Recursion

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Exercise 6.2 A generic container class

Thursday, February 16, More C++ and root

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

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

Week 8: Operator overloading

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

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

Constants, References


Introduction to Classes

OBJECT ORIENTED PROGRAMMING USING C++

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

Object-Oriented Programming, Iouliia Skliarova

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

Course Coding Standards

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

EEE-425 Programming Languages (2013) 1

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

What is Class? Remember

Absolute C++ Walter Savitch

Abstract Data Types and Encapsulation Concepts

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

SFU CMPT Topic: Classes

Fundamental Concepts and Definitions

Computer Programming C++ Classes and Objects 6 th Lecture

An Introduction to C++

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Import Statements, Instance Members, and the Default Constructor

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Lecture 10: building large projects, beginning C++, C++ and structs

III. Classes (Chap. 3)

AN OVERVIEW OF C++ 1

CSCI 123 Introduction to Programming Concepts in C++

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

CSI33 Data Structures

Object Oriented Design

A A B U n i v e r s i t y

APCS Semester #1 Final Exam Practice Problems

2 nd Week Lecture Notes

Introduction Of Classes ( OOPS )

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Data Structures (list, dictionary, tuples, sets, strings)

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Unit 5: More on Classes/Objects Notes

15: Polymorphism & Virtual Functions

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Transcription:

QUIZ How do we implement run-time constants and compile-time constants inside classes?

Compile-time constants in classes The static keyword inside a class means there s only one instance, regardless of how many objects of the class are created static consts must be initialized when they are defined (all other data members must be initialized in the constructor or in other member functions) Run-time constants in classes Use the constructor initializer list. This means each object of this class can have a different constant value

QUIZ Is the destructor called in the right place in this code? Explain!

Yes! Although the destructor is called when the object (f) ends its lifetime, for a static object the lifetime is the entire program!

Ch. 9: Inline functions

Two problems w/preprocessor macros in C++ 1. A macro looks like a function call, but doesn t always act like one. This can create difficult-to-find bugs. This is also true with C 2. Specific to C++: the preprocessor has no permission to access class member data preprocessor macros cannot be used as class member functions. Review the example at beginning of ch.4 notes. More examples on the next slides.

QUIZ What does the preprocessor produce when given this code?

solution What does the preprocessor produce when given this code?

QUIZ What does the preprocessor produce when given this code?

solution What does the preprocessor produce when given this code? The preprocessor is smart enough to recognize strings and not mess with them!

Function-like macros The first rule: never separate the name of the macro from the opening parenthesis No space here!

Expressions may expand inside the macro so that their evaluation precedence is different from what you expect & has less precedence than >=

The problem can be corrected with a little work, but it s unintuitive, and, therefore, error-prone:

QUIZ What does the preprocessor produce when given this code? #define SQUARE(x) x*x SQUARE(2+3)

solution What does the preprocessor produce when given this code? #define SQUARE(x) x*x SQUARE(2+3) 2+3*2+3 11

Even if we do parenthesize the macro correctly, we still have the problem of side-effects: If we apply side-effects to the argument, they will be applied in each point of occurrence (unlike a function, where they only apply once!):...... See full code in text: C09:MacroSideEffects.cpp

A detailed classification of all possible macro-related bugs in C, with examples and explanations can be found at: https://gcc.gnu.org/onlinedocs/gcc-3.0.1/cpp_3.html#sec26

In C++ we have the following new problem: A macro has no concept of the scoping required with member functions:

C++ macro problem: a macro has no concept of the scoping required with member functions Someone wrote this on a forum: What will the code look like when the preprocessor is through? What is the correct way to implement what the programmer wants? Source: https://social.msdn.microsoft.com/forums/vstudio/en-us/607b89fe-10e6-49ca-866d-15aa2047f8c2/define-inside-a-class

C++ macro problem: a macro has no concept of the scoping required with member functions Someone wrote this on a forum: Source: https://social.msdn.microsoft.com/forums/vstudio/en-us/607b89fe-10e6-49ca-866d-15aa2047f8c2/define-inside-a-class

Solution to the C++ macro problem: C++ implements the macro in the compiler, as inline function, which is a true function in every sense. The only difference is that an inline function is expanded in place, like a preprocessor macro, so the overhead of the function call is eliminated.

When the compiler sees such a definition, it puts the function type (the signature combined with the return value) and the function body in its symbol table.

When the compiler later encounters calls to the function, it checks to ensure that the call is correct and that the return value is being used correctly then substitutes the function body for the function call, thus eliminating the overhead.

The inline code does occupy extra space, but, if the function is small, this space can actually be less than the code generated to do an ordinary function call (pushing arguments on the stack and doing the CALL).

Remember the rule many declarations, but only one definition, a.k.a. ODR? For the inline mechanism to work, we must include the header file containing the function declaration and its definition in every file* where the function is used. The compiler makes an exception so we don t end up with multiple definition errors. Of course, all definitions must be identical! * Technically, every translation unit.

Inlines inside classes The inline keyword is not needed inside a class definition. Any function defined inside a class definition is automatically an inline: example on next slide

Example: Both constructors and print function are inline

One of the most important uses of inline inside classes is the access function = small function that reads or changes the state of an object (one or more internal variables). example on next slide

The concept of access functions is further divided into: accessors, a.k.a. getters (to read state information from an object) mutators, a.k.a. setters (to change the state of an object).

In addition, overloading may be used to give the same name to both accessor and mutator: Note the naming problem! That s why a better way is

For complex examples with accessors and mutators, read: C09:Cpptime Stash and Stack with inlines

Inlines & the compiler What the compiler stores in its symbol table for an inline function: Entire prototype Function body (code)

Two cases in which the compiler may ignore the inline directive Function is too complicated (Depends on compiler) E.g. if function contains a loop Function address is taken (implicitly or explicitly [?]) Author means to say that function is accessed via function pointer, e.g.

How the compiler resolves fwd. references in inline functions No inline functions in a class is evaluated until the closing brace of the class declaration!

SKIP the remainder of Ch.9: Hidden activities in constructors & destructors Reducing clutter More preprocessor features Improved error checking No homework for ch. 9 EOL 1