Total newb here, so go easy :) I've googled and can't really seem to find an elegant solution to this. I'm doing some coding to learn a few concepts.
I have a class - called 'sally' which has a constructor which is overloaded twice i.e. my sally.cpp is:
#include "sally.h"
#include <iostream>
#include <string>
using namespace std;
sally::sally()
{
}
sally::sally(int x)
{
inputvarint = x;
cout << "you have an int of value: " << inputvarint << endl;
}
sally::sally(string y)
{
inputstring = y;
cout << "you have a string that is: " << inputstring << endl;
}
in my main.cpp if I create the object sally myobj;
and then call the function via the object, i.e. sally.myobj(55)
I get the correct constructor firing off and telling me I have an INT of value whatever, or if I go sally.myobj("johhny")
I get the other constructor going and telling me I have a string that says "johnny", so that part I get fine.
What I would like to know is, is there an elegant way I can use CIN to take input from a user, and either pass it straight to the object - without using a variable ( i've tried and can't get it to work ).
Basically, I'd like the user to be able to enter either a string, or an int and use that in the sally.myobj(); call, and let the constructor(s) do the work of figuring out what type of data it is.
things like:
auto x = 0;
and then cin >> x
don't work as the initial declaration was an int, so it stays like that.
Is there a way to declare a variable without a type and assign it a type based on the input from CIN?
Apologies if the answer to this is blindingly obvious, but i'm at the very beginning of my journey ( I'm up to about tutorial 40 of this guy: http://www.youtube.com/watch?v=tvC1WCdV1XU ) and also reading C++ Primer by Stanley Lippman ( 5th edition ) and The Object-Oriented Thought Process by Matt Weisfield. They are my only resources at present ( that and google ).
Thanks in advance
Seb
EDIT:
After seeing some of the responses below, it helped me figure out what can/can't be done and I've tackled it this way using a parsing function which utilises some Regex, and if string content is deemed to be INT then converts it and places it into a new variable and off to the constructor:
Sally.cpp:
#include "sally.h"
#include <iostream>
#include <string>
#include <regex>
using namespace std;
sally::sally()
{
}
sally::sally(int x)
{
inputvarint = x;
cout << "Constructor output: you have an int of value: " << inputvarint << endl;
}
sally::sally(string y)
{
inputstring = y;
cout << "Constructor Output: you have a string that is: " << inputstring << endl;
}
void sally::StringParser(string x){
regex e("^[0-9]+$"); //test if the string starts with ends with and has a whole lot of numbers in between, i.e. and INT of any length
bool match = regex_match(x, e);
if(match){
cout << "you have an int" << endl;
int newvar = atoi(x.c_str());
sally::sally(newvar);
}
else {sally::sally(x);}
}
then I simply sally myobj;
and myobj.StringParser(x);
whereby x is the input from CIN and the Parsing function takes care of the rest and passes it to the constructors.
I'm sure I could build this out a bit with a SWITCH statement with different REGEX's to handle floats etc and then create additional constructors. But For now, I seem to have found a solution.
Keen to see if there are any issues with my approach.