COM SCI 32 Lecture Notes - Lecture 2: Init, System Resource
Document Summary
Class cylinder { private: int radius; int height; public: void init(r. h) make a simple class first, just get the public interface right later make the implementation more efficient. Constructors: just use name of the class as the name of the function, no return type: class gassy public: Gassy(int age, bool atebeans) m_age = age; m_atebeans = atebeans; private: int m_age; bool m_atebeans; Contsructor automatically called every time you create a new variable of that class. You can also define constructor outside the class declaration (class declaration could be in . h while some functions are defined in . cpp) For small proejctes, its easier to put everything in the class declaration. For bigger projects, it might be faster to define some functions outside. Gassy alan; // compiler error, no constructor with 0 parameters another constructor: Gassy lec1[4]; when you call a ptr, it does not call the constructor. Gassy *ptr = new gassy(1,3); // constructor is called when you use new.