Java:
class Person {
private String firstName;
private String lastName;
public String getDisplayName() {
return firstName + " " + lastName;
}
}
C#
class Person
{
private string FirstName;
private string LastName;
public String GetDisplayName() {
return FirstName + " " + LastName;
}
}
Haskell
data Person = Person String String
displayName :: Person -> String
displayName (Person firstName lastName) = firstName ++ " " ++ lastName
Ruby
class Person
attr_reader :first_name, :last_name
def initialize (first_name, last_name)
@first_name = first_name
@last_name = last_name
end
def display_name
return @first_name + " " + @last_name
end
end
PHP
class Person {
public $firstName;
public $lastName;
function getDisplayName() {
return "$this->firstName $this->lastName";
}
}
C++
class person {
private:
std::string first_name;
std::string last_name;
public:
std::string get_display_name() {
return first_name + " " + last_name;
}
}
C++ (have to do that because of header files, as the C++ compiler can not just infer the interface from the c source file...)
person.h
class person {
private:
std::string first_name;
std::string last_name;
public:
std::string get_display_name();
}
person.cpp
std::string person::get_display_name() {
return first_name + " " + last_name;
}
C++ templates
template <typename T>
struct point {
T x;
T y;
std::string to_string() {
return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
}
}
C++ templates (the less sane version, note that you could put the separate definition in a *.cpp
file but now you need to instantiate every possible use of the class in there already...)
template <typename T>
class point {
T x;
T y;
std::string to_string();
};
template <typename T>
std::string point<T>::to_string() {
return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
}