-
August 29th, 2021, 05:51 AM
#1
Basic class advice needed
The CPP language has changed and I am now studying an online book, but I have some basic questions on this simple class.
Code:
#include <iostream>
using namespace std;
class vmSize {
public:
vmSize() = default;
vmSize(int nWidth, int nHeight) { m_nWidth = nWidth, m_nHeight = nHeight; }
int GetWidth() const { return m_nWidth; }
int GetHeight() const { return m_nHeight; }
private:
int m_nWidth {}, m_nHeight {};
};
int main()
{
vmSize mySize;
cout << "width " << mySize.GetWidth() << "\n";
cout << "height " << mySize.GetHeight() << "\n";
vmSize anotherSize{ 10, 5 };
cout << "\nwidth " << anotherSize.GetWidth() << "\n";
cout << "height " << anotherSize.GetHeight() << "\n";
return 0;
}
1. With the default constructor, my program shows both width and height initialized to 0, am I right in thinking that these two variables will always be 0 irrespective of what compiler I use? I did not place a 0 within the curly braces of the private data members of the class?
2. I could make the default c'tor redundant by doing this:
Code:
vmSize(int nWidth = 0, int nHeight = 0) { m_nWidth = nWidth, m_nHeight = nHeight; }
Is this good programming practice to do this, or is it better to have a default c'tor and not make this change?
3. Looking at this simple code I can't help but ask, What if I wanted two other c'tor's that take two doubles and another that takes two floats for width and height. Would I need to write these two other c'tor's, or is there a way of writting ONE c'tor that could take int, double or float and not complain. Hope this does not sound silly! Thinking of code reduction?
Thank you.
What the mind can conceive it can achieve.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|