Help With C++ CLASS Definition -
i have bit of code:
int main() { string s1; // s1 == "" assert(s1.length() == 0); string s2("hi"); // s2 == "hi" assert(s2.length() == 2); cout << "success" << endl; }
and want write class go through , ending "cout" statement.
so far, have this:
#include <iostream> #include <cassert> #include <string> using namespace std; class string { int size; char * buffer; public: string(); string(const string &); string(const char *); ~string(); int length(); }; string::string() { buffer = 0; } string::string(const string &) { } string::string(const char *) { buffer = 0; } string::~string() { delete[ ] buffer; } string::length() { }
which believe correct far, @ least in terms of how class should built i'm no sure should go within of member functions. can me out or show me example of need class go through main program , compute correct buffer size , read in strings?
thanks in advance.
do know pointers , arrays?
string array of characters. need allocate right amount of memory using new[] operator, make sure it's cleaned when it's supposed (you've got right, delete[] operator correct way) , put there content want.
copy constructors should iterate through passed char*(whether it's in parameter or internal char* in string object - it's same) , make copy of content.
in c++ it's common use "null terminated strings" meaning every char* has 0 (binary, not ascii character 0) @ end. function you'll need strlen - returns length of string passed in argument (by string mean char* of course)
Comments
Post a Comment