C++ Programming for Graphics

23
C++ Programming for Graphics Lecture 5 References, New, Arrays and Destructors

description

C++ Programming for Graphics. Lecture 5 References, New, Arrays and Destructors. Introduction. References New Arrays by type by pointers Destructors. Constructors - Extra. If the class does not contain a constructor The system provides a default If the class contains one or more - PowerPoint PPT Presentation

Transcript of C++ Programming for Graphics

Page 1: C++ Programming for Graphics

C++ Programming for Graphics

Lecture 5

References, New, Arrays and Destructors

Page 2: C++ Programming for Graphics

Introduction

• References

• New

• Arrays– by type– by pointers

• Destructors

Page 3: C++ Programming for Graphics

Constructors - Extra

• If the class does not contain a constructor– The system provides a default

• If the class contains one or more– You lose the default constructor.– You have to provide all required.

Page 4: C++ Programming for Graphics

References

• Referring back to the “swap” function

• Instead of passing values.

• Passed addresses of the variables when calling function.

• Manipulated values by dereferencing.

• Had to remember to use “&” and “*”

Page 5: C++ Programming for Graphics

References

• C++ has “reference type”

• Do not need to use “&” and “*” when using.

• Declared by – basic type, followed by– “&”– e.g int& “reference to an integer”

double& “reference to a double”

Page 6: C++ Programming for Graphics

Reference

• Usage• Must declare and initialise at same time• E.g.

– int nCount;– int& refInt = nCount;

• Can then access the value by– using “nCount” – or its alias– refInt

Page 7: C++ Programming for Graphics

References and classes

• Can use references for classes• E.g. Account Freddy;

Account& refAcc = Freddy;so, can access the object using either

• E.g Freddy.setBalance(13.26);refAcc.setBalance(13.26);

• Both – produce the same result.

Page 8: C++ Programming for Graphics

New

• So far – instantiated by “name”– e.g. Account Freddy;

Account Tripta(12.36, 2.5, 2);

• Can also use “new”– similar to malloc()– much easier to use.– allocates memory for the object.– returns a pointer to the memory address.

Page 9: C++ Programming for Graphics

Using “new”

• First need a pointer– e.g. Account *pAcc;

• Now instantiate– e.g. pAcc = new Account;

• or with arguments– pAcc = new Account(12.36, 2.4, 6);

Page 10: C++ Programming for Graphics

Error trapping

• If “new” is unable to acquire memory– it will return “NULL”

• Should always test for this– e.g.

Account *pAcc = new Account(12.36, 2.4, 1);if(!pAcc) // test for NULL{

// Error handlercout << “Error allocating memory”;exit(1); // terminate application

}

Page 11: C++ Programming for Graphics

Accessing the object

• When using a pointer.

• Methods etc. are accessed differently

• Using “.” notation– e.g. (*pAcc).setBalance(15.26);

• Using an alternative notation “->”– e.g. pAcc->setBalance(15.26);

Page 12: C++ Programming for Graphics

Arrays

• So far, have used names for our accounts.

• Have just seen how to use individual pointers.

• Not very efficient!

• We can do better than that ….– Array of class objects.– Array of pointers.– Linked lists - later lecture

Page 13: C++ Programming for Graphics

Array Indexing

• Don’t forget…….– If you declare an array e.g. arAccs[10]– The indexing values are 0 to 9– NOT 1 to 10

• Also – don’t forget that in C/C++ there is no bounds checking.

Page 14: C++ Programming for Graphics

Array example

• Declaration– Account arAccs[3];

• This will work only under certain conditions– The class does not have a constructor

or– The class has a constructor that does not

require arguments.

Page 15: C++ Programming for Graphics

Arrays and Constructor Args

• Account arAcc[3];

• If Account has constructors that require zero arguments – equivalent to…

• Account arAcc[3] = {account(), account(), account()};

Page 16: C++ Programming for Graphics

Arrays and Constructor Args

• If wanted to include arguments.• Account arAccs[3] =

{account(12.36, 1.2, 1), account(100.52, 2.5, 2), account(125.0, 2.5, 3)};

• There are many other possibilities, all dependant upon the number of arguments required. You should consult your text books etc. for more information regarding this matter.

Page 17: C++ Programming for Graphics

Arrays and Pointers

• Also possible to have an array of pointers.– e.g. Account *parAccs[10];

An array of pointers to objects of type account

• Instantiation– e.g. parAccs[0] = new Account(12.36, 2.5, 1);

Page 18: C++ Programming for Graphics

Multiple Instantiations

Account *parAccs[10];

// Instantiate a number of Accountsfor(int nCount = 0; nCount < 10; nCount++){

parAccs[nCount] = new Account(0.0, 0.0, nCount +1);}

// Access a single accountcout << parAccs[1]->getBalance() << endl << endl;

Page 19: C++ Programming for Graphics

Destructors

• Just as a class can have constructors– That are “special” methods which…..– are called when instantiating an object

• It can also have a “special” methods that are called when deleting an object– These are called “destructors”– Take the same name as the class– But preceded by the “tilde” character – “~”

Page 20: C++ Programming for Graphics

Destructors

• So, for the class – Account.

• The destructor would be….– ~Account

• Destructors– Do not return a value.– Do not accept any arguments.– Called automatically when using “delete”.– Maximum of one per class.

Page 21: C++ Programming for Graphics

“delete”

• Just as “new” will acquire memory

• “delete” will release it back to the system

• Can only be used when memory via “new”– See malloc() & free (‘C’ programming)

• Use of delete– “Tidy up” objects – esp. if holding pointers.– Save object data.– etc.

Page 22: C++ Programming for Graphics

Summary

• In this lecture have considered– References– Use of “new”– Arrays - by type

- by pointers– “new”– Destructors– “delete”

Page 23: C++ Programming for Graphics

Next Lecture

• Copy Constructors

• Overloading operators– This is challenging so make sure you are up

to date with the work.