CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Question 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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Basic class advice needed

    1. Yes. {} is the default initializer. The default value is specified for a type. For a int/double et al it is 0.

    2. You constructor should be :

    Code:
    vmSize(int nWidth, int nHeight) : m_nWidth(nWidth), m_nHeight(nHeight) {}
    I prefer a separate default constructor.

    Code:
    vmSize() {}
    where the member variables are value initialized.

    3). You'd use a templated class:

    Code:
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    class vmSize {
    public:
    	vmSize() {};
    	vmSize(T nWidth, T nHeight) : m_nWidth(nWidth), m_nHeight(nHeight) {}
    
    	T GetWidth() const { return m_nWidth; }
    	T GetHeight() const { return m_nHeight; }
    
    private:
    	T m_nWidth {}, m_nHeight {};
    };
    
    int main()
    {
    	vmSize<int> mySize;
    
    	cout << "width " << mySize.GetWidth() << "\n";
    	cout << "height " << mySize.GetHeight() << "\n";
    
    	vmSize<int> intSize {10, 5};
    
    	cout << "\nwidth " << intSize.GetWidth() << "\n";
    	cout << "height " << intSize.GetHeight() << "\n";
    
    	vmSize<double> doubleSize {11.34, 13.56};
    
    	cout << "\nwidth " << doubleSize.GetWidth() << "\n";
    	cout << "height " << doubleSize.GetHeight() << "\n";
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Basic class advice needed

    Many thanks 2kaud, this has really helped.
    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
  •  





Click Here to Expand Forum to Full Width

Featured