Lec10InClass

Lec10InClass - Constructors Queues int *pi = new int; Link...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Constructors Queues int *pi = new int; Link *l = new Link; If we don’t give a constructor any arguments, then that is called the default constructor – you can define it yourself OR the system will define one for you. *pi = 5; l->setValue(5); int i = 7; int i; Link l; // declare l, create space, call default constructor i = 7; l.setValue(7); How do I make an array of Link objects? int *iArray = new int[10]; Link *lArray = new Link[10]; int iArray[10]; Link lArray[10]; What’s the difference between those two ways? 1) lArray is a local array, and it can never point anywhere else larray = …… with new, if you decide you want more memory, you can allocate a bigger array and then point lArray to that bigger array later 2) the local array disappears when the function returns, but the new array stays around int *pi, *pj, *pk; // makes three pointers int* pi, pj, pk; // makes one pointer and two integers What about a constructor that takes input arguments? int *pi = new int; Link *pl = new Link(5);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

Lec10InClass - Constructors Queues int *pi = new int; Link...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online