زيادة تحميل مؤثرات الإدخال والإخراج في ++C
زيادة تحميل مؤثرات الإدخال والإخراج
ننتقل الأن إلي موضوع أخر متعلق بالتدفقات , زيادة تحميل مؤثري الإدخال والإخراج , وهي أداة من الأدوات القوية للسي++ , فيها يمكنك أن تشكل إخراج الكائنات التي عرفتها علي الصورة الملائمة لها مباشرة دون أن تتجشم عبء دالة مخصوصة للإظهار , علي غرار الدالة display() التي كانت قاسما مشتركا في كافة برامجنا المتعلقة بالمسافات الإنجليزية . ويبين لك البرنامج التالي هذه الإمكانية .
englio.cpp
// englio.cpp// overloaded << and >> operators// UCS Laboratories#include <iostream.h>
#include <conio.h>
class Distance // English Distance class
{ private: int feet; float inches; public: Distance() // constructor (no args) { feet = 0; inches = 0.0; }Distance(int ft, float in) // constructor (two args)
{ feet = ft; inches = in; } friend istream& operator >> (istream& s, Distance& d); friend ostream& operator << (ostream& s, Distance& d);};
istream& operator >> (istream& s, Distance& d) // get Distance
{ // from usercout << "\nEnter feet: "; s >> d.feet; // using
cout << "Enter inches: "; s >> d.inches; // overloaded
return s; // >> operator
}
ostream& operator << (ostream& s, Distance& d) // display
{ // Distances << d.feet << "\'-" << d.inches << '\"'; // using
return s; // overloaded
} // << operatorvoid main() { Distance dist1, dist2; // define Distances cout << "\nEnter two Distance values:"; cin >> dist1 >> dist2; // get values from user Distance dist3(11, 6.25); // define, initialize dist3 // display distancescout << "\ndist1 = " << dist1 << "\ndist2 = " << dist2;
cout << "\ndist3 = " << dist3;getche();
}
وكما تري زيد تحميل المؤثرين << و >> بحيث يمكنك معاملة المتغيرات من النوع distance كأي متغير أخر من متغيرات اللغة الأساسية , كأن تستخدم لها أوامر علي الصورة :
Cin >> sist1 >> dist2;
Cout <<dist1 <<dist2;
وأسلوب تحميل المؤثرين متشابه , وهما يعيدان , بطريق الإشارة , كائنا من النوع istream بالنسبة للمؤثر >> , ومن النوع ostream بالنسبة للمؤثر << . ويتقبل كلاهما معاملين يمررا لهما الحالة بطريقة الإشارة أيضا , المعامل الأول هو كائن من نوع istream في الحالة الأولي , ألا وهو cin , وكائن من النوع ostream في الحالة الثانية و هو الكائن cout أما المعامل الثاني لدالة زيادة التحميل فهو الكائن الكائن المراد إظهاره .
وفي حالة الإدخال , يقوم المؤثر >> بتقبل تدفق من الكائن المعرف في المعامل الأول , ويضعه في عنصر البيانات للكائن المعرف في المعامل الثاني . وفي حالة الإخراج , يقوم المؤثر << باستخلاص البيانات من الكائن المعرف في المعامل الثاني , ويرسله للكائن المعرف في المعامل الأول .
ويجب أن تكون كلتا الدالتين >> operator <<, operator صديقتين للفئة Distance حيث يقع الكائنان الخاصين بكلا من istream, ostream علي الجانب الأيسر من المؤثر .
زيادة التحميل من أجل الملفات
في البرنامج التالي سوف نبين كيف يمكن زيادة تحميل مؤثري الإدخال والإخراج لكي يتعاملا مع الملفات , أسوة بما فعلناه بالنسبة للكائنين cin, cout .
englio2.cpp
// englio2.cpp// overloaded << and >> operators can work with files// UCS Laboratories#include <fstream.h>
#include <conio.h>
class Distance // English Distance class
{ private: int feet; float inches; public: Distance() // constructor (no args) { feet = 0; inches = 0.0; }Distance(int ft, float in) // constructor (two args)
{ feet = ft; inches = in; } friend istream& operator >> (istream& s, Distance& d); friend ostream& operator << (ostream& s, Distance& d);};
istream& operator >> (istream& s, Distance& d) // get Distance
{ // from file orchar dummy; // for ('), (-), and (") // keyboard
// withs >> d.feet >> dummy >> dummy >> d.inches >> dummy;
return s; // overloaded
} // >> operatorostream& operator << (ostream& s, Distance& d) // send Distance
{ // to file ors << d.feet << "\'-" << d.inches << '\"'; // screen with
return s; // overloaded
} // << operatorvoid main() { char ch;Distance dist1;
ofstream ofile; // create and openofile.open("DIST.DAT"); // output stream
do { cout << "\nEnter Distance: "; cin >> dist1; // get distance from user ofile << dist1; // write it to output str cout << "Do another (y/n)? ";cin >> ch;
}
while(ch != 'n');
ofile.close(); // close output stream ifstream ifile; // create and openifile.open("DIST.DAT"); // input stream
cout << "\nContents of disk file is:"; while(1) { ifile >> dist1; // read dist from streamif( ifile.eof() ) // quit on EOF
break;cout << "\nDistance = " << dist1; // display distance
}
getche();
}
وقد أجرينا تعديلا صغيرا هنا , فلم يعد المؤثر >> يستحث علي الإدخال , حيث لا معني لاستحثاث ملف . ونفترض أن المستخدم يعلك جيدا كيف يدخل الأبعاد بالصورة المطلوبة وإلا فلن يمكن كتابة الأبعاد أو قراءتها .
تعليقات
إرسال تعليق