Classes in C++98 and C++11

Similar documents
CSC1322 Object-Oriented Programming Concepts

CMPT 135: Midterm Answer Key

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

Short Notes of CS201

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

CS201 - Introduction to Programming Glossary By

CSE 303: Concepts and Tools for Software Development

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

CS24 Week 3 Lecture 1

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

CS201 Some Important Definitions

CSE 333 Midterm Exam July 24, Name UW ID#

C++ Constructor Insanity

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

Intermediate Programming, Spring 2017*

PIC 10A Objects/Classes

Written by John Bell for CS 342, Spring 2018

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Review: C++ Basic Concepts. Dr. Yingwu Zhu

2 ADT Programming User-defined abstract data types

Abstract Data Types (ADT) and C++ Classes

EL2310 Scientific Programming

PIC10B/1 Winter 2014 Exam I Study Guide

More class design with C++ Starting Savitch Chap. 11

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

Pointers, Dynamic Data, and Reference Types

OBJECT ORIENTED PROGRAMMING

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

COMP6771 Advanced C++ Programming

04-19 Discussion Notes

CS 103 Unit 10 Slides

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

Programming, numerics and optimization

CPSC 427: Object-Oriented Programming

A brief introduction to C++

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

CSE 333 Midterm Exam July 24, 2017 Sample Solution

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Assignment of Structs

CPSC 427: Object-Oriented Programming

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

dynamically allocated memory char* x = new char; int* x = new int[n]; ???...?

COMP6771 Advanced C++ Programming

CSE 333 Midterm Exam 7/27/15 Sample Solution

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Initializing and Finalizing Objects

Financial computing with C++

Interview Questions of C++

CS

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CS 103 Unit 10 Slides

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CS304 Object Oriented Programming Final Term

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Operator overloading. Conversions. friend. inline

Introduction to Programming Using Java (98-388)

Intermediate Programming, Spring 2017*

Object-Oriented Programming

Introducing C++ to Java Programmers

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

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

Ch. 12: Operator Overloading

Set Implementation Version 1

Object-Oriented Programming for Scientific Computing

Intermediate Programming & Design (C++) Classes in C++

Common Misunderstandings from Exam 1 Material

C++_ MARKS 40 MIN

Lecture 14: more class, C++ streams

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Vector and Free Store (Vectors and Arrays)

Financial computing with C++

Homework 4. Any questions?

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Assignment of Objects

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

C++ Class Details, Heap

G52CPP C++ Programming Lecture 20

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

COMP6771 Advanced C++ Programming

An Introduction to C++

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

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

G52CPP C++ Programming Lecture 13

Object Reference and Memory Allocation. Questions:

Fast Introduction to Object Oriented Programming and C++

Pointers II. Class 31

Worksheet 5 Chapter 6

The paramaterless ctor (aka default ctor)

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

ComS 228 Exam 1. September 27, 2004

Stack memory - "scratch pad" memory that is used by automatic variables.

IS0020 Program Design and Software Tools Midterm, Fall, 2004

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

Transcription:

Classes in C++98 and C++11 January 10, 2018 Brian A. Malloy Slide 1 of 38

1. When we refer to C++98, we are referring to C++98 and C++03, since they differ only slightly. C++98 contained 3 types of constructors, but C++11 added a move constructor. The C++ class is one of the most difficult constructs to write correctly Some methods are written silently by the compiler Some methods are required w/ pointers These slides describe classes, including 3 of the 4 constructors. We describe move semantics in separate slides Slide 2 of 38

2. Unit of encapsulation: Public operations Private implementation Abstraction: string: abstracts char* of C student sprite C++ Classes: easy to write, hard to get right! Need lots of examples Slide 3 of 38

2.1. The actions of a class Constructors: initialize data attributes Constructors: allocate memory when needed Destructor: De-allocate memory when necessary Slide 4 of 38

2.2. C++ class vs C++ struct Default access is only difference Generally, structs used for data Classes used for data and methods Bad class Good Class class Student { class Student { public: string name; string name; float gpa; float gpa; }; }; Slide 5 of 38

2.3. Object: an instantiated class C++ objects can be stored on the stack: class A{}; int main() { A a, b; }; Or on the heap: int main() { A *a = new A; A *b = new B; }; Compiler does stack; programmer does heap! Slide 6 of 38

3. Constructors & Destructors No name and cannot be called directly Init data through initialization lists Constructor types are distinguished by their parameters. The four types of constructors are: 1. Default 2. Conversion 3. Copy 4. Move (which we describe in later slides) Slide 7 of 38

Constructor examples: class Student { public: Student(); Student(char * n); Student(const Student&); Student(Student&&); ~Student(); private: char* name; }; // default: no params // convert // copy: param is Student // move // destructor (no params) Slide 8 of 38

3.1. Default Constructor 1 c l a s s s t r i n g { 2 p u b l i c : 3 s t r i n g ( ) : buf ( new char [ 1 ] ) { buf [ 0 ] = \ 0 ; } 4 p r i v a t e : 5 char buf ; 6 } ; No parameters to default constructor Uses an initialization list to create a buffer of length 1 characters: buf(new char[1]) Places the null termination character into the newly created buffer. cppreference: Constructs an empty string, with a length of zero characters. Slide 9 of 38

3.2. Prefer initialization to assignment Initialization is more efficient for data members that are objects (demo later) Only way to pass parameters to base class: class Person { public: Person(int a) : age(a) {} private: int age; }; class Student : public Person { public: Student(int age, float g) : Person(age), gpa(g) {} private: float gpa; }; Slide 10 of 38

3.3. Init performed in order of declare In Student, the constructor will initialize iq first, then age, because iq appears first in declaration (line 5). Initialization list not needed for built-in types. 1 c l a s s Student { 2 p u b l i c : 3 Student ( i n t a ) : age ( a ), i q ( age +100) {} 4 p r i v a t e : 5 i n t i q ; 6 i n t age ; 7 } ; Slide 11 of 38

3.4. Conversion Constructor 1 c l a s s s t r i n g { 2 p u b l i c : 3 s t r i n g ( const char b ) : 4 buf (new char [ s t r l e n ( b )+1]) { 5 s t r c p y ( buf, b ) ; 6 } 7 p r i v a t e : 8 char buf ; 9 } ; Converts b, on line 3, into a string strlen returns the size of the c-string, not including the null termination On line 4 we allocate strlen(b)+1 bytes, where +1 allows for the null termination Slide 12 of 38

3.5. Copy Constructor 1 c l a s s s t r i n g { 2 p u b l i c : 3 s t r i n g ( const s t r i n g& s ) : 4 buf (new char [ s t r l e n ( s. buf )+1]) { 5 s t r c p y ( buf, s. buf ) ; 6 } 7 p r i v a t e : 8 char buf ; 9 } ; Copy constructor uses the parameter s, line 3, to make a deep copy. Notice the parameter transmission mode: const& Slide 13 of 38

3.6. Destructor 1 c l a s s s t r i n g { 2 p u b l i c : 3 s t r i n g ( ) { d e l e t e [ ] buf ; } 4 p r i v a t e : 5 char buf ; 6 } ; We used new char[] in the constructors to allocate an array We use delete [] on line 3 to indicate that we are deallocating an array. Slide 14 of 38

4. What if I don t write one I write this: class Empty{}; Compiler writes this: class Empty { public: Empty(); Empty(const Empty &); ~Empty(); Empty& operator=(const Empty &); }; Slide 15 of 38

4.1. Here s what they look like: inline Empty::Empty() {} inline Empty::~Empty() {} inline Empty * Empty::operator&() {return this;} inline const Empty * Empty::operator&() const { return this; } The copy constructor & assignment operator simply do a member wise copy, i.e., shallow. Note that the default copy/assign may induce leak/dble free Slide 16 of 38

4.2. What can go wrong? Consider: 1 #i n c l u d e <iostream > 2 #i n c l u d e <c s t r i n g > 3 c l a s s s t r i n g { 4 p u b l i c : 5 s t r i n g ( ) : buf ( new char [ 1 ] ) { buf [ 0 ] = \ 0 ; } 6 s t r i n g ( const char s ) : 7 buf (new char [ s t r l e n ( s )+1]) { 8 s t r c p y ( buf, s ) ; 9 } 10 s t r i n g ( ) { d e l e t e [ ] buf ; } 11 p r i v a t e : 12 char buf ; 13 } ; 14 i n t main ( ) { 15 s t r i n g a, b ( a ) ; 16 } Slide 17 of 38

4.3. Shallow Copy The previous example gives undefined behavior, usually double free. Default constructor creates string a, line 15 However, the compiler generated copy constructor simply copies the address in a.buf into b.buf, which makes a shallow copy In memory it looks like: Slide 18 of 38 Deletion of a is okay; deletion of b is a problem!

4.4. Prevent Compiler Generated Ctors To address the problem of shallow copies, C++98 developers suggested placing signatures in private (line 10). Use of copy constructor won t compile This is Item #6 in Meyers Effective C++. 1 #i n c l u d e <iostream > 2 #i n c l u d e <c s t r i n g > 3 c l a s s s t r i n g { 4 p u b l i c : 5 s t r i n g ( ) ; 6 s t r i n g ( c o n s t char s ) ; 7 s t r i n g ( ) { d e l e t e [ ] buf ; } 8 p r i v a t e : 9 char buf ; 10 s t r i n g ( c o n s t s t r i n g &); 11 } ; Slide 19 of 38

4.5. C++11 Solution If the special syntax = delete is used, the function is defined as deleted (line 8) Any use of a deleted function is ill-formed and the program will not compile. 1 #i n c l u d e <iostream > 2 #i n c l u d e <c s t r i n g > 3 c l a s s s t r i n g { 4 p u b l i c : 5 s t r i n g ( ) ; 6 s t r i n g ( c o n s t char s ) ; 7 s t r i n g ( ) { d e l e t e [ ] buf ; } 8 s t r i n g ( c o n s t s t r i n g &) = d e l e t e ; 9 p r i v a t e : 10 char buf ; 11 } ; Slide 20 of 38

4.6. Canonical Form James Coplien: a class with pointer data should be in Canonical Form, aka The Rule of Three, which means programmer writes: 1. Copy constructor 2. Copy assignment 3. Destructor Canonical form prevents shallow copy Slide 21 of 38

4.7. Compiler generated Shallow Copy Slide 22 of 38

4.8. Canonical Form Deep Copy Slide 23 of 38

5. Meyers, in Item #4 of Effective C++, says prefer initialization to assignment in ctors. The two examples in Sections 5.1 and 5.2 illustrate a considerable efficiency boost when using initialization rather than assignment. The two examples are exactly the same except for line 18: Section 5.1, line 18, assignment:: TestAssign(char* n) { name = n; } Section 5.2, line 18, initialization list: TestAssign(char* n) : name(n) { } Slide 24 of 38

5.1. assign Example 1 2 #i n c l u d e <iostream > #i n c l u d e <c s t r i n g > 3 c l a s s s t r i n g { 4 p u b l i c : 5 s t r i n g ( ) { std : : cout << d e f a u l t << std : : endl ; } 6 s t r i n g ( c o n s t char b ) { std : : cout << c o n v e r t << std : : endl ; } 7 s t r i n g ( c o n s t s t r i n g& s ) { std : : cout << copy << std : : endl Static ; } Class Variabless 8 s t r i n g ( ) { std : : cout << d e s t r u c t o r << std : : endl ; } 9 s t r i n g& o p e r a t o r =( c o n s t s t r i n g &) { 10 std : : cout << a s s i g n << std : : endl ; 11 r e t u r n t h i s ; 12 } 13 p r i v a t e : 14 char buf ; 15 } ; 16 c l a s s TestAssign { 17 p u b l i c : 18 TestAssign ( char n ) { name = n ; } 19 p r i v a t e : Slide 25 of 38 20 s t r i n g name ; 21 } ; 22 i n t main ( ) { TestAssign t e s t ( dog ) ; }

The output for the previous program in Section 5.1 is: default convert assign destructor destructor The first line of output, default, results when the compiler tries to initialize name in an initialization list. Since there isn t one, it uses the default constructor. Slide 26 of 38

The next two lines of output, convert and assign result from name = n, which doesn t match any function call as written. However, if n is converted to a string then it will match: string.operator=(string). The first destructor call results when the compiler reallocates the temporary string that was created with the convert. The final destructor call results when the compiler deallocates name in Student. Slide 27 of 38

5.2. Init Example 1 2 #i n c l u d e <iostream > #i n c l u d e <c s t r i n g > 3 c l a s s s t r i n g { 4 p u b l i c : 5 s t r i n g ( ) { std : : cout << d e f a u l t << std : : endl ; } 6 s t r i n g ( c o n s t char b ) { std : : cout << c o n v e r t << std : : endl ; } 7 s t r i n g ( c o n s t s t r i n g& s ) { std : : cout << copy << std : : endl Static ; } Class Variabless 8 s t r i n g ( ) { std : : cout << d e s t r u c t o r << std : : endl ; } 9 s t r i n g& o p e r a t o r =( c o n s t s t r i n g &) { 10 std : : cout << a s s i g n << std : : endl ; 11 r e t u r n t h i s ; 12 } 13 p r i v a t e : 14 char buf ; 15 } ; 16 c l a s s T e s t I n i t { 17 p u b l i c : 18 T e s t I n i t ( char n ) : name ( n ) { } 19 p r i v a t e : Slide 28 of 38 20 s t r i n g name ; 21 } ; 22 i n t main ( ) { T e s t I n i t t e s t ( dog ) ; }

The output for the previous program in Section 5.2 is: convert destructor Clearly, the initialization list, name(n), is a use of the conversion constructor in string to convert n to a string. Slide 29 of 38

6. Slide 30 of 38

7. Principle of Least Privilege A const class method cannot change any of the class data attributes. Use const as much as possible! Can reduce debugging Provides documentation Allow a function enough data access to accomplish its task and no more! Most beginners take them all out... probably need more! Slide 31 of 38

7.1. Example of Least Privilege class string { public: string(const char* n) : buf(new char[strlen(n)+1]) { strcpy(buf, n); } const char* get() const { return buf; } private: char *buf; }; std::ostream& operator<<(std::ostream& out, const string& s) { return out << s.get(); } int main() { string x("hello"); std::cout << x.get() << std::endl; } Slide 32 of 38

7.2. What s wrong with this class? class Student { public: Student(const char * n) : name(n) { } const getname() const { return name; } void setname(char *n) { name = n; } private: char *name; }; Slide 33 of 38

8. Interface vs Implementation Interface goes in.h file: class Student { public: getname() const { return name; } getgpa() const { return gpa; } private: char * name; float gpa; }; ostream& operator <<(ostream &, const Student &); Implementation goes in.cpp file: ostream & operator<<(ostream& out, const Student& s) { out << s.getname() << s.getgpa(); return out; } Slide 34 of 38

9. Useful as projects grow larger with multiple files. Consist of definitions, Followed by sequences of 2 line commands. First line begins with < id >:, followed by dependencies of < id >. Second line is the rule to make < id >; this line MUST be preceded by a tab To use the make file type: simply: make make {< id >}, or Slide 35 of 38

9.1. Simple makefile CCC=g++ FLAGS=-Wall main: main.o Binary.o $(CCC) $(FLAGS) -o main main.o Binary.o main.o: main.cpp Binary.h $(CCC) $(FLAGS) -c main.cpp Binary.o: Binary.cpp Binary.h $(CCC) $(FLAGS) -c Binary.cpp clean: rm -f main *.o core Slide 36 of 38

9.2. Discussion of Makefile $(CCC) permits us to easily switch to another compiler; e.g. clang++ make clean will clean the directory of large files -o option creates an executable -c option creates.o file Slide 37 of 38

10. 1 c l a s s s t r i n g { 2 p u b l i c : 3 s t r i n g ( ) ; 4 s t r i n g ( const char ) ; 5 s t r i n g ( const s t r i n g &); 6 s t r i n g ( ) ; 7 s t r i n g operator+( const s t r i n g &); 8 s t r i n g& operator=( const s t r i n g &); 9 char& operator [ ] ( i n t index ) ; 10 const char& o p e r a t o r [ ] const ( i n t index ) ; 11 p r i v a t e : 12 char buf ; 13 } ; 14 ostream& operator <<(ostream &, const s t r i n g &); 15 s t r i n g operator+( const char, const s t r i n g &); Overloaded operators will be described separately. Slide 38 of 38