简体   繁体   中英

C++ Class Constructor Data Passing

I have a quiz that's due soon and I'm having a tough time with one question in particular. The question is:

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"

This is my code;

class SimpleMath
{

public:

    SimpleMath(int var1, int var2);

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

private:
   int m_value1;
   int m_value2;

};

I was wondering if someone can verify that it is or is not correct. If it's not I'd appreciate any help you can offer. Thank you in advance.

Please implement the constructor in your example. Below example demonstrates how to do so:

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;

};

it's correct but you forgot to show how you store value, you juste define function here . . .

SimpleMath(int var1, int var2)
{
 //somme stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM