Answer to Question #234154 in C# for nawab ul haq

Question #234154

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.


1
Expert's answer
2021-09-07T02:10:47-0400
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);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS