Created
April 30, 2015 09:43
-
-
Save safiire/9ea3c2c0144758b14e6f to your computer and use it in GitHub Desktop.
Example of returning a string initialized in the constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Comple with g++ *.cpp -o prog // | |
#include <iostream> | |
#include "test.h" | |
using namespace std; | |
int main(int argc, char **argv){ | |
Test test; | |
cout << "My name is " << test.serialize() << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "test.h" | |
Test::Test() : m_name("saf") {} | |
string Test::serialize(){ | |
return m_name; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TEST_H | |
#define TEST_H | |
#include <string> | |
using namespace std; | |
class Test { | |
private: | |
string m_name; | |
public: | |
Test(); | |
// Be aware that this returns a copy of the string using | |
// the default copy constructor | |
string serialize(); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment