0

I have went to so many places online but can't find anything that explains overloading constructors well. I am justlooking for some guidance

I have to Overload the constuctor in my song class to allow a song to be created without providing a value for copiesSold.

class Song
{
    string name;
    string artist;
    int copiesSold;

    public Song(string name, string artist, int copiesSold)
    {
        this.name = name;
        this.artist = artist;
        this.copiesSold = copiesSold;
    }

    public Song()
    {
    }

    public string GetArtist()
    {
        return artist;
    }

    public string GetDetails()
    {
        return $"Name: {name} Artist: {artist} Copies Sold: {copiesSold},";
    }

    public string GetCertification()
    {
        if (copiesSold < 200000)
        {
            return null;
        }
        if (copiesSold < 400000)
        {
            return "Silver";
        }
        if (copiesSold < 600000)
        {
            return "Gold";
        }
        return "Platinum";

1 Answers1

1

The call of a constructor of the same class have to be performed by this keyword as in the example below

class Song
{
   public string name;
    string artist;
    int copiesSold;

    public Song(string name, string artist, int copiesSold)
    {
        this.name = name;
        this.artist = artist;
        this.copiesSold = copiesSold;
    }

    public Song():this("my_name","my_artist",1000)
    {
    }

}
Christian L.
  • 290
  • 3
  • 9