Help with a Classes/Inheritance and I/O streams project!

Mar 29, 2011 at 9:00pm
I'm working on a project for class that I know very little about... Once we got to "classes" I became very confused and I'm having difficulties grasping all the concepts. There are so many classes and functions etc. that I literally have no idea where to even begin... I'm an extreme beginner so if you could give some explanation to your coding etc. I would appreciate it... The code I have thus far isnt really worth showing... its mostly just things like
1
2
3
class MathProblem 
{  
}
and some of the other classes/functions..but not much filled in etc..it seems like the project is basically just a way to be able to do simple math... but I'm really not good at this C++ stuff and have fallen pretty far behind now... .... Here is the assignment...


Problem:

1. Create a MathProblem class that holds fields such as
• Three integers: the first operand, the second operand and the user’s answer;
• One character field that stores an operator such as ‘+’, ‘-‘, ‘*’ or ‘/’;
• An integer field that shows if the answer is correct, such as 1 expresses correct, 0 wrong;
• A function called setProblem(…) that sets the MathProblem with 3 arguments ( 2 operands and an operator);
• A function getAnswer() shows the problem, accepts the answer and set the correctness by computing and comparing;
• A function showProblem() shows the problem;
• A function showOther() shows the user answer, the correct answer and the correctness of the user answer by computing and comparing;
• Include any functions you feel are useful and appropriate.
2. Create a new class MathProblemWithAnswer that inherits MathProblem and holds new fields
• An integer for the correct answer;
• An overridden function called setProblem(…) sets the inherited fields with 3 arguments, computes and sets the correct answer; and
• An overridden function getAnswer() shows the problem and accepts the answer and sets the correctness by comparing with the correct answer stored.
• An overridden function showOther() shows the user answer, the correct answer and the correctness of the user answer by comparing.
3. Write a main function to create one instance of MathProblem and one instance of MathProblemWithAnswer, use getAnswer() to get answers from a user, and use show…() to show the user’s answer, correct answer and its correctness.
4. Bonus: (10%) Write the two instances to a file and then read them back to show them again.



Any help/guidance/code would be appreciated.. thank you!
Mar 29, 2011 at 9:05pm
There is a tutorial on this site. See if it helps at all.
http://cplusplus.com/doc/tutorial/classes
Mar 29, 2011 at 10:00pm
Well.. here's what I have thus far..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;

class MathProblem
{
private:
	int first; //first operand
		int second; //second operand
		int answer; //users answer
		char operand[20];  //character field to hold operator
public:
	void setProblem(int one,int two,int ans);
		void getAnswer(); 
	void showProblem();
	void showOther();


}






class MathProblemWithAnswer
{

private:
	int correct;
public:
	void getAnswer();
	void showOther();

}





void main()
{


	getch();
}

   




It seems like from the instructions that a user would input a math equation... such as "2+2=5"

Then my program should return..... a 0 (because the answer is wrong)....and the equation with the correct answer.... "2+2=4"


But I'm pretty confused in the coding aspect... obviously I understand how to add, subtract etc. just not how to put it into a running code...
Mar 29, 2011 at 10:25pm
I know this is an awful example... but I made this when I was bored one day... and its obviously similar to what I need for determining answers... except its based on one exact question... not on one that the user inputs... so again... that is why Im so confused.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


#include<iostream>
#include<conio.h>
using namespace std;


void main()

{
	int answer;


		cout<<"whats 2+2?"<<endl;

		cin>>answer;

		if (answer == 4)
cout<<answer<<"is correct"<<endl;

		else
			cout<<"sorry you are fucked in the head"<<endl;

		getch();


}



Mar 30, 2011 at 4:12am
Well considering that computers are though of as over-powered calculators, you might guess that c++ knows how to do math too!
1
2
3
4
5
6
7
8
int
	a = 2,
	b = 3;

int
	a_plus_b = a + b,
	a_minus_b = a - b,
	a_times_b = a * b; //etc... 


So rewrite it like
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using std::cout;
using std::endl;

int main() {
	int guess;
	cout << "what's 2+2?" << endl;
	cin >> guess;
	if( guess == 2+2 ) ... //correct
	else ... //incorrect
	return 0;
}
Topic archived. No new replies allowed.