Constructors 11/10/10
Today Use constructors to initialize objects. Use const to protect data members.
Constructors
Constructor Public member functions that initialize their objects Automatically called when object declared
Constructor Syntax Always has the same name as the class Definition doesn't have a void or return type
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; }
//Default Constructor -- Always Provide Thermometer::Thermometer() { degrees = 0.0; scale = 'C'; } sec6.2/tconstr.cpp
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.
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 , #1-3