Code:
class Date
{
// insert usual datestuff here
public:
const char* AsText() const;
void Change( int, int, int );
};
class Person
{
private:
Date Birthdate;
public:
const Date& GetBirthdate() const
{ return Birthdate; }
void SetBirthdate( const Date& d )
{ Birthdate = d; }
};
There is a slight problem with your code. Since this is a birthday, you don't want it changing. Face it, people's birthday's never change. As such:
Code:
class Date
{ private int d,m,yr;
public void Date( int day, int month, int year )
{ d = day; m = month; y = year; }
}
class BirthDate extends Date
{ public Date GetBirthDate( )
{ return new Date( this.d, this.m, this.y );
}
public void ShowBirthDate( )
{ System.out.println( "" + this.d + "/" + this.m + "/" + this.y );
}
}
class Person
{ private BirthDate b;
//more personal information
public Person( int day, int month, int year )
{ b = new BirthDate( day, month, year );
}
public ShowBirthday( )
{ b.ShowBirthDate( ); }
}
class MyProggie
{ public static void main( String [] args )
{ Person p = new Person( 4, 11, 2001 );
p.ShowBirthDay( );
}
}
That is how the class should be designed. More like that anyway. Yours was poor class design, in that, you are extracting information from the class in a way you shouldn't. A class should be self contained. Anything that you need from the class, it should be able to provide you with access functions.
Yes, it is more work, more lines of code, and takes longer to write;
but, if you want your classes working the way they should, then that (above) is how you'd go about it.
Granted, I just threw that together, so there are probably ways it could be done better. (For example, I should have made the functions return a string, rather than have called println inside it. I was just hacking this together real quick, thus the result.)
Additionally, my Date class could have been done better. For example, I could have used a 'final' setDate class in Birthday, to prevent the date from being changed, something along that line.
Quzah.