C++ OOP Troubles



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2012 at 11:44, xxxxxxxx wrote:

    This is a generic C++ question. Not related to the SDK.

    I'm trying to force myself to stop putting everything in one file(a bad habit picked up from scripting). And learn how to construct OOP style programs. And I'm having a hard time with the constructors.
    I'm trying to use only declarations in my .h files. And not do any implementations in them at all. But I can't get my constructors to work that way.
    If I implement the constructor variables in the .h file it works. But it does not work when implemented in the .cpp file. And I don't know why this is happening.

    Example:

      
    .h file  
      
    #pragma once  
    #include<iostream>  
    #include<string>  
    using namespace std;  
      
    class Name  
    {  
    public:  
      string fname;  
      
      //Name(string name);  //<--This is how I want to declare it   
      
      Name(string name)  
      {  
        fname = name;      //This works..But I don't want to implement any code like this in the .h file!!  
      }  
      
      string toString();   //Declare a method to return the string value in the constructor..This is fine  
      
    };  
      
    ..............  
      
      
    .cpp file  
      
    #include "name.h"  
      
      
    //This is how/where I want to implement the code..  
    //But it won't work this way...It returns a blank line  
      
    //Name::Name(string name)  
    //{  
    //  string fname = name;     //<--Why won't this code work here in the .cpp file?  
    //}  
      
      
    string Name::toString()  
    {  
      return fname;           //This works fine being implemented from inside of the .cpp file  
    }  
      
    ................  
      
      
    main.cpp file  
      
    #include<iostream>  
    #include<string>  
    #include "name.h"  
    using namespace std;  
      
    int main ()  
    {  
      Name myname("Scott");  
      cout<< myname.toString() <<endl;   //prints the name "Scott" from the constructor's parameter  
      
      system ("pause"); // wait for user to close the output window  
      return 0;  
    }
    

    Can anyone tell me how to correctly write the code in the .cpp file so it works from there. Instead of implementing the code in the .h file?
    I want to only declare the constructor in the .h file. And then let the .cpp file do the implementation(the work).

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2012 at 15:50, xxxxxxxx wrote:

    hm,

    i'm everything but an experienced c++ coder, but to my understanding you are not meant to
    place constructor code in the header file, but in the the cpp file. it is also considerd to be better 
    to use a getter/setter approach, rather than define your values as public.

    http://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/

    Date.h:

    #ifndef DATE_H
    #define DATE_H
     
    class Date
    {
    private:
        int m_nMonth;
        int m_nDay;
        int m_nYear;
     
        Date() { } // private default constructor
     
    public:
        Date(int nMonth, int nDay, int nYear);
     
        void SetDate(int nMonth, int nDay, int nYear);
     
        int GetMonth() { return m_nMonth; }
        int GetDay()  { return m_nDay; }
        int GetYear() { return m_nYear; }
    };
     
    #endif
    

    Date.cpp:

    #include "Date.h"
     
    // Date constructor
    Date::Date(int nMonth, int nDay, int nYear)
    {
        SetDate(nMonth, nDay, nYear);
    }
     
    // Date member function
    void Date::SetDate(int nMonth, int nDay, int nYear)
    {
        m_nMonth = nMonth;
        m_nDay = nDay;
        m_nYear = nYear;
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2012 at 17:16, xxxxxxxx wrote:

    I don't think that's correct.
    AFAIK. All definitions go in the header files. Even the constructor definitions that have parameters.
    The question is how to handle these constructors when they don't use Getters&Setters like the example I posted.

    After searching around for a long while. I found a web site where someone posted that constructors have to be declared fully in the header file. And implied that this is not a full declaration of the constructor in the case of my example:

    Name(string Fname, string Lname);
    

    They seem to be implying that when no get/set methods are used. This is the only way to declare it in the .h file:

        Name(string Fname, string Lname)  
      {  
          fname = Fname;  
          lname = Lname;  
      }
    

    Which is an awful lot like .cpp looking code to me. And something I wanted to avoid.

    This problem doesn't seem to be the case when using getter&setter methods.
    So I suppose this kind of code example is relatively rare. And won't happen very often?

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2012 at 18:36, xxxxxxxx wrote:

    hey,

    i took a look on ge_vector.h, as i thought it would be a good idea to see how it is done by maxon 
    (although i have been told, that maxons c++ style cannot be considered exemplary).

    found this, it is basicly the same as your example, but maybe it helps :

    ge_vector.h

    struct SVector
    {
    	SReal x, y, z;
      
    	SVector() : x(0.0f), y(0.0f), z(0.0f) { }
    	SVector(SReal in) : x(in), y(in), z(in) { }
    	SVector(SReal ix, SReal iy, SReal iz)  : x(ix), y(iy), z(iz) { }
    	SVector(_DONTCONSTRUCT v){}
    ...
    

    edit : haven't tested if this fomating is also useable on class constructors, but i assume it is.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/04/2012 at 21:00, xxxxxxxx wrote:

    Found the problem.

    In my .cpp file I wrote this:

    Name::Name(string name)  
    {  
      string fname = name;  
    }
    

    But What I needed to write was this:

    Name::Name(string name)  
    {  
      fname = name;  
    }
    

    Once that little error was fixed. I could then declare my constructor properly in the .h file like this and it worked:

    Name(string name);
    

    -Scott


Log in to reply