C++ variable type [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a function which takes two variables:
call((void**) &var, float);
call((void**) &var, int);
call((void**) &var, string);
Now I want to create a function to call that special function:
function start_call(/*what to put here?*/)
{
call((void**) &var1, float);
call((void**) &var2, int);
call((void**) &var3, string);
//code
}
So, what to put in the "what to put here?" place, in order for the function to accep any variable?
I tried my berst to explain it...

You could use a template:
template <class T> start_call(T in);

Related

How to do I implement oop with a number of questions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have about 40 questions. (all the answers to the questions are of either int or float data type)
I will display the question and ask the user to input his/her answer.
Then I will check if the answer is correct or not.
I need to implement this program in object oriented programming.
I thought about doing it this way,questions by questions..but is there another way of doing this? Please help me..
The code below is sort of a pseudocode . I use c++ .
class workout{
private:
float variables...;
public:
workout();
workout(float variables...);
void answers();
int Display();
~workout();
};
...
void workout::answers(){
declare variables;
display question1;
input answer1
check if correct
display question2;
input answer2
check if correct
display question3;
..and so on..
}
workout::~workout(){
}
int main(){
...
return 0;
}
Well you could have a Question class, and then go from there. Here's an example.
class Question
{
public:
std::string question
float answer;
Question(std::string _question, float _answer)
: question(_question), answer(_answer) {}
void Display() {}
bool Check(float input) {}
};
And then maybe create an array of questions?

how to declare a generic class in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My main question is: can I put the generic type before a class definition?
Like, can I do something like this:
generic class Classname (ParameterType parameter)
{
cout << "Hello world";
}
Templates are the way to do generics in C++. You can write it like this:
template<class ItemType> class ClassName
{
public:
ClassName(const ItemType& newdata) : data(newdata) {}
private:
ItemType data;
};
Later on in main:
ClassName<int> data1(1);
ClassName<char> data2('A');

How to override the setter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an private property in my class and I want to add some code on its setter, how can I accomplish that?
I might do that using method but I don't want to just to avoid using property.
private:
float damage;
You can replace your plain old data type with a class type to do what you want:
class Damage
{
public:
Damage& operator=(float x) { value = x; /* add code here*/ }
operator float() const { return value; }
private:
float value;
};
Then just replace your float with Damage and you can make it behave how you want.

Coding convention of using a string literal [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I know there is no specific rule of how to use or declare a string literal, like for example in my class, I want to use "MyName" string literal, and its the only class that will use it, for example,
// CFoo.h
class CFoo
{
public:
CFoo();
~CFoo();
void printString();
}
// CFoo.cpp
CFoo::CFoo()
{
}
CFoo::~CFoo()
{
}
void CFoo::printString()
{
std::cout << "MyName" << std::endl;
}
Now I want that "MyName" will have a descriptive name placeholder, like NameLiter or something like that. Should I use define preprocessor, or declare it as global in cpp as const std::string? Or should I make a private member variable and initialize it in the ctor initializer list?
Thanks!
Making it a private static const char* in CFoo would satisfy your requirements.

How to use variable type Struct inside class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to build a videogame with SDL for my end year proyect. However I'm somewhat lost about how and when to use classes.
T tried to include a variable type struct inside a class but I can't manage to do it, maybe Position should be a class instead of a struct? Here's my code:
struct Position{
int x,y;
};
class Object{
private:
Position pos;
Position speed;
int tipe;
public:
Objeto(int,int);
Objeto();
~Objeto(); // DESTROY
};
When I try to do this I get error: 'class Object' has no member named 'x' How can I include the struct in the object?
From your error, I think you are trying to use
Objeto.x
instead of
Objeto.pos.x