CS31-11

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

Info iconThis preview shows pages 1–13. Sign up to view the full content.

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

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights 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
Background image of page 2
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights 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
Background image of page 4
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 11-6 Constructors Default Constructor ClassName::ClassName() { // Initialization code }
Background image of page 6
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 11-8 Constructors Constructor with Parameters ClassName::ClassName( Parameters ) { // Initialization code using parameters }
Background image of page 8
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights 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
Background image of page 9

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 11-10 Constructors Complex Class Example class Complex { public: void output(); private: double real; double imag; };
Background image of page 10
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 11-11 Constructors Complex Class Initialization As written, the Complex class has no way to set values into the member variables real and imag A constructor could be used to set values in the variables when objects of the class Complex are declared
Background image of page 11

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 61

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

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online