I need help figuring out how to do this. This is what my professor is asking us
ID: 3835745 • Letter: I
Question
I need help figuring out how to do this. This is what my professor is asking us to do:
STEP 2
1. Create a UML class diagram for a new class demonstrating composition using Step 1.
2. Extend Step 1 to include Operator Overloads in
o Insertion ( Required )
o Extraction ( Required )
o Relational Operators ( Minimum 2 )
o Math Operators( Minimum 2 )
3. Code and Test. Turn in: UML Class Diagram(s), C++ Code for all classes and test driver(s).
Turn in: UML Class Diagram(s), C++ Code for all classes and test driver(s).
PLEASE HELP
This is step 1 and the code I have based on it. I have absolutely no idea what to do here.
STEP 1
1. Create a UML class diagram for a class you create.
2. Code your class.
3. Test your class.
Turn in: UML Class Diagram, C++ Code for class and test driver.
#include <iostream>
#include <string>
using namespace std;
class quiz
{
private:
int amountOfQuestions;
char answer[50];
string question[50];
int rightWrong;
string right;
string wrong;
public:
quiz()
{
rightWrong = 0;
}
void set_rightWrong_answers(string rightt, string wrongg)
{
right = rightt;
wrong = wrongg;
}
void set_amountOfQuestions(int amountOfQuestionss)
{
amountOfQuestions = amountOfQuestionss;
}
void set_q_a(int qn, string q, char ca)
{
question[qn - 1] = q;
answer[qn - 1] = ca;
}
void run_quiz()
{
char ans;
for (int i = 0; i < amountOfQuestions; i++)
{
cout << question[i] << ": ";
cin >> ans;
if (ans == answer[i])
{
cout << "Correct! Great Job!" << endl;
rightWrong = rightWrong + 1;
}
else
{
cout << "Incorrect. Better luck next time." << endl;
rightWrong = rightWrong - 1;
}
system("pause");
system("cls");
}
if (rightWrong >= 0)
{
cout << right << endl;
cout << "YOUR GRAND SCORE: " << rightWrong << endl;
}
else if (rightWrong < 0)
{
cout << wrong << endl;
cout << "YOUR GRAND SCORE: " << rightWrong << endl;
}
else
{
cout << "Try inputting one of the answers provided." << endl;
}
system("pause");
}
};
int main() {
quiz music_theory;
music_theory.set_amountOfQuestions(10);
music_theory.set_rightWrong_answers("You know your theory!", "You might want to work on memorizing some theory.");
music_theory.set_q_a(1, "What note is the 3rd line from the bottom of the treble cleff? [a],[b],[c],[d],[e],[f], or [g]", 'b');
music_theory.set_q_a(2, "What note is the 4th space from the bottom? [a],[b],[c],[d],[e],[f], or [g]", 'e');
music_theory.set_q_a(3, "What note is the 2nd line from the top? [a],[b],[c],[d],[e],[f], or [g]", 'd');
music_theory.set_q_a(4, "What note is the 5th line from the bottom? [a],[b],[c],[d],[e],[f], or [g]", 'e');
music_theory.set_q_a(5, "What note is the 2nd space from the top? [a],[b],[c],[d],[e],[f], or [g]", 'a');
music_theory.set_q_a(6, "What note is the 5th line from the top? [a],[b],[c],[d],[e],[f], or [g]", 'g');
music_theory.set_q_a(7, "What note is the 4th space from the top? [a],[b],[c],[d],[e],[f], or [g]", 'f');
music_theory.set_q_a(8, "What note is the 1st space from the bottom? [a],[b],[c],[d],[e],[f], or [g]", 'f');
music_theory.set_q_a(9, "What note is the 3rd line from the top? [a],[b],[c],[d],[e],[f], or [g]", 'b');
music_theory.set_q_a(10, "What note is the 2nd space from the bottom? [a],[b],[c],[d],[e],[f], or [g]", 'a');
music_theory.run_quiz();
system("pause");
return 0;
}
Explanation / Answer
Hey there I am Primehero, how are you? I can see that you have a class here and its not that good, you'll run into problems if you keep extending the code farther. So I'd like to know if you created this class? And more info about this class(The question you created this class for as answer). Comment back all the details and we'll code a better class and we'll do the UML diagram first and other steps too .... . I really need the main question you had for creating this class.
Edit 1:
Question.h
====
/*
* Question.h
*
* Created on: 08-May-2017
* Author: Rj
*/
#ifndef QUESTION_H_
#define QUESTION_H_
#include <string>
using namespace std;
class Question
{
static const int NUM_OPTIONS = 4;
private:
string _question;
string *_options;
int _answer;
public:
Question();
virtual ~Question();
// GETTERS
string get_question() const;
string* get_options() const;
int get_answer() const;
// SETTERS
void set_question(string);
void set_options(string);
void set_options(string, string);
void set_options(string, string, string);
void set_options(string, string, string, string);
void set_answer(int);
// OPERATOR OVERLOADING
friend istream & operator >> (istream &, Question &);
friend ostream & operator << (ostream &, Question &);
};
#endif /* QUESTION_H_ */
Question.cpp
====
/*
* Question.cpp
*
* Created on: 08-May-2017
* Author: Rj
*/
#include "Question.h"
Question::Question()
: _question{}, _answer{}
{
_options = new string[NUM_OPTIONS]{};
for (int i = 0; i < NUM_OPTIONS; ++i)
{
_options[i] = "None";
}
}
Question::~Question()
{}
// GETTERS
string Question::get_question() const
{
return this->_question;
}
string* Question::get_options() const
{
return _options;
}
int Question::get_answer() const
{
return this->_answer;
}
// SETTERS
void Question::set_question(string q)
{
this->_question = q;
}
void Question::set_options(string a)
{
this->_options[0] = a;
}
void Question::set_options(string a, string b)
{
this->_options[0] = a;
this->_options[1] = b;
}
void Question::set_options(string a, string b, string c)
{
this->_options[0] = a;
this->_options[1] = b;
this->_options[2] = c;
}
void Question::set_options(string a, string b, string c, string d)
{
this->_options[0] = a;
this->_options[1] = b;
this->_options[2] = c;
this->_options[3] = d;
}
void Question::set_answer(int a)
{
this->_answer = a;
}
istream & operator >> (istream & is, Question & q)
{
// Get question.
getline(is, q._question);
// Get four options
getline(is, q._options[0]);
getline(is, q._options[1]);
getline(is, q._options[2]);
getline(is, q._options[3]);
// Get the answer index.
string ans{};
getline(is, ans);
q._answer = stoi(ans);
return is;
}
ostream & operator << (ostream & os, Question & q)
{
os << q._question << endl
<< "Option 1: " << q._options[0] << endl
<< "Option 2: " << q._options[1] << endl
<< "Option 3: " << q._options[2] << endl
<< "Option 4: " << q._options[3] << endl
<< "Answer: Option " << q._answer << endl;
return os;
}
Quiz.h
====
/*
* Quiz.h
*
* Created on: 08-May-2017
* Author: Rj
*/
#ifndef QUIZ_H_
#define QUIZ_H_
#include <vector>
#include <string>
#include "Question.h"
class Quiz
{
private:
std::vector<Question> _questions;
int _num_questions;
public:
Quiz();
virtual ~Quiz();
// GETTERS
Question get_one(int) const;
int get_num_questions() const;
void add_question(
string, string, string,
string, string, int );
};
#endif /* QUIZ_H_ */
Quiz.cpp
====
/*
* Quiz.cpp
*
* Created on: 08-May-2017
* Author: Rj
*/
#include "Quiz.h"
Quiz::Quiz()
: _questions{}, _num_questions{}
{}
Quiz::~Quiz()
{}
Question Quiz::get_one(int indx) const
{
return this->_questions.at(indx);
}
int Quiz::get_num_questions() const
{
return this->_num_questions;
}
void Quiz::add_question(
string q, string a, string b,
string c, string d, int i )
{
Question *q2 = new Question{};
q2->set_question(q);
q2->set_options(a, b, c, d);
q2->set_answer(i);
_questions[_num_questions] = *q2;
++_num_questions;
}
This is a minimalist quiz I made right now. I'll add the overloading operators soon, try figuring them out they are not that hard now. I'll get back to you soon.
Edit 2:
Hey there I have added overloading operator. I have done unit testing. I added overloading operator to just the Question class. You can add it to Quiz class, if you'd like to. But I didn't think it was appropriate and the question quite long too. So if you have more queries you can make your question a bit smaller now, since most of it is answered and create another question. Have a great day.
Cheers,
Primehero
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.