1 / 48

More about Constructors … Pointer Review for Exam 2

More about Constructors … Pointer Review for Exam 2. Thurs, april 10. Exam 2. During lecture. Tuesday, April 22 Review next week. Lab Final. During last week of regular classes. Monday, April 21 thru Friday, April 25 In your regularly scheduled lab. The Default Constructor.

qabil
Download Presentation

More about Constructors … Pointer Review for Exam 2

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. More about Constructors…Pointer Review for Exam 2 Thurs, april 10

  2. Exam 2. • During lecture. • Tuesday, April 22 • Review next week. • Lab Final. • During last week of regular classes. • Monday, April 21 thru Friday, April 25 • In your regularly scheduled lab.

  3. The Default Constructor • The default constructor is a constructor that takes no arguments. • This is the constructor you are calling when you declare an object with no parameters. E.g.,

  4. 1.1. Declaring a Default Constructor • It might be declared like this or with defaults: • Either way, we cancallit with no parameters. • It might be declared like this

  5. 1.2. Why default'? • Just an ordinary constructor,………but,…… • Used (implicitly) to initialize elements of an array. • Used (implicitly) in other ADTs' constructors when they do not explicitly initialize a data member.

  6. 1.3. Implicit Use: Arrays • For example, if we declared: • then each of the 5000 elements of this array will be initialized using the default constructor for string

  7. 1.4. Implicit Use: Other Constructors • bidPlacedAt is initialized using the Time default constructor. • bidderName and itemName are initialized using the string default constructor.

  8. 1.5. Explicit Construction in Other Constructors • If we don't want the default value, we need to explicitly perform some other initialization: This actually constructs a Time object and then copies it. Abit inefficient! • C++ provides a special way to directly call constructors

  9. 1.6. Explicit Construction: Initializer Lists • Alternate way to explicitly perform some other initialization: • data member name followed by constructor arguments

  10. 1.7. Initializer Lists • Can be used to initialize any data member • Must be used to initialize data members that are • constants (const) • references • members of classes that have no default constructors

  11. 1.8. The Helpful Compiler • If we create no constructors at all for a class, the compiler generates a default constructor for us. • Initializes each data member using their data types' default constructors • For primitives such as int, double, pointers, etc., this does nothing at all

  12. 1.9. Example: Name • Compiler will generate a default constructor Name() • givenName and surName will be initialized using the default constructor of string • Probably just fine

  13. 1.10. Example: Name 2 • Compiler will not generate a default constructor Name() because we provided a different constructor • If we want one, we have to write our own • If we don't, we cannot have arrays of Names

  14. 1.11. Example: Name 3

  15. 1.12. Example: BidCollectionBook implicit default constructor • Compiler would generate a default constructor that would leave random bits in all 3 data members

  16. 1.13. Example: BidCollectionBook explicit default constructor

  17. 2. Copy Constructors • The copy constructor for a class Foo is the constructor of the form:

  18. 2.1. Where Do We Use a Copy Constructor? The copy constructor gets used in 5 situations: • When you declare a new object as a copy of an old one:

  19. 2.1. Where Do We Use a Copy Constructor? The copy constructor gets used in 5 situations: 2. When a function call passes a parameter “by copy” (i.e., the formal parameter does not have a &):

  20. 2.1. Where Do We Use a Copy Constructor? The copy constructor gets used in 5 situations: 3. When a function returns an object

  21. 2.1. Where Do We Use a Copy Constructor? The copy constructor gets used in 5 situations: 4. When explicitly invoked in another constructor's initialization list:

  22. 2.1. Where Do We Use a Copy Constructor? The copy constructor gets used in 5 situations: 5. When an object is a data member of another class for which the compiler has generated its own copy constructor.

  23. 2.2. Compiler-Generated Copy Constructors If we do not create a copy constructor for a class, the compiler generates one for us. • copies each data member via their individual copy constructors. • For primitive types (int, double, pointers, etc.), just copies the bits.

  24. 2.3. Example: Bid: Compiler-Generated Copy Constructor Bid does not provide a copy constructor, so the compiler generates one for us, just as if we had written:

  25. 2.4. Example: BidCollection: Compiler-Generated Copy Constructor • BidCollection does not provide a copy constructor, so the compiler generates one for us, just as if we had written: which is not good at all!

  26. 2.5. Example: BidCollection: harder to copy • To see why, suppose we had some application code:

  27. The return statement makes a copy of bc, which is stored in afterNoonBidsremoveLate removes the remaining morning bid from bc. Note that we have corrupted the original collection, bids • It thinks it has more (according to size) • bids than are left in the array • The bids that it has are now changed • When we exit removeLate, the destructor for BidCollection is called on bc • Both collections have "dangling pointers"

  28. 2.14. Avoiding this Problem • We could • Decide never to pass a BidCollection by copy, never return one from a function, never to create a new BidCollection as a copy of an old one • We would have to remember this in all future applications • or, write our own copy constructor that actually works • Every BidCollection should have its own unique array

  29. 2.15. Writing a BidCollection Copy Constructor With this copy constructor in place, things should go much more smoothly

  30. 2.22. Shallow & Deep Copying • If our data members do not have explicit copy constructors (and their data members do not have explicit copy constructors, and …) ……………………. then the compiler-provided copy constructor amounts to a bit-by-bit copy.

  31. Shallow vs Deep Copy • Copy operations are distinguished by how they treat pointers: • In a shallow copy, all pointers are copied. • Leads to shared data on the heap. • In a deep copy, objects pointed to are copied, then the new pointer set to the address of the copied object. • Copied objects keep exclusive access to the things they point to.

  32. Shallow copy is wrong when... • Your ADT has pointers among its data members, and • You don't want to share the objects being pointed to.

  33. Compiler-generated copy constructors are wrong when... • Your ADT has pointers among its data members, and • You don't want to share the objects being pointed to.

  34. 3. Assignment Copy constructors are not the only way we make copies of data. • Even more common is the use of assignment: time2 = time1; • Assignment is so common that it can be hard to imagine programming • though in rare conditions we will do so • E.g., you cannot assign istreams and ostreams • You cannot copy them by constructor either • So the compiler will try to be helpful again

  35. 3.2. Example: BidCollection: Guess what happens • Our BidCollection class has no assignment operator, so the code below uses the compiler-generated version. To see why, suppose we had some application code:

  36. Assume we start with this in bids • After the assignment, we have copied • bids, bit-by-bit, into bc

  37. After the assignment, we have copied • bids, bit-by-bit, into bc • We remove the first morning bid from bc Then remove the remaining morning bid from bc. Note that we have corrupted the original collection, bids • It thinks it has more (according to size) bids than are left in the array • The bids that it has are now changed

  38. Pointer Review. 1a) Show the output produced by the program shown below. The input entered at the keyboard is 2 1 5 3 4 6

  39. Pointer Review. 1c) Continuing with the same example: 2 1 5 3 4 6

  40. 2a) Show the output produced by the program shown below.

  41. 2b) Show the output produced by the program shown below.

  42. 3) Show the output produced by the program shown below. The input entered at the keyboard is 2 1 5 6 8 Declare some pointers and variables to work with. Acquire input data pointer assignments and assignment statements

  43. 3b) Show the output produced by the program shown below. The input entered at the keyboard is 2 1 5 6 8 …….. ………

  44. LL_Exam2_Practice_.docin our Week 13 lecture directory.Print it outFill in the solutions.Bring it to class on Tuesday, and we will review it together as needed.

More Related