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

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?

Related

Passing data between variables inside a class in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I don't fully understand nor did i find anything online about this topic but this code(cut down so it wont be 500 lines of code):
class Character{
public:
int MaxHP;
int currentHP;
currentHP = MaxHP;
int getHP()
{
return CurrentHP;
}
Character(int h){
maxHP=h
};
~Character(){};
};
int main()
{
Character warrior(300)
cout<<getHP();
return 0;
};
maxHP = 300
CurrentHP = -875000
now trying to acces the value of currentHP i get -87878....
My question is what is the problem?
If you wish to initialize a private variable it's better done in a constructor:
class Character{
private:
int MaxHP;
int currentHP;
public:
Character() : MaxHP(0), currentHP(MaxHP){}
}

C pointers and objects as part of different classes [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 4 years ago.
Improve this question
I have relatively less experience with C++ coding, and I'm working on a project that requires me to use classes, pointers and objects in C++. I'm facing some basic problems while practising coding.
This is what part of my .cpp file looks like:
A::A(int x, int y):CD()
{
//some code
}
B::B(int z):CD()
{
//some code
}
If I need to pass values from A to B, how would I be able to do it in such a scenario? Could anyone please clarify and help me.
Edit: According to the code I have, CD is also defined as a class like A and B.
Here A is a class and B is another class right,
Let's take a simple addition example
A(int x,int y)
{
b=new B(x+y);//let be a reference of class B
//this will call B's constructor
}
B(int z)
{
this.z=z;//lets's assume that z is a variable in B class
}

How to use classes 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 6 years ago.
Improve this question
I can't see to find the problem in my code. Nor the solution on the internet. I can see that i can create the code in a different way but i need to know how to work with it written like this:
class Triunghi{
int l1=0;//latura1
int l2=0;//latura2
int ba=0;//baza
int p=0;//perimetru
public:
Triunghi(){}
Triunghi(int a):l1(a){}
Triunghi(int a,int b):l1(a),l2(b){}
Triunghi(int a,int b,int c):l1(a),l2(b),ba(c){}
Triunghi(int a,int b,int c,char *msg):l1(a),l2(b),ba(c){
cout<<msg<<""<<l1<<l2<<ba<<endl;
}
Triunghi(Triunghi &A){
l1=A.l1;
l2=A.l2;
ba=A.ba;
}
~Triunghi(){
}
int Perimetru()
{
p=l1+l2+ba;
return (p);
}
};
It works fine there are no errors but i cant seem to give value to l1, l2 and ba, to use them in function 'Perimetru'.
This is how my main looks.
int main()
{
Triunghi tri;
Triunghi(1,2,3);
tri.Perimetru();
return 0;
}
How to make it work?
Triunghi tri;
Triunghi(1,2,3);
This doesn't do what you think it does. tri is created with default constructor, so all of your members are set with their in-class default values, meaning 0. Then, you create temporary object Triunghi, and you don't even use it. What you might wanted to do instead is:
Triunghi tri(1,2,3);
Then Perimetru() returns 6 as expected.

PAWN to C++ global variable [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 7 years ago.
Improve this question
How do i write it in c++ ??
// in PAWN ( Procedural Language )
enum e_player_data
{
id, // Integer
username[24] // string
};
new PlayerData[50][e_player_data];
/*
so i can access it like PlayerData[1][id] = 1; or PlayerData[1][username] = "Firstname_Lastname";
*/
Can someone code it to c++ for me?
You can use a std::map:
typedef std::map<int,std::string> Player;
Player p;
// add a player:
int id = 0;
p[id] = "Max Power";
As tobi303 wrote in the comment, you can use a std::map to accomplish this.
struct e_player_data
{
int id,
std::string username
};
std::map<int,e_player_data> PlayerData;
PlayerData[1].id = 1;
PlayerData[2].username = "Firstname_Lastname";

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.