Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Road Map Introduction to object oriented programming. Classes
Chapter 14: Overloading and Templates
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Constructors and Other Tools Version 1.0 Topics Constructors & Destructors Composition const Parameter Modifier const objects const functions In-line.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Module 2: User Data types #1 2000/01Scientific Computing in OOCourse code 3C59 Module 2: User defined Data Types & Operations upon them In this module.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Module 10: Simple Inheritance #1 2000/01Scientific Computing in OOCourse code 3C59 Module 10: Simple Inheritance In this module we will cover Inheritance.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Defining New Types Lecture 21 Hartmut Kaiser
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Object-Oriented Programming in C++
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
Module 8: more on methods #1 2000/01Scientific Computing in OOCourse code 3C59 Module 8: More on methods: In this module we will cover: Overloading of.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Session 7 Methods Strings Constructors this Inheritance.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Module : Technical topics #1 2000/01Scientific Computing in OOCourse code 3C59 Module : Technical topics In this module we have grouped together several.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
OOP Basics Classes & Methods (c) IDMS/SQL News
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Learners Support Publications Constructors and Destructors.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Module 11: Polymorhism #1 2000/01Scientific Computing in OOCourse code 3C59 Module 11: Polymorphism and virtual methods In this module we will cover Polymorphism.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Constructors and Destructors
User-Written Functions
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Constructor & Destructor
This pointer, Dynamic memory allocation, Constructors and Destructor
Constructors and destructors
Constructors and Destructors
Java-An introduction In this module we will cover The class in Java
CS148 Introduction to Programming II
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors Default constructor Other constructors Copy constructor Quirks about constructors Destructor

Module 7: Constructors #2 2000/2001Scientific Computing in OOCourse code 3C59 Aims of this module We have previously introduced the concept of a “class” as a construct which combines member variables and methods in a formal way. In this module you will learn about a new and vitally important part of a class which determines how an object gets made. This all revolves around the idea of "Constructors" which are special methods which get called automatically to initialise objects. Associated with these are "Destructors" which get called when an object goes out of scope or is deleted. These are fundamental new concepts in OO and you must use these from early on. Therefore this module covers their use in some detail.

Module 7: Constructors #3 2000/2001Scientific Computing in OOCourse code 3C Constructors Throughout the course so far we have seen the idea that as soon as you make an Object then you should remember to initialise is so that its member variables are set to something sensible. I.e // Make a ThreeVector and initialise the px,py,pz ThreeVector vec ; vec.initialise( 0.8, 0.4, 1.3 ) If you DO NOT do something like this then you may find that your internal variables are full of some random garbage like , or whatever happened to be left in the memory from a previous use.

Module 7: Constructors #4 2000/2001Scientific Computing in OOCourse code 3C59 We can say the same thing in a more abstract way: When you make an object it is unlikely to be of any use unless you give it "state" which distinguishes it from other objects: Employee class "Joe bloggs", age=20, accoutnumer=12345 "Ann Banks", age=25, accoutnumer= "Art Daumann ", age=50,accoutnumer=896543

Module 7: Constructors #5 2000/2001Scientific Computing in OOCourse code 3C59 So far we have always done this by invoking this special “initialise” method explicitly after making the object. C++ formalises this by defining a “special method” which is invoked automatically for you when you make the object. This is called a Constructor To start we will see a so called: Default Constructor This doesnt actually do anything useful in the way of giving state, but shows the syntax. Then we look at more useful ones which take arguments

Module 7: Constructors #6 2000/2001Scientific Computing in OOCourse code 3C59 The Default Constructor (no arguments) class ThreeVector { private:.... public: //Normal methods float magnitude( ) ;.... // Constructor ThreeVector( ) ; }; To provide a default constructor you declare it (in the.h file) as a you would any normal method. In this case the method name is the same as the class name itself The default constructor has no arguments. There is NEVER a return type

Module 7: Constructors #7 2000/2001Scientific Computing in OOCourse code 3C // Magnitude method float ThreeVector::magnitude( ) {.... } // Default constructor ThreeVector::ThreeVector( ) { px = 0 ; py = 0 ; pz = 0 ; } You write the code for the method as you would for a normal method, except that there is no return type. This is of course normally sitting in a.cpp file

Module 7: Constructors #8 2000/2001Scientific Computing in OOCourse code 3C59 // make a ThreeVector ThreeVector vec ;....rest of code to use vec... Now, whenever you make a ThreeVector, this default constructor will automatically be called for you vec.ThreeVector( ) ; You might want to think of it as some sort of hidden call like this inserted immediately after making vec

Module 7: Constructors #9 2000/2001Scientific Computing in OOCourse code 3C59 class ThreeVector { private:.... public: //Normal methods float magnitude( ) ;.... // Constructors ThreeVector( ) ; ThreeVector( float x, float y, float z ) ; }; Here is an extra constructor which takes three floating point arguments. We would use this to initialise to a specific (x,y,z) rather than just (0,0,0). Other Constructors (ones which take some usefel arguments)

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C // Default constructor ThreeVector::ThreeVector( ) { px = 0 ; py = 0 ; pz = 0 ; } // Extra constructor ThreeVector::ThreeVector( float x, float y, float z ) { px = x ; py = y ; pz = z ; } Here is the extra code in the.cpp file for the constructor taking three floats

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 // make a ThreeVector ThreeVector vec( 0.8, 0.4, 1.2 ) ;....rest of code to use vec... Suppose you want to make a ThreeVector and initialise it to ( 0.8, 0.4, 1.2 ). To cause the appropriate constructor to be invoked when you make the ThreeVector you simply do this: Note the format: you are not supplying arguments to a method called vec  vec is the variable name !  you are putting arguments after the variable name at the point it is created

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 // make a ThreeVector ThreeVector vec( 0.8, 0.4, 1.2 ) ;....rest of code to use vec... This is the ONLY way you can make a specific constructor get invoked: You can NEVER call it yourself later like this: // make a ThreeVector ThreeVector vec ;... some code... vec.ThreeVector( 0.8, 0.4, 1.2 ) ; This is not allowed. A thing gets constructed ONLY when it is made, not later on

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 Student exercise Write an Animal class which has member variables to represent: -The food type (carnivore, omnivore, herbivore) - the number of legs Supply the class with (at least) two constructors: 1. One which takes the only the food type This should assume the animal has 4 legs 2. Another which takes the food type, but also the number of legs in case it is not 4. You might like to make this check that the value supplied is even and print an error message if not. Write some code to demonstrate the use of these constructors. Hint: I would provide a dump() method for the Animal class which can be used to print out the member variable information after you have constructed a few animals

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C The copy constructor There is a special constructor which is very often provided for a class. It is called the "copy constructor" Its purpose is to constuct a new object by making an exact copy of an existing object. This example shows how it is invoked: // Example of using the copy // constructor // make a Thing Thing t1 ; //...set t1 to something.... // make a second Thing which is // a copy of t1 by invoking the // copy constructor Thing t2( t1 ) ;

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 This is how you (could) declare a copy constructor class ThreeVector { private:.... public: //Normal methods float magnitude( ) ;.... // Default Constructor ThreeVector( ) ; // Copy constructor ThreeVector( ThreeVector source) ; }; The copy constructor takes another ThreeVector as its argument

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 // This is in the.cpp file // Copy constructor ThreeVector::ThreeVector( ThreeVector src ) { px = src.px ; py = src.py ; pz = src.pz ; } The member variables of the new object are set to equal the member variables of the source object This is how you write the code for the copy constructor Note how they are accessed from the source, using src.px

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 If you find this difficult, you might like to realise that you have already done something very like this ! You did it when writing the dotProduct( ) method for the ThreeVector class. This method took another ThreeVector as an argument Look at this and see the similarity if you need to.

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 Now you need to know about references ! go to special module on technical topics to find out what a reference is.

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 // This is the.cpp file // Copy constructor ThreeVector::ThreeVector( const ThreeVector& src ) { px = src.px ; py = src.py ; pz = src.pz ; } In practice copy constructors are always written using a const reference to the source. This is because there is no need to waste the time and memory making a temporary copy. Thus a copy constructor is always declared thus: And written thus: Note that the code itself is unchanged // This is the.h file class ThreeVector { private:.... public: //Normal methods... // Copy constructor ThreeVector( const ThreeVector& source); };

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 7.3: Quirks about Constructors IMPLICIT & EXPLICIT DEFAULT CONSTRUCTORS If you ever want to make an object in the simplest way, i.e like this then you have to realise that the compiler will only let you do this if there exists a default constructor: Thing( ) i.e a constructor with no arguments which the system can call. Thus you have to be aware of when a default constructor exists or not ! // make a Thing Thing t ;

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 1. If you NEVER explicitly declare or write ANY constructors i.e your.h file looks like this then the compiler in effect provides an implicit default constructor free. You never see it, but it is as if Thing( ) ; exists. class Thing { // Normal variables float xxx ; // Normal methods void aService(..) ; // But no constructors }; // Then this always works Thing t ;

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 2. If you DO explicitly declare or write ANY constructor at all i.e your.h file looks like this then the compiler NO LONGER provides an implicit default constructor. It asssumes you know what you are doing, and have taken charge of constructors. class Thing { // Normal variables float xxx ; // Normal methods void aService(..) ; // A constructor Thing( float initial ) ; }; // Then this WILL NOT work Thing t ;

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 3. If you DO explicitly declare or write constructors, and you INCLUDE your own default constructor i.e your.h file looks like this.... class Thing { // Normal variables float xxx ; // Normal methods void aService(..) ; // My default constructor Thing( ) ; // Possible other constructors Thing(.... ) ; }; // Then this will work Thing t ;

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 IMPLICIT "COPY" CONSTRUCTOR The compiler ALSO provides an implicit COPY constructor free. What this means is that you can always do this.. t2 is a direct copy of t1 ! In effect the compiler has provided a constructor like this: Thing( const Thing& source ) // Create t1 Thing t1 ; // Create t2 as a COPY of t1 Thing t2( t1 ) ; However is only does a bit by bit copy of the object. This may not be what you want ! Always therefore consider whether you need to write your own explicit copy constructor

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 INITIALISATION of MEMBER VARIABLES You might already have noticed that you CANNOT do this.... the compiler throws a wobbly. In order to initialise member varibles you must do it in the constructor like this.... class Thing { float x = 0. ; int y = 1 ; } Thing::Thing( ) { x = 0. ; y = 1 ; }

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 INITIALISATION of CONST MEMBERs Similarly you CANNOT do this.. Unfortunately.. NOR can you do this when the members are const.... class Thing { const float x = 0. ; const int y = 1 ; } Thing::Thing( ) { x = 0. ; y = 1 ; }

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 In order to initialise const members you must do it in the constructor like this or like this... Thing::Thing( ) : x(0.), y(1) { rest of constructor.. } Thing::Thing( float xinit, int yinit ) : x( xinit ), y( yinit ) { rest of constructor.. } This is NEW !!!! -It is called a member initialisation list -For now just be aware that they are needed for some operations - I WILL NOT require you to know this for the course - its too obtuse

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 7.4: Destructor The constructor has an opposite. It is called the: Destructor This gets called automatically when an Object goes out of existence [I.e. at he end of a {...} in which it was created. Its function is to tidy up anytihng the object might have done before it gets destroyed.

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 To supply a destructor you add ~ClassName( ) as a new special method // The.h file for class Thing { public: // A constructor Thing( ) ; // A destructor ~Thing( ) ; }; // In the.cpp file Thing::~Thing( ) {... code in here to tidy... up Thing object } Then you write this code to implement the destructor.

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 For the most part you don’t need to supply a destructor. It is however considered to be good practice to always write a destructor, even if it is empty to start with. If you have allocated memory from within your Object, then you will probably have to write a destructor which explicitly deletes this memory.We will return to this later.

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 Student exercise to do in your own time (i.e. between now and next session) Modify your ThreeVector class to provide some sensible constructors Write - a default constructor to set everything to zero - an obvious constructor to take x,y,z as arguments - a copy constructor Write some code to demonstrate that you know how to use these constructors. We will look at it next session util/ ThreeVector.h ThreeVector.cpp

Module 7: Constructors # /2001Scientific Computing in OOCourse code 3C59 Summary of Module 7: Constructors and Destructors In this module we have covered the following topics. Constructors Constructors are called automatically when you make an object You can provide several constructors with different argument lists The correct constructor is called according to the arguments to supply when making the object Copy constructor Destructor The destructor is called when an object get destroyed. Its function is to cleanly end the life of an object