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

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

QUIZ. Source:

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

QUIZ Friends class Y;

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?

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

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

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

Lecture 13: more class, C++ memory management

Software Design and Analysis for Engineers

Ch. 12: Operator Overloading

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

C++ for Java Programmers

JAVA: A Primer. By: Amrita Rajagopal

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Fall 2017 CISC/CMPE320 9/27/2017

QUIZ. Can you find 5 errors in this code?

Linked lists Tutorial 5b

CS3157: Advanced Programming. Outline

Lecture 14: more class, C++ streams

Writing Functions in C

CPSC 427: Object-Oriented Programming

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

PIC 10A Objects/Classes

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Introduction Of Classes ( OOPS )

Object-Oriented Programming, Iouliia Skliarova

Exercise: Inventing Language

aggregate data types member functions cis15 advanced programming techniques, using c++ lecture # II.1

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Unix System Programming - Chapter 2, part a

QUIZ. How could we disable the automatic creation of copyconstructors

Interview Questions of C++

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

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Exam 3 Chapters 7 & 9

QUIZ. How could we disable the automatic creation of copyconstructors

377 Student Guide to C++

Chapter 1 Getting Started

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Memory and Pointers written by Cathy Saxton

D Programming Language

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

The Dynamic Typing Interlude

A brief introduction to C programming for Java programmers

CSE 303: Concepts and Tools for Software Development

Pointers, Dynamic Data, and Reference Types

Sixth lecture; classes, objects, reference operator.

Software Design and Analysis for Engineers

CPSC 427: Object-Oriented Programming

CIS 190: C/C++ Programming. Classes in C++

Lecture 8 Data Structures

Introduction to C++ with content from

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Ch. 10: Name Control

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

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

Constants, References

VARIABLES AND TYPES CITS1001

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

CS Lecture #6. Administrative... Assignment #2 Graded NN Turned In, Average Grade: XX Assignment #3 due on Thursday

Pointers II. Class 31

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

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

CS Lecture #14

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

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

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

Lecture 11. Demonstration #1. More Inheritance, More Constructors. Absolute C++ Chapter 15. Virtual Functions. Overriding

OBJECT ORIENTED PROGRAMMING USING C++

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

Object Oriented Programming in C#

Section Notes - Week 1 (9/17)

A class is a user-defined type. It is composed of built-in types, other user-defined types and

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

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

CSC 533: Organization of Programming Languages. Spring 2005

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

CSE 12 Abstract Syntax Trees

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

内存管理. Memory management

EL2310 Scientific Programming

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

Object-Oriented Programming for Scientific Computing

Classes Ch

C++ Programming Lecture 4 Software Engineering Group

Data Structure Series

Pointers and References

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

CS24 Week 3 Lecture 1

This book is about using Visual Basic for Applications (VBA), which is a

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

Transcription:

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

Clarification of the concept of INTERFACE The interface we ve been talking about in OOP is not the man-machine interface between a program and its user. Rather, it is the interface between a class and other classes or user code that interact with that class. example

A student was asked to point out interface elements in this code: This is the interface!

Clarification of the concept of INTERFACE cout is not part of the interface; it is an object (of the ostream class). Its interface contains these and other tools : the << operator the write and width member functions the precision member function the rdbuf pointer

Ch. 5: Hiding the Implementation

Remember from ch.4 A typical C library contains structs and associated functions to act on those structs. What we have changed from C to C++:

In C++, we take the functions that are conceptually associated with the struct and make them literally associated by: Putting the function declarations inside the scope of the struct These are now called member functions or methods.

Changing the way functions are called: s.add(); Eliminating the passing of the structure address as the first argument Adding a new type name to the program (so we don t have to create a typedef for the struct tag).

Further problems with C libraries: control and safety text When you create a library, you establish a relationship with the client programmer who uses that library to build an application or another library. In a C struct, as with most things in C, there are no rules. Client programmers can do anything they want with that struct, and there s no way to force any particular behaviors.

text For example, even though you saw in the last chapter the importance of the functions named initialize( ) and cleanup( ), the client programmer has the option not to call those functions. And even though you would really prefer that the client programmer not directly manipulate some of the members of your struct, in C there s no way to prevent it.

There are two reasons for controlling access to members: text 1 - To keep the client programmer s hands off tools they shouldn t touch, tools that are necessary for the internal machinations of the data type, but not part of the interface the client programmer needs to solve their particular problems. This is actually a service to client programmers because they can easily see what s important to them and what they can ignore.

There are two reasons for controlling access to members: text 2 - To allow the library designer to change the internal workings of the structure without worrying about how it will affect the client programmer. In the Stack example in the last chapter, you might want to allocate the storage in big chunks, for speed, rather than creating new storage each time an element is added. If the interface and implementation are clearly separated and protected, you can accomplish this and require only a relink by the client programmer. And this is why we have

C++ access control C++ introduces three new keywords to set the boundaries in a structure: public, private, and protected. These access specifiers are used only in a struct (or class) declaration, and they change the boundary for all the declarations that follow them. Whenever you use an access specifier, it must be followed by a colon. example

By default, the members of a struct are public

C++ access control For backward compatibility, C++ accepts the keyword struct, although in new programs we shall always use class instead!:

In a class, however, the members are private by default

In a struct, we can also control the access with private and public

QUIZ: Is either of the new statements legal? a.i = 42; b.j = 43; }

QUIZ What s wrong in this code?

Solution While it s OK for a member function to access the private member width a global function isn t allowed to access it.