This may seem a bit confusing. Basically, I have a function
CCard newCard() { std::stringstream ssPIN, ssBN; int picker1, picker2; int pin, bankNum; for( int loop = 0; loop < 5; ++loop ) { picker1 = rand() % 8 + 1; picker2 = rand() % 8 + 1; ssPIN << picker1; ssBN << picker2; } ssPIN >> pin; ssBN >> bankNum; CCard card( pin, bankNum ); return card; }
which creates a new CCard variable and returns it to the caller
CCard card = newCard();
My teacher advised me that this is a violation of OOP principles and should be placed in class. He told me to use this method as a constructor. What I've done:
CCard::CCard() { m_Sperre = false; m_Guthaben = rand() % 1000; ssPIN >> m_Geheimzahl; ssBN >> m_Nummer; }
All variables with m_ are member variables. However, the constructor works when I initialize the map normally
CCard card();
at the beginning of the program. However, I also have a function that should create a new map and return it to the user, this function is now broken.
Original command: card = newCard(); is no longer available, and card = new CCard(); does not work. What other options do I have? I feel that using the constructor will not work, and that I probably should just create a newCard class method, but I want to see how it is generally possible to do as the teacher wanted.
This creates a lot of headaches for me. I told the teacher that this is a stupid idea, and not everything should be classified in OOP. Since then, he told me that Java or C # does not allow code to be used outside of classes, which sounds a bit unbelievable. Not sure if you can do this in C ++, especially when there are boilerplate functions or general algorithms. Is it true that this would be bad code for OOP in C ++ if I hadn't injected it into a class?
EDIT:
I want to thank everyone for their helpful answers! However, I believe that my wording of the question is a little screwed up, and I think that people do not understand what I'm looking for.
I do not want to initialize another element of type CCard. I want to intitialize
CCard card once, and then give the card new values through the constructor, because this is what the teacher told me to do. I do not want to create a new CCard object, just use the same variable with the new values again.
This is why I said that this probably won't work with the constructor. Therefore, I have a function that should take the initialized variable card , and then call the constructor again ("What?" Is what I told the teacher), and then give it new values.
Code example:
void foo() { CCard card; card; }
This is a topical issue. Sorry if I confused everyone xD