–أنشِئ Class :
اسمه: Patient
المتغيرات : Name , Age , ID , DoctorName
:يحوي Parameterized constructor و Display function “لطباعة معلومات كل مريض “.
-Get function , Display function يجب أن تكون read only .
-ID الخاص بكل مريض يتم إنشاؤه أوتوماتكيًا ولايمكن تغييره .
–قم بفصل الـ Class interface في header file و Class implementation في cpp file
*في Main function أنشِئ 3 objects وأطبع معلوماتهم.
الحل:
المقصود بـ read only هي أن تكون constant function وهذا النوع من الـ functions لايمكنه تغيير قيمة أي متغير ، مثال لايمكن كتابة هذه الجملة داخل هذا النوع من الدوال : “Name = “Amjad” >> “Error.
إنشاء الـ ID أتوماتيكيًا يتم عن طريق تعريف static data member بحيث أن static هو متغيّر عام تشترك فيه كل objects لهذا الـ class وليس خاص لكل object ، بحيث أن كل تغيير على static تستطيع أن تراه كل الـ objects .
-ID يكون const حتى لايتم تغييره ، وبذلك لايحتاج إلى set function لأنه لايمكن التعديل عليه .
//.h
#include <iostream>
#include <string>
using namespace std;
class patient
{
string Name;
float age;
const int ID; //const ID
string doctorName;
static int num; // stataic var. to generate the ID automatically
public:
patient(string, string, float); //Parameterized constructor
//set functions
void SetName(string);
void SetdoctorName(string);
void Setage(float);
//read only get functions
string GetName() const;
string GetdoctorName() const;
float Getage() const;
int GetID() const;
//const display function
void Display() const;
};
-----------------------------------------------------------------
//.cpp
#include <iostream>
#include <string>
#include "patient.h"
using namespace std;
int patient::num = 0; //initialize static data memebr
////Parameterized constructor
//initialize the data members using initializer list
patient::patient(string N, string DN, float A) :Name(N), doctorName(DN), age(A), ID(++num){}//initialize ID by num
//set functions
void patient::SetName(string N){ Name = N; }
void patient::Setage(float A){ age = A; }
void patient::SetdoctorName(string DN){ doctorName = DN; }
//const get functions
string patient::GetName() const{ return Name; }
string patient::GetdoctorName() const { return doctorName; }
float patient::Getage() const{ return age; }
int patient::GetID() const { return ID; }
//const display function
void patient::Display() const
{
cout << "Your ID: " << GetID() << endl;
cout << "Your Name: " << GetName() << endl;
cout << "YOur Age: " << Getage() << endl;
cout << "Your D. Name: " << GetdoctorName() << endl << endl;
}
------------------------------------------------------------
//main.cpp
#include <iostream>
#include <string>
#include "patient.h"
using namespace std;
void main()
{
string Name[3], doctorName[3]; float age[3];
//read the info
for (int i = 0; i<3; i++)
{
cout << "you are patint #" << i + 1 << endl;
cout << "Enter Your Name: ";
cin >> Name[i];
cout << "Enter YOur Age: ";
cin >> age[i];
cout << "Enter Your D. Name: ";
cin >> doctorName[i];
cout << endl;
}
//create objects
patient obj1(Name[0], doctorName[0], age[0]);
patient obj2(Name[1], doctorName[1], age[1]);
patient obj3(Name[2], doctorName[2], age[2]);
//print info
obj1.Display();
obj2.Display();
obj3.Display();
system("pause");
}
-السطر 38 طريقة تعريف static في cpp.
-السطر 42 يحوي طريقة initialize ID باستخدام static data member.