CS31-11 - CS 31 Introduction to Computer Science I Topic 11...

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-1 CS 31 : Introduction to Computer Science I Topic 11 Constructors & Destructors
Image of page 1

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-2 Constructors Constructor Function A constructor is a special member function of a class A constructor has the same name as the name of the class A constructor cannot have a return value not even void
Image of page 2
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-3 Constructors Constructor Invocation The constructor is called automatically every time an object of a class is instantiated The constructor can be used to perform any initialization actions required to set up an object of the class
Image of page 3

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-4 Constructors Constructor Parameters A constructor can have parameters The parameters can be used to provide data to initialize class data members Constructors can be overloaded Different constructors can be called with different function signatures
Image of page 4
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-5 Constructors Default Constructor The default constructor is the constructor that has no parameters The compiler will generate a default constructor if no constructor is defined The compiler will not generate a default constructor is any constructor is defined
Image of page 5

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-6 Constructors Default Constructor ClassName::ClassName() { // Initialization code }
Image of page 6
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-7 Constructors Constructor with Parameters A constructor can have parameters Multiple constructors with parameters can be written with different signatures The constructor that matches the parameters in the declaration of an object will be called
Image of page 7

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-8 Constructors Constructor with Parameters ClassName::ClassName( Parameters ) { // Initialization code using parameters }
Image of page 8
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-9 Constructors Complex Class Example Recall the Complex class example used previously to demonstrate classes The example will be extended to demonstrate constructors
Image of page 9

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A. Rohr All Rights Reserved JAR 11-10 Constructors Complex Class Example class Complex { public: void output(); private: double real; double imag; };
Image of page 10
CS 31: Introduction To CS I Topic 11: Constructor & 9/2009 John A.
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
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