Statcic and Const data member and Const function

أنشِئ 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.

Pointer data member

أنشِئ Class :

اسمه:  Fraction

المتغيرات : *numerator و  denominator

:يحوي   Parameterized constructor  و Copy constructor و Default constructor

قم بفصل الـ Class interface  في header file و Class implementation في cpp file

لهدف من السؤال هو كيفية التعامل مع الـ pointer في constructors , destructor , set & get  .

الحل:

//.h
class Fraction
{
 int *numerator; //pointer data member
 int denominator;

public:

 Fraction(); //Default constructor
 Fraction(int, int);//Parameterized constructor 
 Fraction(const Fraction &);// Copy constructor 
 ~Fraction();//destructor

 void setNumerator(int);
 void setDenominator(int);

 int getDenominator();
 int getNumerator();

};
------------------------------------------------------
//.cpp
#include <iostream>
#include "Fraction.h"
using namespace std;

//Default constructor
Fraction::Fraction()
{
 numerator = new int; //allocate pointer
 if (!numerator) { cout << "Allocation failure"; exit(1); } //if not allocate
 //initialize
 setNumerator(1);
 setDenominator(1);
}

//Parameterized constructor
Fraction::Fraction(int N, int D)
{
 setNumerator(N); setDenominator(D);
}

//Copy constructor
Fraction::Fraction(const Fraction &obj)
{ numerator = new int; //allocate the pointer for the new object 
if (!numerator) { cout << "Allocation failure"; exit(1); } //if not allocte

//copy information
setNumerator(*obj.numerator); //use '*' to copy the value 
setDenominator(obj.denominator); }

//destructor
Fraction::~Fraction()
{ delete numerator; //delete pointer
}

void Fraction::setNumerator(int num)
{//use '*'
 *numerator = num; 
}
void Fraction::setDenominator(int num){ denominator = num; }

int Fraction::getDenominator(){ return denominator; }

int Fraction::getNumerator()
{ //use '*'
 return *numerator; 
}

تعريف Class

-في جامعة معيّنة يوجد 3 أنواع من Classroom ، كل Classroom لديه رقم و الحد الأعلى لعدد الطلاب الذي من الممكن أن يتحمّله والقسم الذي يستخدم هذا الـ Classroom، أنشِئ Class لـ Classroom.

الحل:

#include <iostream>
#include <string>
using namespace std;

class Classroom
{
public:
 //Memebr functions

//Default Constructor
 Classroom()
 {
 number = 0;
 MaxCapacity = 0;
 type = '\0';
 Department = '\0';
 } 
//Destructor
 ~Classroom(){} 

 //Set functions
 void setNumber(int num) { number = num; }
 void setMax(int Max){ MaxCapacity = Max; }
 void setType(string T) { type = T; }
 void setDepartment(string D){ Department = D; }

 //Get functions
 int getNumber(){ return number; }
 int getMax(){ return MaxCapacity; }
 string getType(){ return type; }
 string getDepartment(){ return Department; }
private:
 //Data Members
 int number;
 int MaxCapacity;
 string type;
 string Department;

}; //Each class end with semicolon