1 / 23

C++ Programming for Graphics

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

garron
Download Presentation

C++ Programming for Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Introduction • References • New • Arrays • by type • by pointers • Destructors

  3. 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.

  4. 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 “*”

  5. 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”

  6. 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

  7. 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.

  8. 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.

  9. 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);

  10. 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}

  11. 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);

  12. 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

  13. 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.

  14. 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.

  15. Arrays and Constructor Args • Account arAcc[3]; • If Account has constructors that require zero arguments – equivalent to… • Account arAcc[3] = {account(), account(), account()};

  16. 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.

  17. 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);

  18. 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;

  19. 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 – “~”

  20. 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.

  21. “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.

  22. Summary • In this lecture have considered • References • Use of “new” • Arrays - by type - by pointers • “new” • Destructors • “delete”

  23. Next Lecture • Copy Constructors • Overloading operators • This is challenging so make sure you are up to date with the work.

More Related