11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

212Part III: Introduction to ClassesMaking constructors workThe constructor is a special member function that’s called automatically whenan object is created. It carries the same name as the class to differentiate itfrom the other members of the class. The designers of C++ could have madeup a different rule, such as: “The constructor must be called init().” Itwouldn’t have made any difference, as long as the compiler could recognizethe constructor. In addition, the constructor has no return type, not evenvoid, because it is only called automatically — if the constructor did returnsomething, there would be no place to put it. A constructor cannot beinvoked manually.Constructing a single objectWith a constructor, the class Student appears as follows://// Constructor - example that invokes a constructor//#include #include #include using namespace std;class Student{public:Student(){cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!