Solved

Simple C++ question

Posted on 2013-01-28
2
186 Views
Last Modified: 2013-01-28
Hello experts,

please have a look at the code below:

#include <iostream>
#include <cstring>
using namespace std;

//tworze klase, ktora umozliwi dodawanie stringow oraz
//zapis stringow w postaci zmienna = "jakis string";

class String{
	char * string;

public:
	String(const char* aChar){
		string = new char[????];   how can I determine the length of aChar here?
		strcpy(string, aChar);
		cout<<string<<endl;
	}

	~String(){
		delete string;
		string = 0;
	}

	char * getString(){
		return string;
	}

	void operator = (char * aString){
		strcpy(string, aString);
	}

	friend ostream& operator<< (ostream & aOstream, const String & aString);

};

ostream& operator<< (ostream & aOstream, const String & aString){
	aOstream << aString.string;
	return aOstream;
}

int main(){
	String string1("kdjshfgkljs");
	String string2("skldjhfls");

	cout << string1 << string2;

	return 0;
}

Open in new window


Thank you

panJames
0
Comment
Question by:panJames
  • 2
2 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 38827233
Hi panJames,

you can use strlen, i.e.:
string = new char[strlen(aChar)+1]; // need to add 1 for terminating NUL character

Open in new window

Hope that helps,

ZOPPO
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 38830005
BTW, two more points:

1. In the constructor you should check if the passed string is a valid pointer.
2. The assignment operator isn't correctly implemented, it doesn't (re-)allocate the memory needed.

So I would change these two functions to look like this:
	String(const char* aChar){
		if ( aChar != 0)
		{
			string = new char[strlen(aChar)];
			strcpy(string, aChar);
			cout<<string<<endl;
		}
		else
		{
			string = 0;
			cout << "empty string"<< endl;
		}
	}

	void operator = (char * aString)
	{
		delete [] string;
		string = 0;

		if ( aString != 0)
		{
			string = new char[strlen(aString);
			strcpy(string, aString);
		}
	}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now