How to override the setter? [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 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.

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');

C++ / how to return best time result? [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 6 years ago.
Improve this question
In the TimeResult class i want to display the best time result.
Lets say I have several time results. and want to return the lowest one.
Header:
class TimeResult : public SportData
{
double time;
public:
TimeResult(string _discipline,string _name,double _time);
virtual ~TimeResult();
virtual double getBestResult();
};
Source:
double TimeResult::getBestResult()
{
???????
}
You can use static member for best time and static function for accessing this member variable. Member variable should be updated every time you add better time.
(It should be usefull to have another static variable, just for knowing if you already set the bestTime variable - relavant for correct updating of bestTime variable)
Code example (header):
class TimeResult : public SportData {
double time;
static double bestTime;
static bool bestTimeWasSet;
public:
TimeResult(string _discipline, string _name, double _time);
virtual ~TimeResult();
static double getBestResult();
};
Code example (cpp):
double TimeResult::getBestResult() {
return TimeResult::bestTime;
}

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