Create a class called Date that has separate int member data for day, month and year. One constructor should initialize this data to 0. and another should initialize it to fixed values. A member function should display it, in day/month/year format. Write a program to add Date of two objects by overloading '+' operator.
using System;
namespace ConsoleApplication1
{
internal class Program
{
public static void Main(string[] args)
{
var date1 = new Data(5, 6, 7);
var date2 = new Data(3, 4, 5);
var date3 = date1 + date2;
date3.Display();
}
}
class Data
{
public int Day;
public int Month;
public int Year;
public Data()
{
Day = 0;
Month = 0;
Year = 0;
}
public Data(int d, int m, int y)
{
Day = d;
Month = m;
Year = y;
}
public void Display()
{
Console.WriteLine($"{Day}/{Month}/{Year}");
}
public static Data operator +(Data d1, Data d2)
{
return new Data(d1.Day + d2.Day, d1.Month + d2.Month, d1.Year + d2.Year);
}
}
}
Comments
Leave a comment