Ch. 10: Name Control

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

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

QUIZ Friends class Y;

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

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. How could we disable the automatic creation of copyconstructors

QUIZ. How could we disable the automatic creation of copyconstructors

Ch. 12: Operator Overloading

QUIZ. Source:

September 10,

QUIZ. Can you find 5 errors in this code?

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

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

Scope. Chapter Ten Modern Programming Languages 1

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

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

Software Design and Analysis for Engineers

Short Notes of CS201

The New C Standard (Excerpted material)

G52CPP C++ Programming Lecture 9

CS201 - Introduction to Programming Glossary By

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

Programming in C and C++

Linked lists Tutorial 5b

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?

Object Oriented Software Design II

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

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

C++11/14 Rocks. Clang Edition. Alex Korban

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Fast Introduction to Object Oriented Programming and C++

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

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

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

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

Absolute C++ Walter Savitch

BLM2031 Structured Programming. Zeyneb KURT

CMPE-013/L. Introduction to C Programming

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CS201 Some Important Definitions

Pointers and References

Review of the C Programming Language for Principles of Operating Systems

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Thursday, February 16, More C++ and root

C: How to Program. Week /Mar/05

EL6483: Brief Overview of C Programming Language

Introduction To C#.NET

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

What is Class? Remember

CSE 303: Concepts and Tools for Software Development

Problem Solving with C++

pointers + memory double x; string a; int x; main overhead int y; main overhead

Chapter 2 - Introduction to C Programming

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

D Programming Language

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras

CE221 Programming in C++ Part 1 Introduction

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

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

A Fast Review of C Essentials Part I

Memory and Pointers written by Cathy Saxton

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

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

clean way to do separate compilation. ECS140A Programming Languages 12-1

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Upcoming Features in C# Mads Torgersen, MSFT

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Friend Functions and Friend Classes

CS201 Latest Solved MCQs

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

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

(Refer Slide Time 01:41 min)

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

History C++ Design Goals. How successful? Significant constraints. Overview of C++

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Getting Deeper into. C++ Semantics. EECS 230 Winter 2018

10. Abstract Data Types

C++ Programming Lecture 1 Software Engineering Group

Chapter 1 Getting Started


Chapter 5. Names, Bindings, and Scopes

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

10. Functions (Part 2)

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

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

C++ Programming Lecture 7 Software Engineering Group

Static Program Analysis Part 9 pointer analysis. Anders Møller & Michael I. Schwartzbach Computer Science, Aarhus University

class objects instances Fields Constructors Methods static

CSE 230 Intermediate Programming in C and C++ Functions

Computer Science 306 Study Guide

C++ Yanyan SHEN. slide 1

Abstract Data Types and Encapsulation Concepts

Transcription:

Ch. 10: Name Control

Static elements from C The static keyword was overloaded in C before people knew what the term overload meant, and C++ has added yet another meaning. The underlying concept with all uses of static seems to be something that holds its position (like static electricity), whether that means a physical location in memory or visibility within a file.

Static elements from C In both C and C++ the keyword static has two basic meanings: Allocated once at a fixed address; that is, the object is created in a special static data area (a.k.a. static storage) rather than on the stack each time a function is called. See next slide Local to a particular translation unit (and local to a class scope in C++, as you will see later). Here, static controls the visibility (a.k.a. scope) of a name, so that name cannot be seen outside the translation unit or class. This also describes the concept of linkage, which determines what names the linker will see.

Static storage Contains: Local auto objects Contains: Objects allocated dynamically Originally it meant Block Started by Symbol Contains: Local, static and constant objects Unitialized global objects Static storage Remember: In C (and C++) uninitialized static variables are initialized by default with 0. Source: Leo Ferres, Memory management in C: The heap and the stack

Static elements from C Review the next part of this section until Other storage class specifiers

Namespaces In a large project, lack of control over the global name space can cause problems. To solve these problems for classes, vendors often create long complicated names that are unlikely to clash, but then you re stuck typing those names. It s not an elegant, language-supported solution.

Namespaces You can subdivide the global name space into more manageable pieces using the namespace feature of C++. The namespace keyword, similar to class, struct, enum, and union, puts the names of its members in a distinct space.

Namespaces A namespace definition can appear only at global scope, or nested within another namespace. No terminating semicolon is necessary after the closing brace of a namespace definition. A namespace definition can be continued over multiple header files using a syntax that, for a class, would appear to be a redefinition:

A namespace name can be aliased to another name, so you don t have to type an unwieldy name created by a library vendor:

Static members in C++ Sometimes we need a single storage space to be used by all objects of a class. We could use a global variable, but this is not very safe: Global data can be modified by anyone Its name can clash with other identical names in a large project. It would be ideal if the data could be stored as if it were global, but be hidden inside a class, and clearly associated with that class. example on next slide

The initialization of a static member must be done outside of the class declaration, in the definitions section (file)! Important: Do not confuse with the static constants from Ch.8!

Actually, even the static integer constants from Ch.8 can be defined outside the class (It is consistent with ODR!):

QUIZ Define a class Turtle with a static data member named pop (population), initialized with zero. The constructor increments pop.

Static arrays (const or not) With static consts of integral types you can provide the definitions inside the class, but for everything else (including arrays of integral types, even if they are static const) you must provide a single external definition. example on next slide

Note that the static keyword is not repeated at initialization!

Note: C++11 has introduced the related qualifier constexpr, which allows more definitions inside the class:

SKIP Nested and local classes

Static member functions static member functions, like static data members, work for the class as a whole rather than for a particular object of a class. Instead of making a global function that lives in and pollutes the global or local namespace, you bring the function inside the class.

Static member functions You can call a static member function in the ordinary way, with the dot or the arrow, in association with an object. However, it s more typical to call it by itself, without any specific object, using the scope-resolution operator:

Static member functions Cannot access ordinary data members, only static data members Can call only other static member functions Normally, the address of the current object (this) is quietly passed in when any member function is called but a static member has no this (which is the reason it cannot access ordinary members) Thus, we get the tiny increase in speed afforded by a global function because a static member function doesn t have the extra overhead of passing this. At the same time you get the benefits of having the function inside the class.

Static member function example

QUIZ: Is this code correct?

Solution: Yes. Although the function call in the main program looks weird, it is legal for a static member function!

SKIP Static initialization dependency Alternate linkage specifications No homework assigned for this chapter! Individual work (preparation for midterm): End-of-chapter 2, 3, 4, 16