FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Explaination about this C++ code needed





akshar
#include <iostream.h>
#include <string.h>
class string
{
private:
int size;
char *ptr;
public:
///EXPLAIN ME WHAT IS THIS PLEASE//////////
string() : size(0), ptr(new char('\0')) {}
string(const string &s) : size(s.size)
{
ptr = new char[size + 1];
strcpy(ptr, s.ptr);
}


~string()
{
delete [] ptr;
}
friend ostream &operator <<(ostream &, const string &);
string &operator=(const char *);
};


ostream &operator<<(ostream &stream, const string &s)
{
return(stream << s.ptr);
}
string &string::operator=(const char *chrs)
{
if (this != &chrs)
{
delete [] ptr;
size = strlen(chrs);
ptr = new char[size + 1];
strcpy(ptr, chrs);
}
return(*this);
}
int main()
{
string str;
str = "Hello Akash";
cout << str << endl;
return(0);
}
[b]I am fairly familier with c++ programming but the above mentioned constructor; I am watching this type of declatation for the first time. Just explain this or provide related links.
akshar
no one knows this or are you lazy to type the answer?
lol what
I'll explain as best I can.

string() : size(0), ptr(new char('\0')) {}

This is a constructor function. It is called when you create a new string without any arguments. It is created with size 0, and the pointer ptr is set the null character, since the string is empty.

string(const string &s) : size(s.size)

This is the beginning of another constructor function. It is called when when you create a new string with a string as an argument. It sets the size of the new string to be equal to the size of the string passed as an argument.

I'm afraid I can't adequately describe the rest.
HoboBarticus
string() : size(0), ptr(new char('\0')) {}

This is a default constructor for the String Class. Whenever you decalre a new sting this is what is called to initilize it. The constructor allocates the memory for the object and gives it the default assignments, size zero (Length), and a ptr to the first character in string.(if You want to know why there is a pointer let me know, I can tell ya that also.


string(const string &s) : size(s.size)
{
ptr = new char[size + 1];
strcpy(ptr, s.ptr);
}


This is an overloaded constructor. If you declare a string to have a value, such as:
string myString = "Nintendo Wiivolution";
Then instead of using the default constructor it uses this initilizer. It copies the text and then creates a string from it. That is what the 2 lines contained within the curly braces do.

If you have any other questions feel free to PM if you find me helpful. And I hope this helps.
Related topics
C++ TUTORIAL : INTRODUCTION TO C++
What's wrong with this C code???
C++
C++ code help
Simple PHP forum code needed!
This C code is little unclear to me.
C++ code
A little help on C++
C++ Codes Tips Tricks for competition...
Funny with C code.
vbsys2.dll = Trojan horse?
C++ code problem
10 frih$ contest: what does this c++ code do?
Slight problem with C code
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.