[C++] Problem with a function

Started by
3 comments, last by Zahlman 15 years, 2 months ago
I'm new to C++, but I've already programmed C# with OOP and I'm trying this exercise in OOP programming with C++. I have a class that represents a date and I want a function to print it in several formats. The first format is "00/00/00". This is the vars in the class and the func:
[SOURCE]
class Date
{
public:
	int day, month, year;
        ...
	char* PrintDate(int format);
        ...
};
[/SOURCE]
And this is the function:
[SOURCE]
char* Date::PrintDate(int format)
{
	switch (format)
	{
	case 1:
		return day + "/" + month + "/" + year;
		break;
	case 2:
		break;
	case 3:
		break;
	case 4:
		break;
	}
}
[/SOURCE]
Adding the vars is what I would do in C#, but how is it done in C++? to return a char* (string)? Thanks in advance.
Advertisement
Use the Standard C++ string type:
#include <string>class Date{public:	int day, month, year;        ...	std::string PrintDate(int format);        ...};

Raw char arrays or char pointers are a poor choice as they are either inflexible or horrible to maintain. Your format should probably be an enum rather than just raw integer constants.
Read up on c++ stringstream, integral types in c++ are not object like, like they are in c#.
Also, I'd use an enum for the format, rather than an int. Makes things easier to use, as the intentions of (and the various options for) that parameter become more obvious.
Create-ivity - a game development blog Mouseover for more information.
Quote:Original post by Portishead
Adding the vars is what I would do in C#, but how is it done in C++? to return a char* (string)?


1) char*s are not strings - at least not in a sense that anyone coming from a C# background would recognize. You want to use the standard library type, std::string, which comes from the <string> header.

2) The string library type allows you to concatenate two strings with '+', but it doesn't provide conversions for other types (except single characters represented with 'char'). What you want to do is use the standard library class std::ostringstream, which comes from <sstream>, to do the concatenation, and then extract the string from the stringstream. This is a little bit like messing around manually with StringBuilders or whatever C# calls them.

3) However, in C++, we don't normally make things "printable" by converting them into strings (and we especially don't use a name like "PrintDate" for a function that doesn't actually do any printing). (And we also, in either C++ or C#, avoid using the name of the class within the names of the member functions, because it doesn't add any information.)

Instead, we use operator overloading. Perhaps you have seen how the standard library object (not function!) std::cout is used for outputting primitive types to the console. The idea is that std::cout is an instance of a class std::ostream (or something derived from that), and we can overload the << operator so that it takes a std::ostream on the left-hand side (just as when we output a primitive type) and an instance of our class on the right-hand side.

(The 'o' at the beginning of 'ostream' and 'ostringstream' stands for 'output', naturally.)

In order to make operator chaining work, we return the left-hand side from our operator-overloading function after we're done with it. See here, or here. Oh, and of course here, where the whole process is illustrated.

Of course, this doesn't (yet!) give us the flexibility to choose an output format, but there are ways around that. And keep in mind that in C#, the standard library classes don't generally work like that either - there is no "format" parameter for .ToString() or whatever, but instead classes like DateFormatter or whatever (I'm actually much more experienced with Java than with C#, so I don't know the details).

Note that if you decide that you do want to write something similar to .ToString() after all, the basic approach is to just write to a std::ostringstream instance in the same way (it's a kind of std::ostream, after all), and then extract the string from it using its .str() member function.

(4) In C++, unlike in C#, 'class' and 'struct' create the same kind of thing. The only difference is that 'class'es default to 'private' access for members and bases, while 'struct's default to 'public' access.

Thus, we could write something like:
#include <iostream>#include <sstream>#include <string>struct Date {  int day, month, year;  ...  std::string ToString();};std::ostream& operator<<(ostream& os, const Date& d) {  return os << d.day << '/' << d.month << '/' << d.year;}std::string Date::ToString() {  std::ostringstream builder;  builder << *this; // makes use of the operator overload above.  return builder.str();}

This topic is closed to new replies.

Advertisement