{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info iconThis 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 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-1 CS 31 : Introduction to Computer Science I Topic 7 Using Classes
Background 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 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-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 Topic 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-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 Document Right Arrow Icon
CS 31: Introduction To CS I Topic 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-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 Topic 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-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 Document Right Arrow Icon
CS 31: Introduction To CS I Topic 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-6 Constructors Default Constructor ClassName::ClassName() { // Initialization code }
Background image of page 6
CS 31: Introduction To CS I Topic 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-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 Document Right Arrow Icon
CS 31: Introduction To CS I Topic 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-8 Constructors Constructor with Parameters ClassName::ClassName( Parameters ) { // Initialization code using parameters }
Background image of page 8
CS 31: Introduction To CS I Topic 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-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 Document Right Arrow Icon
CS 31: Introduction To CS I Topic 7: Using Classes 9/2009 John A. Rohr All Rights Reserved JAR 7-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 Topic 7: Using Classes 9/2009 John A.
Background 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 ]}