announcement
play

Announcement RIT Career Fair C++: Constructors and Thursday, Oct - PDF document

Announcement RIT Career Fair C++: Constructors and Thursday, Oct 3 rd 1pm 7pm Destructors Friday, Oct 4 th 9am 5pm (interviews) Clark Gym www.rit.edu/co-op/careers Announcement Project Questions? Date for


  1. Announcement • RIT Career Fair C++: Constructors and – Thursday, Oct 3 rd 1pm – 7pm Destructors – Friday, Oct 4 th 9am – 5pm (interviews) • Clark Gym • www.rit.edu/co-op/careers Announcement Project • Questions? • Date for Exam 1 will probably be changed – New date TBD. • Yes, there will be only one project. – Will know new date by Thursday. – Typo on Web page. • Everyone have a partner? • Please e-mail me with the name of your partner and I will assign you a group account. • RCS in group accounts. Plan for today Constructor • Constructors • A constructor for a class is called when an object of that class is created: • Destructors – Local / Stack Based • Enumerated Types • Foo F (3, 4, “Joe”); • Assertions – On Free Store • Foo *Fptr (new Foo ((3, 4, “Joe”)); 1

  2. Constructor Constructor class Date { • Constructors have the same name as the private: class. int d, m, y; • Constructors do not return a value. public: Date (int day, int month, int year); • The constructor, like all functions, can be Date (int day, int month); overloaded with each constructor having a Date (int day); different set of parameters. Date (); // today’s date Date (const *char stringdate); … } Constructor Constructor Date::Date (int day, int month, int year) • Default Constructor { – Constructor with no arguments. d = day; • Foo F; m = month; y = year; – If a class defines constructors but no default } constructor, compiler will generate an error. Can also be written using subobject constructor (this way is more efficient). – If no constructors are defined for a class, the Date::Date (int day, int month, int year) : compiler will generate a default constructor. d (day), m (month), y (year) {} Constructor Constructor • Copy Constructor • Copy Constructor – Initializes an object based on the contents of another – Called when: object of the same type. • A declaration is made with initialization from Date (const Date &D) : another object d (D.d), m (D.m), y (D.y) {} – Date d1 (d2); • Parameters are passed by value. – Object has access to non-public members of objects of • An object is returned by a function. the same class 2

  3. Constructor Constructor • Copy vs. Assignment • Copy vs. Assignment Date::Date (int day, int month, int year) – operator= is called when an assignment is made… { • Date d1 (10, 2, 2002); d = day; // constructor + assignment performed • Date d2; m = month; y = year; • d2 = d1; // operator= called } – However, Can also be written using subobject constructor (this way is more efficient). • If an assignment is made during a variable declaration, then the copy constructor rather than the assignment operator is called Date::Date (int day, int month, int year) : – Date d1 = d2; // Copy NOT assignment! d (day), m (month), y (year) {} // just copy constructor is called. Constructor Constructor Summary Date d1(3, 10, 2002); // constructor called • Copy Constructor Date d2, d5; // default constructor called – If no copy constructor is defined for a class, the Date d3 (d2); // copy constructor called default copy constructor is used. Date d4 = d1; // copy constructor called d5 = d2; // assignment operator called. • Member by member copy of data from one object to another. • Can be troublesome if class have pointers as data members. Questions? – Same issues as with the default assignment operator!!!! Constructor Destructor • A destructor for a class is called when the memory • Important safety tips: of an object of that class is reclaimed: – Always provide a default constructor – A global (static) object is reclaimed when the program – If your constructors perform any non-trivial terminates. work (e.g. memory allocation), should define – A local (automatic) object is reclaimed when the function terminates (stack is popped). the full suite of: – A dynamically allocated object is reclaimed when • Constructors someone invokes the delete operator on it. • Copy constructor • operator= – Like Java finalize 3

  4. Destructor Destructor • Destructors have the same name as the class but void aFunction (Foo f) preceded with a ~. { • Destructors do not return a value. Foo f2; Foo *fooptr = new Foo(); • Destructors take no arguments and cannot be overloaded. ... delete fooptr; // destructor called • Destructors are used for cleaning up object data / state } – Allocated memory // after function is complete, destructor – Close files called on f and f2 – Close network connections, etc. Destructors Destructors class Foo Foo::Foo (int size) : asize (size), array_member (new int[size]) { { private: for (int i=0; i<size; i++) int *array_member; array_member[i] = 0; int asize; } ... Foo::~Foo () public: { Foo (int size); // cleanup what was allocated by ~Foo (); // constructor } if (array_member != 0) delete array_member; } Destructors Enumerated Types • Questions? • An enumeration is a type that can hold a set of values defined by the user. • Once defined, they can be used like an integer type. • enum statement assigns sequential integer values to names and provide a type name for declaration. 4

  5. Enumerated Types Enumerated Types • It's possible to control the values that are enum TrafficLightColor {RED, YELLOW,GREEN}; assigned to each enum constant. – enum Day {MON=1, TUE, WED, THU, TrafficLightColor x; FRI, SAT, SUN}; x = YELLOW; – WED == 3 – enum DayFlag {MON=1, TUE=2, WED=4, THU=8, FRI=16, SAT=32, SUN=64}; Enumerated Types Enumerated Types • Java doesn’t have enums, instead, Java uses • Enums are types, not just integers constants: public int final MON=1; enum TrafficLightColor {RED, YELLOW, GREEN}; public int final TUE=2; enum Gender {MALE, FEMALE}; TrafficLightColor x; public int final WED=3; . . . public int final THU=4; x = YELLOW; // good public int final FRI=5; x = FEMALE; // error x = 2; // error public int final SAT=6; public int final SUN=7; Enumerated Types Enumerated Types • Enums can be placed in the scope of a class: • Questions? class Calendar { public: enum Day {MON=1, TUE, WED, THU, FRI, SAT, SUN}; ... } Calendar::Day x; x = Calendar::SAT; 5

  6. Assertions Assertions • Debugging mechanism to check condition #include <cassert> at a given point in the code void f (int *p) – If condition is false, then program will abort { with an error message then dump core. // At this point p should be non-null assert (p!=0); – Detects code error, not user error. … } Assertions Assertions • Used for debugging • When to use – Can be turned off – Test preconditions – Removing does not affect how the program – Test postconditions works – “At this point x should be equal to …” – “We should never reach this line” – CC –DNDEBUG foo.C Summary • Constructors – Copy Constructor – Default Constructor • Destructor • Enum • Assertion • Questions? 6

Recommend


More recommend