-1

I have a take home test that it work a large percentage of my grade. These 3 questions are on it and they are worth half the test. I was wondering if someone could kindly review my quiz and make sure they are correct for me. I’m a worrier and I want to make sure they are correct. Thank you in advance!

Implement the constructor for the class called "SimpleMath". The constructor takes two integer parameters; "var1" and "var2". The constructor is to store the value that was passed into "var1" into the private integer member variable "m_value1" and the value that was passed into "var2" into the private integer member variable "m_value2"

class SimpleMath
{

public:

    SimpleMath(int var1, int var2) : m_value1(var1), m_value2(var2) {};

    int getVar1() const
    {
        return m_value1;
    } 
    int getVar2() const
    {
        return m_value2;
    } 

private:
   int m_value1;
   int m_value2;

};

Implement the "Multiply" method for the "SimpleMath" class. This method does not require any parameters and returns an integer value. This method should multiply the values stored in the classes private integer member variables "m_value1" and "m_value2" the resulting value is returned. Assume that "m_value1" and "m_value2" were loaded inside the classes constructor.

class SimpleMath
{

public:

    SimpleMath(int var1, int var2);

    int Mutiply= m_value1* m_value2;

private:
    int m_value1;
    int m_value2;
};

Write a class definition called "SimpleMath" that has a constructor that takes two integers "var1" and "var2". It has four public methods that take no parameters and return an integer value; "Add","Subract","Divide" and "Multiply". The class has two private member variables of type integer; "m_value1" and "m_value2".

class SimpleMath
{
    SimpleMath(int var1, int var2);

public:
    int Add;
    int Subract;
    int Divide; 
    int Multiply;

private:
    int m_value1;
    int m_value2;
};
2
  • 3
    I suggest writing a main function that exercises the class you implemented as a way to check your homework.
    – doug
    Nov 8, 2015 at 21:35
  • 1
    int Mutiply= m_value1* m_value2; does not define a method. Check this out: en.cppreference.com/w/cpp/language/member_functions.
    – R Sahu
    Nov 8, 2015 at 21:42

1 Answer 1

0

Am I to assume you have no prior programming experience?

At any rate, the first part looks ok. What the : denotes is the initializer list. It can, and is, correctly used to initialize a class's members.

Second part, you got it dead wrong I am afraid.

First of, you've not implemented the method Multiply() there, you've just declared a variable.

Instead do this:

int Multiply()
{
   int sum = m_value1 * m_value2;
   return sum;
}

For brevity's sake, you can also do this:

int Multiply()
{
    return m_value1 * m_value2;
}

Does the same thing.

Also note that the answer from question 1 has not carried over to question 2, ie the constructor is incomplete. It won't compile.

As for question 3, it's just question 2 all over again, except you've also got to implemented the three remaining arithmetic functions. Suffice it to say, I am sure you can figure it out.

1
  • Thank you, and no, I do not have much experience. This is my first class and its starting to go fast and get pretty complicated. I wish i could show you my updated code to ask a couple more questions but i don't want to just copy and paste it here. i think it'll get all jumbled up. Nov 9, 2015 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.