void Person::setDOB(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
string Person::getDOB()
{
return day + "-" + month + "-" + year;
}
Hello,
The above code is Java.
I was wondering if it where possible to return multiple variables at the sametime in c++? i.e. the getDOB() method...if so how would this be done?
That Java code isn't returning multiple variables. It is returning a single string composed by concatenating the values of the variables and a couple of hyphens. I don't even think that syntax compiles, though. Not enough + operators (needed between the hyphens and the variable names immediately following).
#include <iostream>
#include <string>
#include <sstream>
class Person
{
private:
int day, month, year;
public:
void setDOB(int d, int m, int y);
std::string getDOB();
};
void Person::setDOB(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
std::string Person::getDOB()
{
std::stringstream ss;
ss << day << "-" << month << "-" << year;
return ss.str();
}
int main()
{
Person p;
p.setDOB(1, 2, 3);
std::cout << p.getDOB() << std::endl;
return 0;
}
The OP's code is not Java, but C++. There are a few hints: AFAIK Java does not use ::, and Java's string type is called String, not string. It's also worth noting that the concatenations on line 11 are not actually concatenations, as string literals in C++ are not of type std::string by default. That line may actually have been ripped out of a Java program.
To answer the question, although strictly speaking functions can only return one object at a time, the fact that std::tuple exists means that in practice, a function can return however many objects it wishes. http://en.cppreference.com/w/cpp/utility/tuple#Example