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

Similar documents
Chapter 9 Technicalities: Classes, etc.

Flashback Technicalities: Classes, etc. Lecture 11 Hartmut Kaiser

Chapter 9 Technicalities: Classes, etc. Walter C. Daugherity Lawrence Pete Petersen Bjarne Stroustrup Fall 2007

Chapter 9 Technicalities: Classes, etc.

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

Chapter 9 Technicalities: Classes

typedef int Array[10]; String name; Array ages;

GEA 2017, Week 4. February 21, 2017

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Inheritance, and Polymorphism.

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

Operator overloading: extra examples

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

Chapter 9 Technicalities: Classes, etc.

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

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

COMP6771 Advanced C++ Programming

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

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

Default Values for Functions Enumeration constant Member Functions

Object Oriented Software Design II

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

Constants, References

AIMS Embedded Systems Programming MT 2017

Object-Oriented Programming, Iouliia Skliarova

CAP Fundamentos de Programação Estruturada Aula 5. Lubia Vinhas

Introducing C++ to Java Programmers

CS93SI Handout 04 Spring 2006 Apr Review Answers

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

CS349/SE382 A1 C Programming Tutorial

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

G52CPP C++ Programming Lecture 13

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

Chapter 17 vector and Free Store

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?

C How to Program, 6/e, 7/e

PIC 10A Objects/Classes

CS32 Summer Intro to Object-Oriented Programming in C++ Victor Amelkin August 12, 2013

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

CE221 Programming in C++ Part 1 Introduction

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

Structures and Pointers

Vector and Free Store (Pointers and Memory Allocation)

6. Pointers, Structs, and Arrays. 1. Juli 2011

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

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

lecture04: Constructors and Destructors

Introduction Of Classes ( OOPS )

Chapter 13: Copy Control. Overview. Overview. Overview

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

Functions and Recursion

Functions, Pointers, and the Basics of C++ Classes

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

Class Destructors constant member functions

Vector and Free Store (Vectors and Arrays)

Introduction to Classes

Abstraction in Software Development

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:

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

Ch. 12: Operator Overloading

COMP6771 Advanced C++ Programming

Pointers. Developed By Ms. K.M.Sanghavi

Fast Introduction to Object Oriented Programming and C++

Structured Data. CIS 15 : Spring 2007

Written by John Bell for CS 342, Spring 2018

Constructors for classes

QUIZ Friends class Y;

CS 247: Software Engineering Principles. ADT Design

Intermediate Programming, Spring 2017*

Operator overloading

Chapter 11: Structured Data

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

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Midterm Review. PIC 10B Spring 2018

Chapter 17 vector and Free Store

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

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

A brief introduction to C++

Scope. Scope is such an important thing that we ll review what we know about scope now:

Chapter 11: Structured Data

Casting and polymorphism Enumeration

Objectives. Describe ways to create constants const readonly enum

Fundamental of Programming (C)

Object-Oriented Programming for Scientific Computing

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

Solved Exercises from exams: Midterm(1)

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

Programming Assignment #2

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

The vector Class and Memory Management Chapter 17. Bjarne Stroustrup Lawrence "Pete" Petersen Walter Daugherity Fall 2007

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Advanced Systems Programming

2 ADT Programming User-defined abstract data types

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors


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

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Transcription:

Chapter 3 User-defined types 3.1 Classes A class is a user-defined type. It is composed of built-in types, other user-defined types and functions. The parts used to define the class are called members. Members are either data members, which define the representation of an object of the class, or function members (sometimes called methods), which provide operations on such objects. Members can be accessed using the object.member notation. 3.1.1 Methods Methods define provide the operations on objects of a given clas. Some methods have a special meaning: 1. Constructor: a method with the same of the class. It is used to initialize (construct) of objects of the class. Constructors can have arguments and you can have more than one constructor for the class. 2. Destructor: a method with the same name of the class preceded by the sign ~. 3. Operators: used to provide a conventional notation, similar to the operators defined in built-in types such as +, -, *, /, %, [], (), &, <, <=, > and >=. This is called operator overloading. It is important to think of class as having an interface plus an implementation. The interface is the part of the class s declaration that its users access directly. The public part of class is the user s view of the class. The implementation details is the implementer s view 25

26 of the class and usually it is defined in a di erent file (remember separate compilation in Section 2.3.4). Program 14 shows the interface and program 15 shows the implementation file of class X. Program 14 Class interface class X 2 public : X( int m) ; // constructor 4 X( ) // destructor 6 int getm () ; // function member void addtom( int v); 8 10 X& operator++() ; private : //operator 12 int m; // data member ; 14 X obj(0) ; // var is variable of type X 16 int i = obj.getm() ; // retrieve var s data member m obj.addtom(10) ; // call var s member function addtom () 18 X++; // call vars operator ++ Program 15 Class implementation 1 X::X(int vi) : m( v i ) 3 5 X : : X( ) 7 int X::getM() 9 return m; 11 13 void X::addToM(int v) 15 m= m+v ; 17 19 X& X : : operator++() ++this >m; 21 return this ; The #define guard prevent multiple inclusion. The format of the symbol name should be

27 chosen so that it guarantee uniqueness, for example base on the file path in a project s source tree. For example, the file foo/src/bar/baz.h in project foo would have the following guard: #ifndef FOO BAR BAZ H 2 #define FOO BAR BAZ H 4... 6 #endif // FOO BAR BAZ H 3.2 Structures For a class that has only data, the distinction between interface and implementation doesn t make much sense, and there is a simplified version of it, a structure. An structure is a user defined data type which allows you to combine data items of di erent kinds. Structures are classes where all member data and functions are public. Program 16 shows how to define and use an structure. 3.3 Enumerations An enum is a very simple user-defined type, specifying its set os values as symbolic constants (see Program 17). 3.4 Reference and pointers You can pass a structure or a class as a function argument in very similar way as you pass any other variable or pointer. You can define pointers to structures or classes in very similar way as you define pointer to any other variable (see Program 18). Remember that to access members through pointer you use the notation object->member. 3.5 Classes and free store allocation Constructors and Destructors are conceptually simple but the foundation of most e ective C++ programming. whatever resource a class object needs to function it acquires in a constructor;

28 Program 16 Structures 1 struct Date 3 int day ; int month ; 5 int year ; 7 Date( int d, int m, int y); void addday( int ndays) ; 9 ; 11 int main( ) 13 struct Date d1 ; struct Date d2 ; 15 d1. year = 2016; 17 d1. month = 03; d1. day = 16; 19 d2. year = 2016; 21 d2. month = 03; d2. day = 15; 23 cout << Date 1 day : << d1. day <<endl ; 25 cout << Date 1 month : << d1. month <<endl ; cout << Date 1 year : << d1. year <<endl ; 27 cout << Date 2 day : << d2. day <<endl ; 29 cout << Date 2 month : << d2. month <<endl ; cout << Date 2 year : << d2. year <<endl ; 31 Date today (16,03.2016) ; 33 today.addday(1) ; 35 return 0; Program 17 Enumerations 1 enum class Day monday, tuesday, wednesday, thursday, friday, saturday, sunday ; enum class Month jan=1, feb=2, mar=3, apr=4, may=5; jun=6, jul=7, aug=8, set =9, out=10, nov=11, dec=12; 3 Month m = Month : : mar ; during the object s lifetime it may release resources and acquire new ones; at the end of the object s lifetime, the destructor releases all resources still owned by the object.

29 2 4 Program 18 User defined types references and pointer void print(const Date& dt ) ; Date pdt ; pdt >day = 18; 6 pdt >month = 1 pdt >ano=2015; Program 19 shows an example using free store memory allocation. If you acquire memory in the constructor you have to delete in the destructor, otherwise you run into memory leak. C++ doesn t provide automatic garbage collection. A program that runs forever can t a ord memory leaks, others will eventually terminate and all the memory used will be returned to the systems. Still, memory leaks are considered a sign of sloppiness and may cause performance problems or even faults if your memory consumption estimate is incorrect. If you do not provide a destructor or a constructor, compiler will provide for you. But it will not acquire or release free store memory. 3.5.1 Copying The default meaning of copying objects is to all data members from the object being copied to the copy. Consider the class container shown in Program 19 what happens if we need a copy of an object of this class? We will write container c2=c1;. In this case, c2.m elements will have a copy of c1.m elements, i.e. a copy of the pointer. Two objects sharing a pointer is a source of trouble, for example, whenever c1 goes out of scope and is destroyed, c2 pointer will be pointing to an invalid memory or vice-versa. To solve this problem there are two special methods to control coying: copy constructors, called when a new object is initialized from an existing one and copy assignments to handle copies. The example can be seen in Program 20. 3.6 Const correctness C++ allows you to explicitly the keyword const to prevent constant objects from getting mutated. For example, if you wanted to create a function f()that accepted a std::string, plus you want to promise callers not to change the callers std::string that gets passed to f(), you can have f() receive its std::string parameter. In the pass by reference-to-const and pass by

30 Program 19 Classes resource management class container 2 public : 4 container(int size): m size( size ), 6 m elements(new int [size]) 8 std :: cout << acquiring memory\n ; for ( int i=0; i<size ; ++i) m elements [ i ]=0; 10 12 container() 14 std :: cout << releasing memory\n ; delete [] m elements ; 16 18 void insert(int elem, int idx) m elements [ idx ] = elem ; 20 int get( int idx) 22 return m elements [ idx ]; 24 private : int m size ; 26 int m elements ; ; 28 30 void f() container c(3) ; 32 c.insert(10,0); c.insert(5,1); 34 c.insert(1,2); std :: cout <<c.get(0)<<, << c.get(1)<<, << c.get(2)<< \n ; 36 38 int main () 40 f(); return 0; 42 pointer-to-const cases, any attempts to change the caller s std::string within the f() functions would be flagged by the compiler as an error at compile-time. In the pass by value case (f3()), the called function gets a copy of the caller std::string (see Program 21). Const-ness can also be applied to class methods (see Program 22). Declaring the const-ness of a parameter is a form of type safety. The benefit of const correctness is that it prevents you

31 Program 20 Copying 1 class container 3... 5 container(container& other) : m size(other. m size), 7 m elements(new int [other.m size ]) 9 std :: cout << acquiring memory copy constructor\n ; for ( unsigned int i=0; i<m size ; ++i ) 11 m elements [ i]=other. m elements [ i ]; 13 container& operator=( container& other ) 15 if ( this == &o t h e r ) 17 return this ; 19 std :: cout << releasing memory assign operator\n ; delete []m elements ; 21 std :: cout << acquiring memory assign operator\n ; 23 m elements = new int [other.m size ]; for ( unsigned int i=0; i<m size ; ++i ) 25 m elements [ i]=other. m elements [ i ]; m size = other. m size ; 27 return this ; 29 ; from inadvertently modifying something you didn t expect would be modified. 3.7 Class interface Considering the description of classes and structures, the possibility of separate compilation as well as the public and private options for members and methods, we have to think on how do we design a good interface. Specially the public interface. Of course, it depends on the software design and the problem being solved, but there are a few general principles that we should try to follow: keep interfaces complete; keep interfaces minimal; provide constructors;

32 Program 21 Const-ness void f1(std :: string s) 2 s = ok ; 4 void f2(std :: string& sr) sr = ok ; 6 void f3(const std :: string& crs ) 8 crs = ooops! ; // error 10 void f4(std :: string ps) ps = ok ; 12 void f5(const std :: string cps) 14 std :: string pps=new std :: string ; 16 cps = pps ; cps= ooops ; // error 18 20 int main () 22 std :: string s ; f1(s) ; 24 f2(s) ; f4(&s) ; 26 const std :: string cs= const ; 28 f1(cs) ; f2(cs) ; 30 f4(&cs) ; f5(&cs) ; 32 return 0; Program 22 Const-ness // mmethod does not change paramenter param1 or the internal 2 // status of the object void ClassX : : mmethod( const string& param1) const ; use types to provide good argument checking; identify non-modifying member functions; provide destructors ; free all resources in the destructor; if you have dynamic allocated members, pay extra attention to constructor, destructor and

33 verify the need for assign operator and copy constructor. 3.7.1 Classes x structures Another good question is, how to decide when use a class or a struct? A very simple rule of thumb is that you should have a real class with an interface and a hidden representation if and only if you can consider an invariant for the class. An invariant allows you to say when the object s representation is good and when it isn t. For example: you want to design a type to represent a person by its name and address. If it doens t matter what value you can have in name or address, if you can not think of a invalid person, than this is a structure. However, if you decide the semantics should be that first, middle, and last name as parts of the name. You can also decide that the address really has to be a valid address. Either you validate the string, or you break the string up into first address field, second address field, city, state, country, zip code, that kind of stu, than it should be a class. 3.7.2 Class methods x functions There is a general rule that helps us decide to choose between methods and external functions to manipulate a class. If the function changes the state of an object, then it ought to be a member of that object. Unfortunately, even this rule says nothing about functions that do not change the state of the object, so we still must decide what to do considering what the function does and also how users might want to call it. Program 23 shows an example of having the compare operator as function instead of overloading the operator ==. Program 23 Functions class Student 2 public : 4 std :: string name() const ; bool valid () const ; 6 std :: istream& read(std :: istream&); double grade () const ; 8 private : std :: string n; 10 double midterm, f i n a l ; std :: vector<double> homework ; 12 ; bool compare( const Student&, const Student&);