///////////////////////////////////////////////////////////////////////////////// // ClassWithFunctions.cpp - demonstrates ideas used to build class instances // // // // Jim Fawcett, Summer Short Course, Su2016 // ///////////////////////////////////////////////////////////////////////////////// /* * Found in: Handouts\ShortCourse\Repository\GettingStartedExamples * */ #include #include class Cwf { public: // member functions Cwf(const std::string& msg); ~Cwf(); void say(); private: // member data std::string msg_; }; //----< constructor >------------------------------------------------ Cwf::Cwf(const std::string& msg) : msg_(msg) {} //----< destructor >------------------------------------------------- Cwf::~Cwf() { this->msg_ = ""; // this-> is optional } //----< non-static member function >--------------------------------- void Cwf::say() { std::cout << "\n " << /* this-> */ msg_.c_str(); } //----< test stub >-------------------------------------------------- int main() { Cwf c1("Hi, I'm c1"); c1.say(); Cwf c2("Hello, I'm c2"); c2.say(); std::cout << "\n\n"; // destructors are called when execution leaves scope where defined }