1 / 9

Classes

Classes. Special member functions: constructors have the same name as the class are used to initialize the data members are invoked automatically when class objects are created do not have a return type destructor has the same name as the class preceded by a tilde (~)

lawsonr
Download Presentation

Classes

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. Classes • Special member functions: • constructors • have the same name as the class • are used to initialize the data members • are invoked automatically when class objects are created • do not have a return type • destructor • has the same name as the class preceded by a tilde (~) • is used to perform housekeeping (such as memory deallocation) when a class object is destroyed • is invoked automatically when class objects are destroyed • does not have arguments • does not have a return type • is ESSENTIAL when there are pointer data members

  2. Classes • When a data member is a pointer, the constructor must allocate space for it and the destructor must deallocate space. • Example: class GradeInfo { private: int *scores; int class_size; public: GradeInfo (int class_size = 1) { scores = new int[class_size]; for (int i=0; i<class_size; i++) scores[i] = 0; } ~GradeInfo() { delete [ ] scores; } };

  3. Copy constructor • There are certain situations when a copy of an object must be made: • when passing the object by value • when returning an object • Furthermore, we want to be able to initialize an object by another object. • e.g. • Creating a copy of an object is the job of the copy constructor. • A special kind of constructor that takes as argument an object of the same type as the class. GradeInfo quiz1 (11); // Now declare copy, a new GradeInfo object that is meant // to be an exact copy of quiz1: GradeInfo copy(quiz1);

  4. Copy constructor • If a copy constructor is missing, the compiler creates a default one performs member-by-member copying. • A default copy constructor for the MemberInfo class would look like this: • As we will see, this is INCORRECT in this case! • Consider the following piece of code: GradeInfo (const GradeInfo& init) { scores = init.scores; class_size = init.class_size } GradeInfo *quiz = new GradeInfo (5); quiz->read_grades(); // reads in the quiz grades GradeInfo backup(*quiz); delete quiz; quiz = NULL; continue on next slide...

  5. Copy constructor & pointers 1:GradeInfo *quiz = new GradeInfo (11); quiz->read_grades(); quiz: 10 9 13 2 7 9 5 2: GradeInfo backup(*quiz); Let's look inside the copy constructor: scores = init.scores; class_size = init.class_size quiz: 10 9 13 2 7 9 5 backup: 5 continue on next slide...

  6. Copy constructor & pointers 3: delete quiz; quiz = NULL; quiz: 0 freed memory backup: 5 backup and quiz should be independent objects. Modifying one should not affect the other. When pointers are involved, the copy constructor must explicitly allocate space and copy the appropriate value. (IOW, it must make a deep copy) See the next slide for a correct version of the copy constructor.

  7. Copy constructors & pointers GradeInfo (const GradeInfo& init) { scores = new int[init.class_size]; class_size = init.class_size; for (int i=0; i<class_size; i++) scores[i] = init.scores[i]; }

  8. operator= • A problem similar to that of the copy constructor appears when we use assignments. • If quiz and backup are two GradeInfo objects, then by default, quiz =backup; will perform a member-by-member assignment. • To avoid the problem, the assignment operator must be overloaded by the programmer, so that it performs a deep assignment. • We will learn about overloading later...

  9. pointer data members • Whenever a class has at least one data member that is a pointer, you MUST write • a destructor • a copy contructor • an overloaded assignment operator THE BIG THREE! If you need one of them, then you also need the other two!

More Related