c programming for graphics n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
C++ Programming for Graphics PowerPoint Presentation
Download Presentation
C++ Programming for Graphics

Loading in 2 Seconds...

play fullscreen
1 / 23

C++ Programming for Graphics - PowerPoint PPT Presentation


  • 146 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'C++ Programming for Graphics' - garron


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
c programming for graphics

C++ Programming for Graphics

Lecture 5

References, New, Arrays and Destructors

introduction
Introduction
  • References
  • New
  • Arrays
    • by type
    • by pointers
  • Destructors
constructors extra
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
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 “*”
references1
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
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
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.
slide8
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
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
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
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
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
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
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
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 args1
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
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
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
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 – “~”
destructors1
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
“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
Summary
  • In this lecture have considered
    • References
    • Use of “new”
    • Arrays - by type - by pointers
    • “new”
    • Destructors
    • “delete”
next lecture
Next Lecture
  • Copy Constructors
  • Overloading operators
    • This is challenging so make sure you are up to date with the work.