Java to C++



1
2
3
4
5
6
7
8
9
10
11
12
13
14
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?

Thanks
Last edited on
In c++ you can only return 1 variable but there are ways around this by passing a variable via reference.
could you give me an example?
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).
Right, but is there a way to convert something like this to c++ or not?
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

void getnum(int &num1, int &num2);

int main()
{
	int sum, num1, num2;

	getnum(num1, num2);

	sum = num1 + num2;

	cout << "The sum is: " << sum << endl;

	return 0;
}

void getnum(int &num1, int &num2)
{
	cout << "Enter num 1: ";
	cin >> num1;
	cout << "Enter num 2: ";
	cin >> num2;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

string getDOB()
{
  int day = 22, month = 11, year = 2000;
	
  return to_string(day) + "-" + to_string(month) + "-" + to_string(year);
}

int main()
{
  cout << getDOB() << "\n\n";

  system("pause");
  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

-Albatross
Thanks for all the help,

I got my program working.

Cheers guys,
Topic archived. No new replies allowed.