Logistics • Job related news C++ Classes: Conversion – Co-op orientation • Friday, Sept 16 th 12:00 – 1:30pm 70-1400 • Friday, Oct 14 th 1:00 – 2:30pm Eastman Aud. – Career Fair • Wednesday, Sept 28 th • 11am – 4pm • Gordon Field House Project Plan • Today • Reminder – Data type conversions – Part 1 (clock and design) due Sunday, Sept 25 th • conversion constructors – Start thinking about partners for Parts 2-3 • Assignment operators • Any questions? • Tomorrow – Overloading methods and operators • Thursday – Intro to templates Reminder Data type conversion • Exam 1 • C++ will perform datatype conversions when at all possible. – Next Thursday, Sept 29 th – More details to come short int a = 12; long b = a; // conversion from short to long 1
Data type conversion Constructor • Conversion occurs: • Special member function used to create an – Assignments object of a class. – Function argument passing – Has same name as the class. – Return values – Does not return anything. – Initializers – Used to initialize data members. – Expression • C++ can do so with classes as well if conversion constructors are defined. Box.h Box.cpp #ifndef BOX_DEFINED #include “Box.h” #define BOX_DEFINED Box::Box (int h, int w, int d) class Box { { height=h; width=w; depth=d; private: int height, width, depth; } public: Box (int h, int w, int d); int Box::volume () int volume (); { }; return height * width * height; #endif } Object initialization Conversion constructors • Objects are initialized using constructors • Converts an object of one type to that of made available by the class. another. • Will get called when the need for an Box mybox (3,4,5); automatic conversion arises. Box *boxOnHeap = new Box(3,4,5); 2
Conversion constructor Conversion constructor class Cube Box::Box (const Cube &C) : height (C.getSize()), { width (C.getSize()), depth (C.getSize()) private: {} int size; public: Cube (int s); int getSize(); } Cube C(6); Box convC = C; // conversion constructor called. class Box { private: int height, width, depth; public: Box (int h, int w, int d); Box (const Cube &C); }; Member conversion functions Member conversion function Date::operator long() • A class can also provide a means for it to be { converted to another class or intrinsic datatype. static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31}; long days = yr – 1900; days *= 365; class Date { days += yr/4; private: for (int i=0; i < mo-1; i++) days+=dys[i] days += da; int month,day, year; return days; public: } Date (int m,int d,int y); operator long(); // convert this date to a long Date today (9, 21, 2005); }; long since = today; Member conversion function Member conversion function class Box Cube::operator Box() { { private: int height, width, depth; return Box(size, size, size); public: Box(); } Box (int h, int w, int d); }; Cube C(6); class Cube Box convC; { convC = C; // conversion function called. private: int size; public: Cube (int s); operator Box(); } 3
Ways to invoke conversion Ways to invoke conversion class Cube • Implicit { private: – Assignment, function args, etc. int size; public: • Explicit Cube (int s); int getSize(); – Cast } – Explicit call to conversion constructor/member class Box conversion function { private: int height, width, depth; public: Box(); Box (int h, int w, int d); Box (const Cube &C); }; Ways to invoke conversion explicit Cube C(6); • You can prevent the compiler from Box convC; performing implicit conversions by convC = C; // implicit conversion. declaring the conversion constructor as convC = (Box)C; // explicit via cast explicit. convC = Box (C); // explicit via constructor call. explicit explicit class Cube Cube C(6); { Box convC; private: convC = C; // implicit conversion will generate error. int size; public: Cube (int s); convC = (Box)C; // explicit via cast--okay int getSize(); } convC = Box (C); // explicit via constructor call—okay. class Box { private: int height, width, depth; public: Box(); Box (int h, int w, int d); explicit Box (const Cube &C); }; 4
Conversions Assignment operator • Questions? • operator= – Called when an assignment is made – Copies all relevant data from object assigner to assignee. – Should check for self-assignment! Assignment operator Assignment operator class Complex Complex & Complex::operator= (Complex &c) { { private: if (c != (*this)) { double re, im; public: re = c.re; Complex(); im = c.im; Complex & operator= (Complex &c); } ... return (*this); } } Complex c1, c2; c2 = c1; // is the same as saying c2.operator= (c1); Assignment operator Assignment operator • Note that the assignment operator returns a • If no assignment operator is defined for a reference to itself class, the default assignment operator is used. – This is to allow statements like: – Member by member copy of data from one object to another. Complex c1, c2, c3; c3 = c2 = c1; – Can be troublesome if class have pointers as data members. 5
Assignment operator Assignment operator c1 class Foo Foo & Foo::operator= (Foo &F) { { private: // cleanup old array int delete array_member; *array_member; int asize; // allocate new array ... asize = F.asize; c2 } array_member = new int (F.asize); Foo c1, c2; // copy Free c1 = c2; for (int i=0; i<asize; i++) delete c1; store array_member[i] = F.array_member[i]; delete c2; } Constructor Constructor • Copy vs. Assignment • Copy Constructor Date::Date (int day, int month, int year) – If no copy constructor is defined for a class, the { d = day; // constructor + assignment performed default copy constructor is used. m = month; • Member by member copy of data from one object to y = year; another. } Can also be written using subobject constructor (this way is more efficient). • Can be troublesome if class have pointers as data members. Date::Date (int day, int month, int year) : d (day), m (month), y (year) {} – Same issues as with the default assignment // just copy constructor is called. operator!!!! Constructor Summary Constructor Date d1(3, 10, 2002); // constructor called • Important safety tips: Date d2, d5; // default constructor called – Always provide a default constructor Date d3 (d2); // copy constructor called Date d4 = d1; // copy constructor called – If your constructors perform any non-trivial d5 = d2; // assignment operator called. work (e.g. memory allocation), should define the full suite of: • Constructors • Copy constructor Questions? • operator= 6
Assignment operator Summary Questions? • Conversion – Conversion constructors – Conversion member functions • Assignment operators • Questions? 7
Recommend
More recommend