EECS
Lec07CtorsDtors_6per.pdf

Lec07CtorsDtors_6per.pdf - EECS 402 Objects As Members The...

Info icon This preview shows pages 1–2. Sign up to view the full content.

1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 07 Savitch Ch. 7 Objects Constructors Destructors EECS 402 EECS 402 Andrew M Morgan 2 Objects As Members Class attributes (i.e. data members) can be of any data type Including user-defined class types class InnerClass { public: int inInt; void print(); }; class OuterClass { public: int outInt; InnerClass inObj; InnerClass inObj2; void print(); }; The outer class has 2 member objects of type InnerClass (in addition to the integer data member "outInt"). Both classes have a function with the same prototype "void print();". How would you implement both functions outside of the class definitions without ambiguity? EECS 402 EECS 402 Andrew M Morgan 3 Example Implementation While the function prototypes are the same, they are unique in what class they belong to The use of scope resolution can solve the ambiguity problem Remember scope resolution is often read "belongs to" void InnerClass::print() { cout << "InnerClass Int: " << inInt << endl; } void OuterClass::print() { cout << "OuterClass Int: " << outInt << endl; cout << "Obj: "; inObj.print(); cout << "Obj2: "; inObj2.print(); } This version of print() "belongs to" the class InnerClass This version of print() "belongs to" the class OuterClass EECS 402 EECS 402 Andrew M Morgan 4 Using InnerClass and OuterClass The dot operator can be used on any object Sometimes, this results in many dots being used in one statement int main() { OuterClass outObj; InnerClass inObj; outObj.outInt = 15; outObj.inObj.inInt = 20; outObj.inObj2.inInt = 30; outObj.print(); cout << endl; inObj.inInt = 50; inObj.print(); return (0); } OuterClass Int: 15 Obj: InnerClass Int: 20 Obj2: InnerClass Int: 30 InnerClass Int: 50 inInt is a data member of inObj, which is a data member of outObj, and is accessed as shown EECS 402 EECS 402 Andrew M Morgan 5 A constructor is a special member function of a class The name of the function is the same as the name of the class It has no return type (can not return a value) It is automatically called every time a new object is constructed The programmer can not directly "call" the constructor Often abbreviated "ctor" A destructor is a special member function of a class The name of the function is the same as the name of the class with a "~" before it It has no return type (can not return a value) It is automatically called when an object is destroyed When a static object goes out of scope, or a dynamic object is deleted (will discuss later The programmer can not directly "call" the destructor Often abbreviated "dtor" Constructors & Destructors EECS 402 EECS 402 Andrew M Morgan 6 Ctor And Dtor Purpose Why are ctors important?
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern