Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

Similar documents
Part VIII. More classes. Philip Blakely (LSC) C++ Introduction 226 / 370

EL2310 Scientific Programming

Part X. Advanced C ++

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

Part V. Memory and pointers. Philip Blakely (LSC) C++ Introduction 145 / 370

EL2310 Scientific Programming

C++ Alexander Baltatzis

EL2310 Scientific Programming

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

CS

Part III. Advanced C ++

Fast Introduction to Object Oriented Programming and C++

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.

G52CPP C++ Programming Lecture 13

COMP6771 Advanced C++ Programming

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

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

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

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

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

An Introduction to C++

Classes. Christian Schumacher, Info1 D-MAVT 2013

Constructors for classes

Chapter 9 Technicalities: Classes, etc.

Object-Oriented Programming in C++

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

Fundamentals of Programming Session 24

C++ (Non for C Programmer) (BT307) 40 Hours

Intermediate Programming, Spring 2017*

Recharge (int, int, int); //constructor declared void disply();

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

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

Object-Oriented Programming

Part XII. More programming comments. Philip Blakely (LSC) C++ Introduction 349 / 370

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

Pointers. Developed By Ms. K.M.Sanghavi

Polymorphism Part 1 1

Introduction Of Classes ( OOPS )

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

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

eingebetteter Systeme

Classes, Constructors, etc., in C++

CSE 303: Concepts and Tools for Software Development

STRUCTURING OF PROGRAM

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

Fundamentals of Programming. Lecture 19 Hamed Rasifard

CS24 Week 3 Lecture 1

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

Data Structures using OOP C++ Lecture 3

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Introduction to Programming session 24

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

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

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


Introduction to C++ with content from

Object-Oriented Programming

Chapter 9 Technicalities: Classes, etc.

Object Oriented Programming COP3330 / CGS5409

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

SFU CMPT Topic: Classes

EL2310 Scientific Programming

Ch. 12: Operator Overloading

Chapter 9 Technicalities: Classes

Programming 2. Object Oriented Programming. Daniel POP

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

VIRTUAL FUNCTIONS Chapter 10

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Part XI. Algorithms and Templates. Philip Blakely (LSC) C++ Introduction 314 / 370

CS105 C++ Lecture 7. More on Classes, Inheritance

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

Defensive Programming

Instantiation of Template class

AN OVERVIEW OF C++ 1

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Flashback Technicalities: Classes, etc. Lecture 11 Hartmut Kaiser

CLASSES AND OBJECTS IN JAVA

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

Short Notes of CS201

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

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

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

CS93SI Handout 04 Spring 2006 Apr Review Answers

CS250 Final Review Questions

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

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

CS201 - Introduction to Programming Glossary By

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

A brief introduction to C++

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

2 ADT Programming User-defined abstract data types

CS304 Object Oriented Programming Final Term

Object Oriented Software Design II

Object-Oriented Programming for Scientific Computing

Chapter 9 Technicalities: Classes, etc.

Pointers and References

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

COMP6771 Advanced C++ Programming

PHY4321 Summary Notes

Transcription:

Part VII Object-Oriented Programming Philip Blakely (LSC) C++ Introduction 194 / 370

OOP Outline 24 Object-Oriented Programming 25 Member functions 26 Constructors 27 Destructors 28 More constructors Philip Blakely (LSC) C++ Introduction 195 / 370

OOP Object-Oriented Programming Object-Oriented Programming is a vital part of most complex programs these days Although not absolutely necessary for scientific programming, it will make for far more readable code and make code easier to read and debug The essential concepts in OOP are encapsulation and data-hiding. There are further concepts such as inheritance and polymorphism This is not a course in OOP, so only a brief introduction to the concepts will be given Much of the skill in using OOP is knowing how to partition concepts into objects/classes; this will not be covered here. Philip Blakely (LSC) C++ Introduction 196 / 370

OOP Classes Simple objects were available in C as a struct. (short for structure) A struct is a self-contained package of other types. The C ++ generalized form of these is a class. Provides names for its member-types Allows for easy copying/assignment of these objects For example, a Date class requires three members: class Date{ public: int day; int month; int year; ; Within C ++ a class is a new type, with all the type-safety features that implies. Philip Blakely (LSC) C++ Introduction 197 / 370

OOP Using classes void printdate(const Date& d){ std::cout << d.day << "/" << d.month << "/" << d.year << std::endl; int main(void){ Date theend; theend.day = 21; theend.month = 12; theend.year = 2012; printdate(theend); Copying and assignment of classes is implemented automatically for simple data. Anything else, e.g. printing, checking for equality, arithmetic operations, etc. must be explicitly coded by the programmer. A specific variable of type Date is referred to as an instance of the Date class. Philip Blakely (LSC) C++ Introduction 198 / 370

Member functions Outline 24 Object-Oriented Programming 25 Member functions 26 Constructors 27 Destructors 28 More constructors Philip Blakely (LSC) C++ Introduction 199 / 370

Member functions Member functions We may want to be able to advance a Date by a single day: Approach 1: Date advance(const Date& d){ Date newd = d; newd.day += 1; if( newd.day > numdaysinmonth[newd.month] ){ newd.day = 1; newd.month += 1; if( newd.month > 12 ){ newd.month = 1; newd.year += 1; return newd; Date tomorrow = advance(today); However, this approach will result in separate functions not obviously related to a Date. Philip Blakely (LSC) C++ Introduction 200 / 370

Member functions Member functions It might be better if we could do something like: mydate.advance(); which would advance mydate by one day. We can do this as follows: class Date{ public: void advance(); ; void Date::advance(){ day += 1; where the advance function knows about the current object that it is being called on. The advance function is a member of the Date class. The day referred to is specific to this object. Calling advance() on one Date object will not affect any other Date object Philip Blakely (LSC) C++ Introduction 201 / 370

Member functions Scope-resolution operator The :: is called the scope-resolution operator and has the highest precedence of all operators This identifies the class in which a function or variable lies For example, when used as int Date::advance(){ it is stating that, even though this function declaration occurs outside a class definition, the advance function belongs to the class Date This is the same use as in std::cout except that cout is being resolved as being the one in namespace std. Philip Blakely (LSC) C++ Introduction 202 / 370

Member functions Access control As we ve written it so far, data members of a Date instance are public. i.e. any external function can access the members.... d.day = 35; d.month = 13;... providing invalid data that will cause problems later. It would be useful if all access to the object s members had to go through a Date member function. Philip Blakely (LSC) C++ Introduction 203 / 370

Member functions Private We can do this by making some of the members private: class Date{ public: void advance(); private: int day; int month; int year ; int main(void){ Date d; d.day = 1; // Compile time error day is private d.advance(); // Allowed since advance() is public In fact, class members are private by default, hence the public in previous slides. Members of a struct are public by default. Philip Blakely (LSC) C++ Introduction 204 / 370

Member functions Access functions However, we now cannot get any data into the Date object in the first place. We can add simple access functions: class Date{ public: int getday()const; void setday(int); ; int Date::getDay()const{ return day; void Date::setDay(int d){ day = d; The advantage is that all access to day goes through one function, and can be checked for errors at this point, without having to remember to introduce checks elsewhere. Philip Blakely (LSC) C++ Introduction 205 / 370

Member functions Const-ness You may have noticed the const on the getday function above. This indicates that the function does not change any member data of the Date object (except for any static members) Any attempt to so within the function is a compile-time error: int Date::getDay()const{ month = 1; // Compile error return day; Philip Blakely (LSC) C++ Introduction 206 / 370

Member functions Const-ness ctd Also, if a Date object has been declared to be constant, then you cannot call non-const functions on it: const Date mybirthday = d; // Where d holds a pre defined Date // The following is OK: std::cout << mybirthday.getday() << "/" << mybirthday.getmonth() << std::endl; // Compile error: setday does not keep a Date object constant mybirthday.setday(12); Philip Blakely (LSC) C++ Introduction 207 / 370

Constructors Outline 24 Object-Oriented Programming 25 Member functions 26 Constructors 27 Destructors 28 More constructors Philip Blakely (LSC) C++ Introduction 208 / 370

Constructors Constructors Having to write: Date d; d.day = 25; d.month = 12; d.year = 2012; is laborious. It would be easier if we could write: Date d(25, 12, 2012); The function that does this is called a constructor. Philip Blakely (LSC) C++ Introduction 209 / 370

Constructors Constructors To define a constructor: class Date{ public: Date(int, int, int);... ; Date::Date(int d, int m, int y){ day = d; month = m; year = y; A constructor is a function with the same name as the object it refers to, and with no return type (not even void). A class may have multiple constructors taking different parameters, or even default parameters. Philip Blakely (LSC) C++ Introduction 210 / 370

Constructors Constructor initializer list You can also initialize data members outside the constructor function This uses an initializer list Date::Date(int d, int m, int y) : day(d), month(m), year(y){ This is the only way to initialize const members of a class, as they cannot be modified once a class instance has been constructed. Philip Blakely (LSC) C++ Introduction 211 / 370

Destructors Outline 24 Object-Oriented Programming 25 Member functions 26 Constructors 27 Destructors 28 More constructors Philip Blakely (LSC) C++ Introduction 212 / 370

Destructors Destructors If you allocate memory in your constructor, using new, you should also delete it when the object is destroyed. This is done in the destructor: class MyArray{ public: MyArray(int); MyArray(); private: double data; ; MyArray::MyArray(int n){ data = new double[n]; MyArray:: MyArray(){ delete[] data; Philip Blakely (LSC) C++ Introduction 213 / 370

Destructors Destruction The data of arr is deleted when arr goes out of scope, i.e. at the end of the function or block in which it is defined. int f(){ MyArray a(5); for(int i=0 ; i < 10 ; i++){ MyArray b(10); // b. MyArray is called at the end of every iteration // a. MyArray is called directly after this line The destructor takes no parameters. (The choice for destructor syntax comes from bitwise NOT.) Philip Blakely (LSC) C++ Introduction 214 / 370

Destructors Heap construction/destruction of classes To allocate space on the heap for objects of a specific class: MyArray a = new MyArray(10); which allocates a single instance of MyArray and calls its constructor with an argument 10. This object then persists until the destructor is explicitly called using delete a; To allocate an array of these, use: MyArray a = new MyArray[10]; delete[] a; This uses the default constructor to initialize the instances. A default constructor must be available; if not it is a compile-time error. Philip Blakely (LSC) C++ Introduction 215 / 370

Destructors Class pointer function call In order to call a member function of a class, given a pointer to an instance of that class, use: Car mycar = new Car; mycar >setnumberpassengers(10); Philip Blakely (LSC) C++ Introduction 216 / 370

Destructors Explicit destruct It is possible to explicitly call a destructor for an object: Car a = new Car; a > Car(); // Call destructor delete a; // Destructor called again likely an error You virtually never need to call a destructor explicitly. It is not possible to explicitly call a constructor. Philip Blakely (LSC) C++ Introduction 217 / 370

Destructors Return by reference Suppose we have a large object stored within a class and need access to it from outside: BigObject MyClass::data(){ return mydata; This may suggest bad design already; direct access to a class s internal data is usually not sensible. However, it is permissible to return a reference to an object from a function to avoid the expense of copying: BigObject& MyClass::data(){ return mydata; Philip Blakely (LSC) C++ Introduction 218 / 370

Destructors Return by reference ctd The lack of copy is only preserved until the object is allocated to another variable: MyClass myobj; BigObject a = myobj.data(); // Invokes a copy myobj.data().bigobjfunc(); // Does not invoke a copy. Note that we have now exposed the contents of mydata to outside its class - possibly bad! Philip Blakely (LSC) C++ Introduction 219 / 370

Destructors Return by reference ctd In order to prevent alteration, we should of course return a constant reference: const BigObject& MyClass::data(){ return mydata; or even const BigObject& MyClass::data()const{ return mydata; which would prevent the call to bigobjfunc() above if it were not a const member function. Philip Blakely (LSC) C++ Introduction 220 / 370

More constructors Outline 24 Object-Oriented Programming 25 Member functions 26 Constructors 27 Destructors 28 More constructors Philip Blakely (LSC) C++ Introduction 221 / 370

More constructors Copy-constructor The copy-constructor is used in the case: MyClass a; MyClass b = a; i.e. b is constructed by copying a. (It is also used when passing an object of type MyClass to a function.) It is declared as: class MyClass{ MyClass(const MyClass& c){ // Initialize all members as necessary from c myint = c.myint; ; Note that a class has access to all private members of any object of the same type. Philip Blakely (LSC) C++ Introduction 222 / 370

More constructors Default copy constructor If a copy-constructor is not defined, then a default version is created that copies the object member-by-member, i.e. all members are copied using their own copy constructors. This is often the required behaviour, unless the object contains heap-allocated pointers that would be freed on destruction of an object. In this case, the copy-constructor needs to allocate more memory and copy the data pointed to. Philip Blakely (LSC) C++ Introduction 223 / 370

More constructors Copy-assignment A slightly different version of the above occurs when objects are copied by assignment: MyClass a(...); MyClass b(...); b = a; Here, the appropriate member function definition is given as: class MyClass{ MyClass& operator=(const MyClass& c){ // Copy data from c as appropriate return this; ; Once again, the default is to copy-assign each member by itself, but this may need to be altered in the case of heap-allocation. The this pointer is a pointer to the current object. It is not usually necessary to use it. Philip Blakely (LSC) C++ Introduction 224 / 370

More constructors Class summary Member functions are typically actions - do something to this object read/set functions - get/set information in this object Member data should usually be private, to avoid unregulated access Member functions are typically public - for access from other objects/functions although some may be private - for use internally If you just want a collection of data, then use a struct A struct is identical to a class with all its members public by default Philip Blakely (LSC) C++ Introduction 225 / 370