КАТЕГОРИИ: Архитектура-(3434)Астрономия-(809)Биология-(7483)Биотехнологии-(1457)Военное дело-(14632)Высокие технологии-(1363)География-(913)Геология-(1438)Государство-(451)Демография-(1065)Дом-(47672)Журналистика и СМИ-(912)Изобретательство-(14524)Иностранные языки-(4268)Информатика-(17799)Искусство-(1338)История-(13644)Компьютеры-(11121)Косметика-(55)Кулинария-(373)Культура-(8427)Лингвистика-(374)Литература-(1642)Маркетинг-(23702)Математика-(16968)Машиностроение-(1700)Медицина-(12668)Менеджмент-(24684)Механика-(15423)Науковедение-(506)Образование-(11852)Охрана труда-(3308)Педагогика-(5571)Полиграфия-(1312)Политика-(7869)Право-(5454)Приборостроение-(1369)Программирование-(2801)Производство-(97182)Промышленность-(8706)Психология-(18388)Религия-(3217)Связь-(10668)Сельское хозяйство-(299)Социология-(6455)Спорт-(42831)Строительство-(4793)Торговля-(5050)Транспорт-(2929)Туризм-(1568)Физика-(3942)Философия-(17015)Финансы-(26596)Химия-(22929)Экология-(12095)Экономика-(9961)Электроника-(8441)Электротехника-(4623)Энергетика-(12629)Юриспруденция-(1492)Ядерная техника-(1748) |
Status of a stream
Input ... Class Complex ... Class ostream Output Standard streams Topic 16. STREAMS
Along with <stdio.h> library, containing input/ouput functions printf, scanf, etc., C++ language has also the library <iostream.h>. In this library there are classes and functions for information input and output. To use this library the program should contain the statement #include <iostream.h>
Advantages of <iostream.h> 1. The syntax is more simple and more elegant that syntax of printf and scanf. 2. Formatted output can be easily redefined. 3. Th library <iostream.h> has a wide hierarchy of classes for buffered and non-buffered output.
In C++ language there are 4 predefinned objects for input/output: · cin - standard input (usually keyboard), corresponding to stdin in C language; · cout - standard output (usually screen), corresponding to stdout in C language; · cerr - standard file for error messages (the screen by default), corresponding to stderr in C language; · clog - full buffered cerr object.
For input and output following operators are used: >> - overloaded input operator “get” << - overloaded output operator “put”. These operators if necessary can be overloaded (redefined) for user defined classes.
EXAMPLE:
In C++ language there is standard predefined class for output { ostream& operator<<(char*); ostream& operator<<(int); ostream& operator<<(long); ostream& operator<<(double); ostream& put(char); };
The operator << has two operands: the first operand is of class ostream, the second is the value for output. Operator << (“put”) returns the reference to ostream object. It is necessary for so called cascade output: cout<<”x=”<<x; This line is interpreted as (cout,operator<<(“x=”)).operator<<(x);
From declaration of ostream class it can be seen that operator << is overloaded for all predefined data types: char, int, long and double. 16.2.1.Overloading << for user defined classes
Consider previously implemented class for Complex numbers. It can be modified for standard output into cout with the help of overloaded operator <<.
#include <stdio.h> #include <iostream.h> { double Re; double Im; public: Complex(double r, double i) {Re=r; Im=i;}; Complex Add(Complex a,Complex b); Complex& Assign(Complex a); friend ostream& operator<<(ostream& s, Complex x); friend istream& operator>>(istream& s, Complex& a); }; ostream& operator<<(ostream& s, Complex x) { return s<<"("<<x.Re<<","<<x.Im<<")"; } Now we can print complex numbers in the form (re,im), for example: main() { Complex x(3,4); cout<<”x=”<<x<<’\n”; } In fact we expanded class ostream with our own operator << for Complex objects. Input is similar to output. There is class istream, which supplies operator >> (“get”) for standard data types of C language:
EXAMPLE:
User can add his own operator >> for new classes.
EXAMPLE: input operator for Complex class.
Operators << and >> for Complex class cann be tested with the help of a following program:
Every stream is connected with error states. They are described as follows in clas ios (stands for input/output status).
Дата добавления: 2014-11-28; Просмотров: 345; Нарушение авторских прав?; Мы поможем в написании вашей работы! Нам важно ваше мнение! Был ли полезен опубликованный материал? Да | Нет |