C ++ สามารถป้อนและส่งออกประเภทข้อมูลในตัวโดยใช้ตัวดำเนินการการแยกสตรีม>> และตัวดำเนินการแทรกสตรีม <<นอกจากนี้ ตัวดำเนินการแทรกสตรีมและแยกสตรีมยังสามารถโอเวอร์โหลดเพื่อดำเนินการอินพุตและเอาต์พุตสำหรับประเภทที่ผู้ใช้กำหนดเอง เช่น ออบเจ็กต์
ในที่นี้ สิ่งสำคัญคือต้องทำให้ฟังก์ชันโอเปอเรเตอร์โอเวอร์โหลดเป็นเพื่อนของชั้นเรียนเพราะจะถูกเรียกโดยไม่ต้องสร้างวัตถุ
ตัวอย่างต่อไปนี้จะอธิบายวิธีที่โอเปอเรเตอร์การแยก>> และตัวดำเนินการแทรก <<.
โค้ดตัวอย่าง
#include <iostream>
using namespace std;
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
friend ostream &operator<<( ostream &output, const Distance &D ) {
output << "F : " << D.feet << " I : " << D.inches;
return output;
}
friend istream &operator>>( istream &input, Distance &D ) {
input >> D.feet >> D.inches;
return input;
}
};
int main() {
Distance D1(11, 10), D2(5, 11), D3;
cout << "Enter the value of object : " << endl;
cin >> D3;
cout << "First Distance : " << D1 << endl;
cout << "Second Distance :" << D2 << endl;
cout << "Third Distance :" << D3 << endl;
return 0;
} ผลลัพธ์
$./a.out Enter the value of object : 70 10 First Distance : F : 11 I : 10 Second Distance :F : 5 I : 11 Third Distance :F : 70 I : 10