first i coded this in C# (just so i could get warmed up with the basic stuff i learned from constructor lessons)
Code:
using System;
namespace myProgram
{
class Person
{
private string name;
private int age;
public Person()
{
this.name = "Unknown";
this.age = 0;
}
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Age { get { return age; } set { this.age = value; } }
public static void Introduce(Person obj)
{
Console.WriteLine("Hi, my name is "+obj.name+", I am "+obj.age+" years old.");
}
}
class Program
{
public static void Main()
{
Person p1 = new Person();
Person p2 = new Person("Jack Wilson",40);
Person.Introduce(p1);
Person.Introduce(p2);
}
}
}
it works as expected. Then I decided to translate it to C++, and this is the best i could come up with:
Code:
#include <iostream>
using namespace std;
class Person
{
private:
string name;
int age;
public:
Person();
Person(string, int);
string getName(){ return name; }
int getAge(){ return age; }
static void Introduce(Person);
};
Person::Person()
{
this->name = "Nameless";
this->age = 0;
}
Person::Person(string name, int age)
{
this->name = name;
this->age = age;
}
void Person::Introduce(Person obj)
{
cout<<"Hello, my name is "<<obj.getName()<<", I am "<<obj.getAge()<<" years old."<<endl;
}
int main()
{
Person p1 = Person();
Person p2 = Person("Jack Wilson",40);
Person::Introduce(p1);
Person::Introduce(p2);
}
Which works, as well.
Well then, i just posted this so that maybe you veterans could clue me in on how to make it cleaner.