Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructors 11/10/10. Today  Use constructors to initialize objects.  Use const to protect data members.

Similar presentations


Presentation on theme: "Constructors 11/10/10. Today  Use constructors to initialize objects.  Use const to protect data members."— Presentation transcript:

1 Constructors 11/10/10

2 Today  Use constructors to initialize objects.  Use const to protect data members.

3 Constructors

4 Constructor  Public member functions that initialize their objects  Automatically called when object declared

5 Constructor Syntax  Always has the same name as the class  Definition doesn't have a void or return type

6 class Thermometer { public: Thermometer (double d, char s); //Constructor void input_T(); void rise(double incr); private: double degrees; char scale; //F or C };. Thermometer::Thermometer(double d, char s)‏ { degrees = d; scale = s; }

7 //Default Constructor -- Always Provide Thermometer::Thermometer()‏ { degrees = 0.0; scale = 'C'; } sec6.2/tconstr.cpp

8 Another Example  rectangle3.cpp  const protects the data members from change. Use it when a function doesn't change data members. Use it when a function doesn't change data members.

9 Constructors  What would constructors for gastank.cpp be?  What function could be constant functions?  What about dog.cpp? Add constructors Add constructors What functions should be constant. What functions should be constant.  Do exercises, p. 226-227, #1-3


Download ppt "Constructors 11/10/10. Today  Use constructors to initialize objects.  Use const to protect data members."

Similar presentations


Ads by Google