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

Similar documents
Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Outline. 1 About the course

G52CPP C++ Programming Lecture 13

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

Object-Oriented Principles and Practice / C++

2 ADT Programming User-defined abstract data types

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

CSC1322 Object-Oriented Programming Concepts

Overloading Operators in C++

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Short Notes of CS201

CS250 Final Review Questions

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Module Operator Overloading and Type Conversion. Table of Contents

CS201 - Introduction to Programming Glossary By

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

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

CS11 Intro C++ Spring 2018 Lecture 5

Advanced Systems Programming

PIC 10A Objects/Classes

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

CPSC 427: Object-Oriented Programming

04-19 Discussion Notes

explicit class and default definitions revision of SC22/WG21/N1582 =

Programming in C and C++

C++ Constructor Insanity

CS 247: Software Engineering Principles. ADT Design

SFU CMPT Topic: Class Templates

III. Classes (Chap. 3)

Midterm Review. PIC 10B Spring 2018

Tokens, Expressions and Control Structures

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

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

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

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Introduction Of Classes ( OOPS )

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

C++ Important Questions with Answers

COMP 2355 Introduction to Systems Programming

pointers & references

W3101: Programming Languages C++ Ramana Isukapalli

CS3157: Advanced Programming. Outline

Computer Science II CSci 1200 Lecture 18 Operators and Friends

Financial computing with C++

Operator overloading: extra examples

Absolute C++ Walter Savitch

ECE 462 Fall 2011, Second Exam

During the course of writing the Matrix class we will cover some interesting C++ topics. Specically: constructors and destructors, operator

Inheritance, and Polymorphism.

Fast Introduction to Object Oriented Programming and C++

The Compositional C++ Language. Denition. Abstract. This document gives a concise denition of the syntax and semantics

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

CS304 Object Oriented Programming Final Term

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

CS250 Final Review Questions

Initializing and Finalizing Objects

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

AIMS Embedded Systems Programming MT 2017

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

Object-Oriented Programming for Scientific Computing

10. Functions (Part 2)

CS250 Final Review Questions

G52CPP C++ Programming Lecture 16

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Friends and Overloaded Operators

Friends and Overloaded Operators

C++ Functions. Procedural-based Programming. what s a functions

Object-Oriented Principles and Practice / C++

Instantiation of Template class

American National Standards Institute Reply to: Josee Lajoie

Classes in C++98 and C++11

class Array; // Class template declaration class Array public: // T="type",e.g.,int or float Array(int n); // Create array of n elements Array(); // C

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

Operator overloading. Conversions. friend. inline

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Constructor - example

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Intermediate Programming, Spring 2017*

Module 9. Templates & STL

Where do we go from here?

Interview Questions of C++

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

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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.

Use the dot operator to access a member of a specific object.

IS0020 Program Design and Software Tools Midterm, Fall, 2004

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

ECE 461 Fall 2011, Final Exam

Evolution of Programming Languages

Advanced C++ Topics. Alexander Warg, 2017

Programming in C++: Assignment Week 4

Assumptions. History

Programming in C++: Assignment Week 3

10. Object-oriented Programming. 7. Juli 2011

COMP 2355 Introduction to Systems Programming

8. Object-oriented Programming. June 15, 2010

Operators. The Arrow Operator. The sizeof Operator

Transcription:

Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened types Categories User-dened types Concrete classes Concrete classes Abstract classes Class hierarchies A concrete type behaves just like a built-in type its representation is part of its denition, That allows us to place objects on the stack (i.e., in local variables) in other objects in statically allocated memory (e.g., global variables) copy objects assignment of a variable copy-constructing an object value parameter of a function refer to objects directly (not just using pointers or references) initialize objects directly and completely (with a constructor) Classes 4. Classes 3/45 Classes 4. Classes 4/45 Default constructor Default constructor A constructor that can be called without arguments May have parameters with default values Automatically dened if no constructor is dened (in declaration: =default, cannot be called if =delete) If not dened, the type is not default constructible Default constructor with member initializer list. class Bar Bar (int v=100, bool b= false ) : value v, flag b int value ; bool flag ; Default arguments If a constructor can be called without arguments, it is a default constructor. class KomplextTal KomplextTal ( float x=1 ): re(x),im (0) gives the same default constructor as the explicit KomplextTal (): re 1, im 0 Classes : 4. Classes 5/45 Classes : 4. Classes 6/45

Two ways of initializing members With member initializer list in constructor class Bar Bar (int v, bool b) : value v, flag b int value ; bool flag ; Members can have a default initializer, in C++11: Foo () = default ; int value 0 bool flag false prefer default initializer to overloaded constructors or default arguments Member initialization rules class Bar Bar () = default ; Bar(int v, bool b) : value v, flag b int value 0 bool flag true If a member has both default initializer and a member initializer in the constructor, the constructor is used. Members are initialized in declaration order. (Compiler warning if member initializers are in dierent order.) Bar() =default; is necessary to make the compiler generate a default constructor (as another constructor is dened). Classes : 4. Classes 7/45 Classes : 4. Classes 8/45 Prefer default member initializers Prefer default member initializers Use default member initializers if class member variables have default values. Default values through overloaded ctors: risk of inconsistency class Simple Simple () :a(1), b(2), c (3) Simple ( int aa, int bb, int cc = -1) :a(aa), b(bb), c(cc) Simple ( int aa) :a(aa), b(0), c (0) int a; int b; int c; Use default member initializers if class member variables have default values. With default initializers: consistent class Simple Simple () = default ; Simple ( int aa, int bb, int cc) :a(aa), b(bb), c(cc) Simple ( int aa) : a(aa) int a -1 int b -1 int c -1 Classes : 4. Classes 9/45 Classes : 4. Classes 10/45 Default constructor and parentheses Default constructor and initialization The default constructor cannot be called with empty parentheses. Bar b1; Bar b2 Bar be (); // Compiler error! " most vexing parse " Bar b3 (25); // OK Bar * bp1 = new Bar ; Bar * bp2 = new Bar Bar * bp3 = new Bar (); // OK automatically generated default constructor (=default) does not always initialize members global variables are initialized to 0 (or corresponding) local variables are not initialized (dierent meaning from Java) struct A int x; int i; // i is initialized to 0 ( global variable ) A a; // a.x is initialized to 0 ( global variable ) int main () int j; // j is uninitialized int k = int (); // k is initialized to 0 int l // l is initialized to 0 A b; // b.x is uninitialized A c = A(); // c.x is initialized to 0 A d // d.x is initialized to 0 always initialize variables always implement default constructor (or =delete) Classes : 4. Classes 11/45 Classes : 4. Classes 12/45

Delegating constructors (C++11) The pointer this Self reference In C++11 a constructor can call another (like this(...) in Java). struct Test int val ; Test (int v) : val v In a member function, there is an implicit pointer this, pointing to the object the function was called on. (cf. this in Java). Test (int v, int scale ) : Test (v* scale ) // delegation Test (int a, int b, int c) : Test (a+b+c) // delegation A delegating constructor call shall be the only member-initializer. ( A constructor initializes an object completely.) typical use: return *this for operations returning a reference to the object itself Classes : 4. Classes 13/45 Classes : the pointer this 4. Classes 14/45 Constant objects const means I promise not to change this Objects (variables) can be declared const I promise not to change the variable References can be declared const I promise not to change the referenced object a const& can refer to a non-const object common for function parameters Member functions can be declared const I promise that the function does not change the state of the object technically: implicit declaration const T* const this; Constant objects Example const references and const functions class Point Point (int xi, int yi) :xxi,yyi int get_x () const return x; int get_y () const return y; void set_x (int xi) x = xi ; void set_y (int yi) y = yi ; int x; int y; void example ( Point & p, const Point & o) p. set_y (10); cout << "p: " << p. get_x () << "," << p. get_y () << endl ; o. set_y (10); cout << "o: " << o. get_x () << "," << o. get_y () << endl ; passing const Point as this argument discards qualifiers Classes : const for objects and members 4. Classes 15/45 Classes : const for objects and members 4. Classes 16/45 Constant objects Example Note const in the declaration (and denition!) of the member function operator[](int) const: (const is part of the name) class Vector double operator []( int i) const ; // function declaration double * ; double Vector :: operator []( int i) const // function definition return [i]; Constant objects Example: const overloading The functions operator[](int) and operator[](int) const are dierent functions. Example class Vector double & operator []( int i) return [i]; double operator []( int i) const return [i]; double * ; If operator[] is called on a non-const object, a reference is returned const object, a copy is returned The assignment v[2] = 10; only works on a non-const v. Classes : const for objects and members 4. Classes 17/45 Classes : const for objects and members 4. Classes 18/45

User-dened types Concrete classes A concrete type behaves just like a built-in type the representation is part if the denition, That allows us to place objects on the stack (i.e., in local variables) in other objects in statically allocated memory (e.g., global variables) copy objects assignment of a variable copy-constructing an object value parameter of a function refer to objects directly (not just using pointers or references) initialize objects directly and completely (with a constructor) Copy Constructor Is called when initializing an object Is not called on assignment Can be dened, otherwise a standard copy constructor is generated (=default, =delete) void function (Bar ); // by - value parameter Bar b1 (10, false Bar b2b1 // the copy constructor is called Bar b3(b2 ); // the copy constructor is called Bar b4 = b2; // the copy constructor is called function (b2 ); // the copy constructor is called Classes : Copying objects 4. Classes 19/45 Classes : Copying objects 4. Classes 20/45 Copy default Classes Example: Copying the Vector class Declaration: class C C( const C&) = default ; default copy constructor Is automatically generated if not dened in the code exception: if there are members that cannot be copied shallow copy of each member Works for members variables with built-in types, or classes that behave like built-in types (RAII-types) Does not work for classes which manage resources manually (More on this later) class Vector Vector ( int s) : new double [s], szs ~ Vector () delete [] ; double & operator []( int i) return [i]; int size () return sz ; double * ; int sz; Vector vec: sz: 5 No copy constructor dened default generated. Classes : Copying objects 4. Classes 21/45 Classes : Copying objects 4. Classes 22/45 Classes Default copy construction: shallow copy void f( Vector v); void test () Vector vec (5); f( vec ); // call by value -> copy vec: v: sz: 5 sz: 5 The parameter v is default copy constructed: the value of each member variable is copied When f() returns, the destructor of v is executed: (delete[] ;) The array pointed to by both copies is deleted. Disaster! Classes : Copying objects 4. Classes 23/45 Special cases: zero or one parameter Copy Constructor Has a const & as parameter: Bar::Bar(const Bar& b); Converting constructor A constructor with one parameter denes an implicit type conversion from the type of the parameter class KomplextTal KomplextTal ():re 0, im 0 KomplextTal ( const KomplextTal & k) :rek.re,imk.im KomplextTal ( double x):rex,im 0 double re; double im; default constructor copy constructor converting constructor Classes : Copying objects 4. Classes 24/45

Converting constructor Warning - implicit conversion class Vector Vector ( int s); // create Vector with size s... int size () const ; // return size of Vector... void example_vector () Vector v = 7; std :: cout << "v.size (): " << v.size () << std :: endl ; v. size (): 7 In std::vector the corresponding constructor is declared explicit vector ( size_type count ); Converting constructor and explicit explicit species that a constructor does not allow implicit type conversion. struct A A( int ); A a1 (2); A a2 = 1; A a3 = (A)1; // OK // OK // OK a3 = 17; // OK [1] struct B explicit B( int ); B b1 (2); // OK B b2 = 1; // Error! [2] B b3 = (B)1; // OK: explicit cast b3 = 17; // Error! [3] [1]: construct an A(17), and then copy [2]: conversion from int to non - scalar type B requested [3]: no match for operator= ( operand types are B and int ) Classes : Copying objects 4. Classes 25/45 Classes : Copying objects 4. Classes 26/45 Copying objects Dierence between construction and assignment Copying objects the copy assignment operator: operator= void function ( Bar ); // by - value parameter Bar b1 (10, false Bar b2b1 // the copy constructor is called Bar b3(b2 ); // the copy constructor is called Bar b4 = b2; // the copy constructor is called function (b2 ); // the copy constructor is called b4 = b3; // the copy constructor is not called copy assignment not construction The copy assignment operator is implicitly dened with the type T& T::operator=(const T&) if no operator= is declared for the type if all member variables can be copied i.e., dene a copy-assignment operator If all members are of built-in (and RAII) types the default variant works (same problems as with copy ctor). vec: sz: 5 v: sz: 5 More on copy control when we discuss resource management Classes : Copying objects 4. Classes 27/45 Classes : Copying objects 4. Classes 28/45 Initialization and assignment Preventing copying It is (often) possible to write like in Java, but it is less ecient the members must be assignable Java-style: assignment in constructor Foo ( const Bar & v) value = v; NB! assignment, not initialization Bar value ; is default constructed before the body of the constructor Declaration: class C C( const C&) = delete ; C& operator =( const C&) = delete ; A class without copy constructor and copy assignment operator cannot be copied. C++-98: declare private and don't dene An object is initialized before the body of the constructor is run Classes : Copying objects 4. Classes 29/45 Classes : Copying objects 4. Classes 30/45

friend friend Functions or classes with access to all members in a class without being members themselves Friend declaration in the class KomplextTal class KomplextTal int re; int im; friend ostream & operator <<( ostream &, const KomplextTal &); Denition outside the class KomplextTal ostream & operator <<( ostream & o, const KomplextTal & c) return o << c.re << "+" c.im << "i"; The free function operator<<(ostream&, const KomplextTal&) can access private members in KomplextTal. Classes : friend 4. Classes 31/45 Functions or classes with full access to all members in a class without being members themselves Free functions, member functions of other classes, or entire classes can be friends. cf. package visibility in Java A friend declaration is not part of the class interface, and can be placed anywhere in the class denition. Classes : friend 4. Classes 32/45 Most operators can be overloaded, except sizeof..* ::?: E.g., these operators can be overloaded = + - * / % ^ & ~ << >> &&!!= == < > ++ -- += *=... () [] -> ->* & new delete new [] delete [] syntax: return_type operator (parameters...) for an operator e.g. == or + For classes, two possibilities: as a member function for binary operators, if the order of operands is suitable as a free function a binary operator takes one argument *this is the left operand, the function argument is the right operand if the public interface is enough, or if the function is declared friend Classes : 4. Classes 33/45 Classes : 4. Classes 34/45 as member function and as free function Example: declaration as member functions class Komplex Komplex ( float r, float i) : re(r), im(i) Komplex operator +( const Komplex & rhs ) const ; Komplex operator *( const Komplex & rhs ) const ; float re, im; Example: declaration of operator+ as friend Declaration inside the class denition of Komplex: friend Komplex operator +( const Komplex & l, const Komplex & r); Note the number of parameters Dening operator+ in two ways: As member function (one parameter) Komplex Komplex :: operator +( const Komplex & rhs)const return Komplex (re + rhs.re, im + rhs.im ); As a free function (two parameters) Komplex operator +( const Komplex & lhs, const Komplex & rhs ) return Komplex (lhs.re + rhs.re, lhs.im + rhs.im ); NB! the friend declaration is only in the class denition Classes : 4. Classes 35/45 Classes : 4. Classes 36/45

Dening operator+ in two ways: As member function Komplex Komplex :: operator +( const Komplex & rhs )const return Komplex (re + rhs.re, im + rhs.im ); the right operand cannot be changed the left operand As a free function cannot be changed Komplex operator +( const Komplex & lhs, const Komplex & rhs ) return Komplex ( lhs.re + rhs.re, lhs.im + rhs.im ); NB! the friend declaration is only in the class denition Another implementation of +, using += Class denition class Komplex const Komplex & operator +=( const Komplex & z) re += z.re; im += z.im; return * this ; Free function, does not need to be friend NB! Returns const reference to disallow e.g. (a += b) = c; (non-standard, dierent from built-in types ). Komplex operator +( Komplex a, const Komplex & b) return a+=b; NB! call by value: we want to return a copy. Classes : 4. Classes 36/45 Classes : 4. Classes 37/45 Example: inline friend operator<< Conversion operators Exempel: Counter The denition (in the class denition) # include <ostream > using std :: ostream ; class Komplex friend ostream & operator <<( ostream & o, const Komplex & v) o << v.re << + << v.im << i ; return o; inline friend denition: denes a free function in the same namespace as the class operator<< cannot be a member function (due to the order of operands it would have to be a member of std::ostream) Conversion to int struct Counter Counter (int c=0) :cntc Counter & inc () ++ cnt; return * this ; Counter inc () const return Counter ( cnt +1); int get () const return cnt ; operator int () const return cnt ; int cnt 0 Note: operator T(). no return type in declaration (must obviously be T) can be declared explicit Classes : 4. Classes 38/45 Classes : 4. Classes 39/45 static members: shared by all objects of the type (like Java) declared in the class denition dened outside class denition (if not const) can be public or private (or protected) Foo () ++ created ; ++alive ; ~Foo () -- alive ; int Foo :: created 0 int Foo :: alive 0 Classes : 4. Classes 40/45 Classes : 4. Classes 41/45

Foo () ++ created ; ++alive ; ~ Foo () -- alive ; int Foo :: created 0 int Foo :: alive 0 Foo () ++ created ; ++alive ; ~Foo () -- alive ; int Foo :: created 0 int Foo :: alive 0 Classes : 4. Classes 41/45 Classes : 4. Classes 41/45 Foo () ++ created ; ++alive ; ~ Foo () -- alive ; int Foo :: created 0 int Foo :: alive 0 Foo () ++ created ; ++alive ; ~Foo () -- alive ; int Foo :: created 0 int Foo :: alive 0 Classes : 4. Classes 41/45 Classes : 4. Classes 41/45 Class denitions Member functions and inline Class denitions Member functions and inline, example Function inlining: Replace a function call with the code in the function body inline is a hint to the compiler Only suitable for (very) small functions Implicit if the function denition is in the class denition If the function is dened outside the class denition, use the keyword inline Inline in the class denition: int getvalue () return value ; int value ; Inline outside the class denition: inline int Foo :: getvalue () return value ; Classes : inline 4. Classes 42/45 Classes : inline 4. Classes 43/45

Suggested reading Next lecture References to sections in Lippman Classes 2.6, 7.1.4, 7.1.5 7.57.5.4 (Aggregate classes) ("C structs" without constructors) 7.5.5 Destructors 13.1.3 this and const p 257258 inline 6.5.2, p 273 friend 7.2.1 static members 7.6 Copying 13.1.1 Assignment 13.1.2 14.1 14.3 References to sections in Lippman Dynamic memory and smart pointers 12.1 Dynamically allocated arrays 12.2.1 Classes, resource management 13.1, 13.2 Classes : inline 4. Classes 44/45 Classes : inline 4. Classes 45/45