290 likes | 597 Views
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
E N D
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 • You lose the default constructor. • You have to provide all required.
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 “*”
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”
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
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.
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.
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);
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 handler cout << “Error allocating memory”; exit(1); // terminate application}
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);
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
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.
Array example • Declaration • Account arAccs[3]; • This will work only under certain conditions • The class does not have a constructoror • The class has a constructor that does not require arguments.
Arrays and Constructor Args • Account arAcc[3]; • If Account has constructors that require zero arguments – equivalent to… • Account arAcc[3] = {account(), account(), account()};
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.
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);
Multiple Instantiations Account *parAccs[10]; // Instantiate a number of Accounts for(int nCount = 0; nCount < 10; nCount++) { parAccs[nCount] = new Account(0.0, 0.0, nCount +1); } // Access a single account cout << parAccs[1]->getBalance() << endl << endl;
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 – “~”
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.
“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.
Summary • In this lecture have considered • References • Use of “new” • Arrays - by type - by pointers • “new” • Destructors • “delete”
Next Lecture • Copy Constructors • Overloading operators • This is challenging so make sure you are up to date with the work.