csi2172 class 5 Midterm: June 12
constructor Special method used to create objects of the class Never has a return type. Is called automatically upon the creation of the object. Main purpose is to pre-define the object's data members A properly written constructor will leave the object in a 'valid' state.
copy constructor Its purpose is to: Create the copy on the stack of the object passed Create the copy on the stack of the object returned Create a copy of the object
Accepts an object of the class itself as its argument Typically Z::Z(const Z&). A copy constructor is used for initialization of objects of type T with objects of type T. If a copy constructor is not declared for a class, memberwise initialization is used.
Assignment operator For a class, = is by default defined member-wise assignment; if necessary, the writer of a class can define it differently.class member
Assignment operator class student { private: char * name; int stno;... public: student& operator=(const student&);... };
student& student::operator=(const student& s) { if (&s == this) return *this; delete [] name; name = new char[strlen(s.name) + 1]; strcpy(name,s.name); return *this; }
destructor member of a class used to clean up before deleting an object.memberclassobject It's name is its class' name prefixed by '~'.name For example, Foo's destructor is ~Foo(). Often used to release resources. A destructor is implicitly called whenever an object goes out of scope or is deleted.resourcescope
Operator overload operator overloading means having more than one operator with the same name in the same scope. Built-in operators, such as + and *, are overloaded for types such as int and float.operatorname scopetypeintfloat Users can define their own additional meanings for user-defined types.user-defined type It is not possible to define new operators or to give new meanings to operators for built-in types.built-in type The compiler picks the operator to be used based on argument types based overload resolution rules.compilerargumentoverload resolution
Class review from class vector
The assignment
Inheritance
/* abstract class shape */ class shape { protected: bool filled; public: /* abstract draw method */ virtual void draw(canvas&) const = 0; virtual ~shape() { } void set_filled(bool); };
/* a concrete shape */ class polygon : public shape { protected: point * vertices; int n; public: polygon(int); /* copy constructor */ polygon(const polygon&); virtual ~polygon(); /* destructor */ /*assignment operator*/ polygon& operator=(const polygon&); void add_point(const point&); /* an actual implementation */ virtual void draw(canvas&) const; };
void polygon::draw(canvas& C) const { for(int i=1; i< n; ++i) C.draw_line(vertices[i-1],vertices[i]); C.draw_line(vertices[i-1]); /* slow */ if (filled) C.flood_fill(inside_point(vertices,n)); }
/* a refinement */ class rectangle : public polygon { public: rectangle(const point&, const point&); void draw(canvas&) const; };
void rectangle::draw(canvas& C) const { C.draw_line(vertices[0],vertices[1]); C.draw_line(vertices[1],vertices[2]); C.draw_line(vertices[2],vertices[3]); C.draw_line(vertices[3],vertices[0]); /* fast */ if (filled) C.rect_fill(vertices[0],vertices[3]); }
The midterm