Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the c++ quivalant of the following c# class file, separated into header file and cpp file:

class hello
{
     private string _fname, _lname;
     public hello(string fname, string lname)
     {
           setName(fname, lname);
     }
     public setName(string fname, string lname)
     {
          _fname = fname;
          _lname = lname;
     }
     public getName()
     { 
          return _fname + " " + _lname;
     }
}
Posted
Updated 6-Jul-10 6:10am
v2

A good first stab would be:

// Class definition in name.h

#include <string>

class name
{
    public:
        name( const std::string &first, const std::string &last );
        std::string full_name() const;

    private:
        std::string first_name_, last_name_;
};

// Class implementation in name.cpp

name::name( const std::string &first, const std::string &last ) :
    first_name_( first ), last_name_( last )
{
}

std::string name::full_name() const
{
    return first_name_ + ' ' + last_name_;
}


Two things to consider here...

- why did I get rid of the setter method you had in your C# implementation? (another way to think about it is why should value classes be constant?)

- why are the functions non-virtual? (I think that's the equivalent of final in C# and Java, might be wrong though, don't use C#)

There are good reasons for both of them, which looking at your C# class makes me think they might not be idiomatic in C#.

Cheers,

Ash
 
Share this answer
 
v2
Comments
William Dyson 6-Jul-10 14:30pm    
it works but why is it not within the brackets? I don't find c++ consistent.
name::name( const std::string &first, const std::string &last ) :
first_name_( first ), last_name_( last )
{
}
Aescleal 6-Jul-10 14:50pm    
The stuff after the semi-colon is a constructor initialiser list. They're there so you can initialise contained objects before entering the body of the constructor.
William Dyson 6-Jul-10 16:11pm    
Reason for my vote of 5
It was the believe it or not the first example I have found after about 2 days to work!
Emilio Garavaglia 7-Jul-10 2:12am    
of course, you can also do
name::name( const std::string &first, const std::string &last )
{
first_name_ = first; last_name_ = last;
}

but it has a different semantic: here the strings are constructed with a default value and then subsequently assigned.
In the above case, they are constructed directly as copy.

What does it make, depends on std::string implementation of the assignment operator and of the copy constructor.
First of all above c# code will never compile, because you are not specified the return type of setName and getName method.
modified version of C# code
C#
class hello
{
    private string _fname, _lname;
    public hello(string fname, string lname)
    {
        setName(fname, lname);
    }
    public void setName(string fname, string lname)
    {
        _fname = fname;
        _lname = lname;
    }
    public string getName()
    {
        return _fname + " " + _lname;
    }
}


C#
//hello.h - This class provides declaration of class Hello
#include <string>
class Hello
{
public:
    Hello(void);//default constructor
    Hello( const std::string& strFirstName,  const std::string& strLastName);
    void SetName( const std::string& strFirstName,  const std::string& strLastName);
    std::string GetName();
private:
    std::string m_strFirstName;
    std::string m_strLastName;
};


C#
//hello.cpp - This class provides implementation of class Hello
#include "Hello.h"
Hello::Hello(void)
{
}
Hello::Hello( const std::string& strFirstName,  const std::string& strLastName):
                m_strFirstName(strFirstName), m_strLastName( strLastName )
{

}
void Hello::SetName( const std::string& strFirstName,  const std::string& strLastName)
{
    m_strFirstName = strFirstName;
    m_strLastName = strLastName;
}
std::string Hello::GetName()
{
    return m_strFirstName + " " + m_strLastName;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900