–أنشِئ Class :
اسمه: Date
المتغيرات : Day , Month , Year
الدوال الإضافية : Display function “لطباعة التاريخ “
:يحوي Parameterized constructor و Copy constructor
-قم بفصل الـ Class interface في header file و Class implementation في cpp file
في Main funtion :
أطلب من المستخدم إدخال اليوم والشهر والسنة ثم أنشِئ object وأملأه بالمعلومات المُدخلة ، ثم أطبع التاريخ ، بعد ذلك أنشِئ object آخر يحمل نفس معلومات الـ object الأول ثم أطبع التاريخ للـ object الثاني.
الحل:
//.h
class Date
{
int Day, Month, Year;
public:
Date(int, int, int); // Parameterized constructor
Date(const Date &); //Copy constructor
~Date(); //destructor
//set functions
void SetDay(int);
void SetMonth(int);
void SetYear(int);
//get functions
int GetDay();
int GetMonth();
int GetYear();
//dispaly
void Display();
};
-----------------------------------------------------
//.cpp
#include <iostream>
#include "Date.h"
using namespace std;
// Parameterized constructor
Date::Date(int D, int M, int Y){ SetDay(D); SetMonth(M); SetYear(Y); }
//Copy constructor
Date::Date(const Date &obj)
{
SetDay(obj.Day); SetMonth(obj.Month); SetYear(obj.Year);
}
Date::~Date(){ }
void Date::SetDay(int D) { Day = D; }
void Date::SetMonth(int M){ Month = M; }
void Date::SetYear(int Y) { Year = Y; }
int Date::GetDay(){ return Day; }
int Date::GetMonth(){ return Month; }
int Date::GetYear(){ return Year; }
void Date::Display()
{
cout << "The Date is is: " << GetDay() << "\\" << GetMonth() << "\\" << GetYear() << endl;
}
-------------------------------------------------------
//main
#include <iostream>
#include "Date.h" //must include
using namespace std;
void main()
{
int Day, Month, Year;
cout << "Ente the Day: "; cin >> Day;
cout << "Ente the Month: "; cin >> Month;
cout << "Ente the Year: "; cin >> Year;
//initialized by Parameterized constructor
Date obj1(Day, Month, Year);
obj1.Display();
//initialized by copy constructor
Date obj2(obj1);
obj2.Display();
system("pause");
}