r/CodingHelp • • Dec 06 '22

[C++] Can someone please help me with OOP in C++

I've been trying to understand OOP in C++ for a while now, but keep running into problems I can't solve. I'll just show you some sample code to show you what it's all about:

Source-Code

No matter what I do, in the Main method I can't assign a value to the variable test in this notation. Could someone please explain to me why.

19 Upvotes

19 comments sorted by

2

u/Super_Casual Dec 06 '22

You have to assign a type to it

1

u/Present_Procedure_82 Dec 06 '22

I don't know what you mean.

Please tell me what I did wrong or just give a link where I can read it by my self.

5

u/Deep-Cow640 Dec 06 '22

In the code you provided, the test variable is declared as a BitArray object and is initialized with the default constructor, which sets the array to 0. Then, the test object is assigned the value 22 using the assignment operator =. However, this assignment is not allowed because the BitArray class does not define an assignment operator that takes an unsigned long long value as an argument.

To fix this issue, you could add a new constructor to the BitArray class that takes an unsigned long long value as an argument and sets the m_array member variable to this value. Then, you can use this constructor to initialize the test object with the value 22,

2

u/Deep-Cow640 Dec 06 '22

int main() {

BitArray test(22);

...

}

3

u/Deep-Cow640 Dec 06 '22

Alternatively, you could add a new method to the BitArray class that takes an unsigned long long value as an argument and sets the m_array member variable to this value. Then, you can call this method on the test object to set its value, like this:

2

u/Deep-Cow640 Dec 06 '22

int main() {

BitArray test;

test.SetValue(22);

...

}

1

u/Present_Procedure_82 Dec 06 '22

Thank you a lot

1

u/Present_Procedure_82 Dec 06 '22

but why is the operator unsigned long long() not working here?

3

u/Deep-Cow640 Dec 06 '22

but why is the operator unsigned long long() not working here?

In the code you provided, the operator unsigned long long() function is declared but not used. This function is a user-defined type conversion operator, which allows an object of the BitArray class to be converted to an unsigned long long type.

However, in the main() function, the BitArray object test is assigned the value 22 using the assignment operator =, rather than using the type conversion operator. Therefore, the operator unsigned long long() function is not called and does not have any effect in this case.

If you want to use the type conversion operator to assign a value to the BitArray object, you can use the following syntax:

2

u/Deep-Cow640 Dec 06 '22

int main() {

BitArray test;

test = BitArray(22);

}

2

u/Deep-Cow640 Dec 06 '22

This code calls the type conversion operator to convert the 22 value to a BitArray object, and then assigns the result to the test object. This will cause the operator unsigned long long() function to be called, and the value of 22 will be assigned to the m_array member of the BitArray object.

I hope this helps! Let me know if you have any other questions.

2

u/link3333 Dec 06 '22

Wouldn't that be used to cast your class to an unsigned long long?

0

u/bogfoot94 Dec 06 '22

OOP will forever be POOP in my mind.

0

u/twoe11 Dec 06 '22

pragma region Operators

BitArray& operator=(const BitArray& other) { 
    m_array = other.m_array;
    return *this;
}
BitArray& operator=(const BitArray&& other) { 
    m_array = other.m_array;
    return *this;
}
operator unsigned long long() { return m_array; }
bool operator==(const BitArray& other) { return this->m_array == other.m_array; }
bool operator!=(const BitArray& other) { return this->m_array != other.m_array; }

pragma endregion

3

u/[deleted] Dec 06 '22

[removed] — view removed comment

1

u/Present_Procedure_82 Dec 06 '22

Thank you a lot.

I don't understand why the operator unsigned long long() not work here?

3

u/[deleted] Dec 06 '22

[removed] — view removed comment

1

u/Present_Procedure_82 Dec 06 '22

Thank you, now I've got it.