C Constructor Insanity (cont’d)

2y ago
6 Views
1 Downloads
793.76 KB
19 Pages
Last View : 2m ago
Last Download : 3m ago
Upload by : Javier Atchley
Transcription

L11: C Constructor InsanityCSE333, Autumn 2021C Constructor Insanity (cont’d)CSE 333 Autumn 2021Instructor:Chris ThachukTeaching Assistants:Arpad (John) DepaszthoryAngela XuIan HsiaoKhang Vinh PhanLogan GnanapragasamMaruchi KimMengqi (Frank) ChenCosmo Wang

L11: C Constructor InsanityCSE333, Autumn 2021Administrivia Exercise 9 out, due Wed (Oct. 27) @ 10am Operators will be covered in Monday’s lecture Homework 2 due next Thursday (Oct. 28) Midterm exam on Friday, Nov. 5 Take-home (completed independently)Exam will be open for 24 hours on GradescopeCan start / stop / resume all dayMore details will be posted on class website2

L11: C Constructor InsanityCSE333, Autumn 2021Lecture Outline ConstructorsCopy Constructors (restart)AssignmentDestructors3

L11: C Constructor InsanityCSE333, Autumn 2021Copy Constructors STYLETIPC has the notion of a copy constructor (cctor) Used to create a new object as a copy of an existing objectPoint::Point(const int x, const int y) : x (x), y (y) { }// copy constructorPoint::Point(const Point& copyme) {x copyme.x ;y copyme.y ;}void foo() {Point x(1, 2);Point y(x);// invokes the 2-int-arguments constructor// invokes the copy constructor// could also be written as "Point y x;"} Initializer lists can also be used in copy constructors (preferred)4

L11: C Constructor InsanityCSE333, Autumn 2021Synthesized Copy Constructor If you don’t define your own copy constructor, C willsynthesize one for you It will do a shallow copy of all of the fields (i.e., member variables)of your class Sometimes the right thing; sometimes the wrong thing#include "SimplePoint.h". // definitions for Distance() and SetLocation()int main(int argc, char** argv) {SimplePoint x;SimplePoint y(x); // invokes synthesized copy constructor.return EXIT SUCCESS;}5

L11: C Constructor InsanityCSE333, Autumn 2021When Do Copies Happen? The copy constructor is invoked if: You initialize an object fromanother object of the sametype: You pass a non-referenceobject as a value parameterto a function: You return a non-referenceobject value from a function:Point x;Point y(x);Point z y;// default ctor// copy ctor// copy ctorvoid foo(Point x) { . }Point y;foo(y);// default ctor// copy ctorPoint foo() {Point y;// default ctorreturn y;// copy ctor}6

L11: C Constructor InsanityCSE333, Autumn 2021Compiler Optimization The compiler sometimes uses a “return by valueoptimization” or “move semantics” to eliminateunnecessary copies Sometimes you might not see a constructor get invoked when youmight expect itPoint foo() {Point y;return y;}// default ctor// copy ctor? optimized?int main(int argc, char** argv) {Point x(1, 2);// two-ints-argument ctorPoint y x;// copy ctorPoint z foo(); // copy ctor? optimized?}7

L11: C Constructor InsanityCSE333, Autumn 2021Lecture Outline ConstructorsCopy ConstructorsAssignmentDestructors8

L11: C Constructor InsanityCSE333, Autumn 2021Assignment ! Construction “ ” is the assignment operator Assigns values to an existing, already constructed objectPoint w;Point x(1, 2);Point y(x);Point z w;y x;//////////default ctortwo-ints-argument ctorcopy ctorcopy ctorassignment operator9

L11: C Constructor InsanityCSE333, Autumn 2021Overloading the “ ” Operator STYLETIPYou can choose to define the “ ” operator But there are some rules you should follow:Point& Point::operator (const Point& rhs) {if (this ! &rhs) { // (1) always check against thisx rhs.x ;y rhs.y ;}return *this;// (2) always return *this from op }Point a;a b c;a (b c);(a b) c;////////default constructorworks because return *thisequiv. to above ( is right-associative)"works" because returns a non-const10

L11: C Constructor InsanityCSE333, Autumn 2021Synthesized Assignment Operator If you don’t define the assignment operator, C willsynthesize one for you It will do a shallow copy of all of the fields (i.e., member variables)of your class Sometimes the right thing; sometimes the wrong thing#include "SimplePoint.h". // definitions for Distance() and SetLocation()int main(int argc, char** argv) {SimplePoint x;SimplePoint y(x);y x;// invokes synthesized assignment operatorreturn EXIT SUCCESS;}11

L11: C Constructor InsanityCSE333, Autumn 2021Lecture Outline ConstructorsCopy ConstructorsAssignmentDestructors12

L11: C Constructor InsanityCSE333, Autumn 2021Destructors C has the notion of a destructor (dtor) Invoked automatically when a class instance is deleted, goes outof scope, etc. (even via exceptions or other causes!) Place to put your cleanup code – free any dynamic storage orother resources owned by the object Standard C idiom for managing dynamic resources Slogan: “Resource Acquisition Is Initialization” (RAII)Point:: Point() {// destructor// do any cleanup needed when a Point object goes away// (nothing to do here since we have no dynamic resources)}13

L11: C Constructor InsanityCSE333, Autumn 2021Destructor Exampleclass FileDescriptor {public:FileDescriptor(char* file) {fd open(file, O RDONLY);// Error checking omitted} FileDescriptor() { close(fd ); }int get fd() const { return fd ; }private:int fd ; // data member}; // class FileDescriptor// Constructor// Destructor// inline member functionFileDescriptor.h#include "FileDescriptor.h"int main(int argc, char** argv) {FileDescriptor fd(foo.txt);return EXIT SUCCESS;}14

L11: C Constructor InsanityCSE333, Autumn 2021pollev.com/cse333 How many times does the destructor get invoked? Assume Point with everything defined (ctor, cctor, , dtor) Assume no compiler optimizationstest.ccA.B.C.D.E.Point PrintRad(Point& pt) {Point origin(0, 0);double r origin.Distance(pt);double theta atan2(pt.get y(), pt.get x());cout "r " r endl;cout "theta " theta " rad" endl;return pt;}12int main(int argc, char**Point pt(3, 4);3PrintRad(pt);return EXIT SUCCESS;4}We’re lost argv) {15

L11: C Constructor InsanityCSE333, Autumn 2021Class Definition (from previous lecture)Point.h#ifndef POINT H#define POINT Hclass Point {public:Point(int x, int y);// constructorint get x() const { return x ; }// inline memberint get y() const { return y ; }// inline memberdouble Distance(const Point& p) const;// membervoid SetLocation(int x, int y);// memberfunctionfunctionfunctionfunctionprivate:int x ; // data memberint y ; // data member}; // class Point#endif// POINT H16

L11: C Constructor InsanityCSE333, Autumn 2021pollev.com/cse333 How many times does the destructor get invoked?ctorcctorop dtortest.ccPoint PrintRad(Point& pt) {Point origin(0, 0);double r origin.Distance(pt);double theta atan2(pt.get y(), pt.get x());cout "r " r endl;cout "theta " theta " rad" endl;return pt;}int main(int argc, char** argv) {Point pt(3, 4);PrintRad(pt);return EXIT SUCCESS;}17

L11: C Constructor InsanityCSE333, Autumn 2021Extra Exercise #1 Modify your Point3D class from Lec 9 Extra #1 Disable the copy constructor and assignment operator Attempt to use copy & assignment in code and see what error thecompiler generates Write a CopyFrom() member function and try using it instead (See details about CopyFrom() in next lecture)19

L11: C Constructor InsanityCSE333, Autumn 2021Extra Exercise #2 Write a C class that: Is given the name of a file as a constructor argument Has a GetNextWord() method that returns the nextwhitespace- or newline-separated word from the file as a copy ofa string object, or an empty string once you hit EOF Has a destructor that cleans up anything that needs cleaning up20

L11: C Constructor Insanity CSE333, Autumn 2021 Synthesized Copy Constructor If you don’t define your own copy constructor, C will synthesize one for you It will do a shallow copy of all of the fields (i.e., member variables) of your class Sometimes the right th

Related Documents:

defence of insanity 1.4 1 The relationship between the law on unfitness to plead and the defence of insanity 1.5 1 Unfitness to plead 1.7 2 The insanity defence 1.8 2 The way forward 1.10 2 The central question in this paper 1.18 4 The defences of insanity and automatism: the present law 1.23 5 Insanity 1.23 5 Automatism 1.27 5

Apr 04, 2020 · and objects, constructor , parameterized constructor Object instantiation Constructors Methods (defining & Calling) Types of constructor Parameter passing to methods Turn-02 3 Method overloading, constructor overloading Method overloading Constructor overloading

In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor is also empty, meaning that it does nothing. A programmer-defined constructor that takes no parameters is also called a default constructor.

Arizona, 548 U.S. 735 (2006), held that insanity rules are a matter of State choice. Due process does not require that a State provide any specific test of legal insanity, and therefore upheld Kansas’s insanity statute here. The dissent would have co

A constructor runs when the client uses the new keyword. A constructor implicitly returns the newly created and initialized object. If a class has no constructor, Java gives it a default constructor with no parameters that sets all the object's fields to 0 or null.

constructor, unless another constructor is invoked (when the last constructor in the chain will invoke the superclass constructor) (c)

L12: C Constructor Insanity CSE333, Fall 2022 Administrivia v Next exercise released today, due Monday morning §Write a substantive class in C (but no dynamic allocation -yet) §Look at Complex.h/Complex.cc(this lecture) for ideas §We now have a C/C style checker (cpplint) that works on the current linuxsystems! See the web Resources page for links

Article 505. Class I, Zone 0, 1, and 2 Locations Figure 500–2. Mike Holt Enterprises, Inc. www.MikeHolt.com 888.NEC.CODE (632.2633) 25 Hazardous (Classified) Locations 500.4 500.4 General (A) Classification Documentation. All hazardous (classified) locations must be properly documented. The documentation must be available to those who are authorized to design, install, inspect .