مرحباً @هبة حمدان
سبب عدم تغير القيمة الإفتراضية للإسم وهو أن نمط الوراثة في هذه الحالة protected أي أن الدوال و المتغيرات في الكلاس الأب المُعرفة على أنها public ستُصبح protected في الكلاس الإبن و بالتالي فإن الدالة setName ستُصبح محمية أي أنه لا يُمكنك إستخدامها في الدالة الرئيسية main بإمكانك تمرير الإسم من خلال بناء الكلاس Customer بهذا الشكل:
class Customer{
protected:
string name;
public:
Customer(string name = "Unknown"){
this->name = name;
}
string getName(){
return name;
}
void setName(string name = "Unknown"){
this->name = name;
}
};
و بالتالي عند إنشاء كائن من الصنف Customer يمكننا تحديد الإسم له، أيضا سنقوم بتوريث البناء للكلاس Invoice و تكون الطريقة كما هي موضحة أدناه:
class Invoice : protected Customer{
private:
float Total = 0;
public:
TV TVs[2];
Invoice(string name) : Customer(name){
for(int i=0;i<2;i++){
Total += TVs[i].price;
}
}
}
و في الدالة الرئيسية main يُمكننا عمل التالي:
int main() {
Invoice* x[2];
for(int i=0; i<2;i++){
x[i] = new Invoice("Invoice " + to_string(i)); // قمنا بتحديد الإسم هنا
x[i]->Print();
x[i]->getMin();
cout<<"********************************"<<endl;
}
return 0;
}
بخصوص هذا الجزء إعادة التحميل أو overloading تسمح لنا بتعريف دوال تحمل نفس الإسم لكن بمُعاملات مُختلفة لِنقم بإعادة تحميل الدالة Print بهذا الشكل:
void Print(string file) {
ofstream f;
f.open (file);
f << "The Name is : " << name << endl;
f << "Total is : " << Total << endl;
f << "TVs: " << endl;
for(int i=0;i<2;i++){
f << "TVs[" << i << "] = (Number = " << TVs[i].getNo() << ", Price = " << TVs[i].getPrice() << ")" << endl;
}
f.close();
}
و هنا قمنا بإنشاء ملف بإسم قيمة المُعامل المُمرر و قمنا بملئه بجميع المتغيرات المحلية و الموروثة و في الأخير قمنا بإغلاق الملف.
ليُصبح الكود بالكامل بهذا الشكل:
#include <iostream>
#include <fstream>
using namespace std;
/*---------------------------------------------------*/
class Customer{
protected:
string name;
public:
Customer(string name = "Unknown"){
this->name = name;
}
string getName(){
return name;
}
void setName(string name = "Unknown"){
this->name = name;
}
};
/*---------------------------------------------------*/
class TV{
private:
int no;
public:
float price;
TV(){
cout << "The TV Number is : " << endl;
cin >> no;
cout << "The TV Price is : " << endl;
cin >> price;
}
float getPrice(){
return price;
}
int getNo(){
return no;
}
void setPrice(float p){
price = p;
}
};
/*---------------------------------------------------*/
class Invoice : protected Customer{
private:
float Total = 0;
public:
TV TVs[2];
void ReadPrice(){
for(int i=0;i<2;i++){
cout << "TVs[" << i << "] = (Number = " << TVs[i].getNo() << ", Price = " << TVs[i].getPrice() << ")" << endl;
}
};
int getTotal(){
return Total;
}
Invoice(string name) : Customer(name){
for(int i=0;i<2;i++){
Total += TVs[i].price;
}
}
void Print(){
cout << "The Name is : " << name << endl;
cout << "Total is : " << Total << endl;
cout << "TVs: " << endl;
this->ReadPrice();
}
void Print(string file) {
ofstream f;
f.open (file);
f << "The Name is : " << name << endl;
f << "Total is : " << Total << endl;
f << "TVs: " << endl;
for(int i=0;i<2;i++){
f << "TVs[" << i << "] = (Number = " << TVs[i].getNo() << ", Price = " << TVs[i].getPrice() << ")" << endl;
}
f.close();
}
int getMin(){
float min;
min = TVs[0].price;
for(int i=0 ;i < 2; i++){
if (TVs[i].price < min)
min = TVs[i].price;
}
cout<<"The minimum :" << min <<endl;
return min;
}
};
/*---------------------------------------------------*/
int main() {
Invoice* x[2];
//customer y;
for(int i=0; i<2;i++){
x[i] = new Invoice("Invoice " + to_string(i));
string filename = "output" + to_string(i) + ".txt";
x[i]->Print(filename);
x[i]->getMin();
cout<<"********************************"<<endl;
}
return 0;
}
بإمكانك تجربة المثال من خلال هذا الرابط: هنا
بالتوفيق.