在C++中,有许多流类,用于定义与文件相关的各种流以及进行输入输出操作。所有这些类都在文件iostream.h中定义。下图显示了这些类的层次结构。
- ios类是流类层次结构中的最高类。它是istream,ostream和streambuf类的基类。
- istream和ostream为iostream类提供基类。 istream类用于输入, ostream类用于输出。
- 类IOS使用istream和ostream的间接继承到的iostream类。为了避免ios类的数据和成员函数的重复,当在istream和ostream中继承为ios时,将其声明为虚拟基类。
class istream: virtual public ios
{
};
class ostream: virtual public ios
{
};
_withassign类为赋值操作提供了额外的功能,这就是_withassign类的原因。
这些流类提供的设施。
- ios类: ios类负责为所有其他流类提供所有输入和输出功能。
- istream类:此类负责处理输入流。它提供了许多处理字符,字符串和对象的函数,例如get,getline,read,ignore,putback等。
例子:
CPP14
#include
using namespace std;
int main()
{
char x;
// used to scan a single char
cin.get(x);
cout << x;
}
CPP14
#include
using namespace std;
int main()
{
char x;
// used to scan a single char
cin.get(x);
// used to put a single char onto the screen.
cout.put(x);
}
CPP14
#include
using namespace std;
int main()
{
// this function display
// ncount character from array
cout.write("geeksforgeeks", 5);
}
CPP14
#include
using namespace std;
class demo {
public:
int dx, dy;
// operator overloading using friend function
friend void operator>>(demo& d, istream& mycin)
{
// cin assigned to another object mycin
mycin >> d.dx >> d.dy;
}
};
int main()
{
demo d;
cout << "Enter two numbers dx and dy\n";
// calls operator >> function and
// pass d and cin as reference
d >> cin; // can also be written as operator >> (d, cin) ;
cout << "dx = " << d.dx << "\tdy = " << d.dy;
}
CPP14
#include
using namespace std;
class demo {
public:
int dx, dy;
demo()
{
dx = 4;
dy = 5;
}
// operator overloading using friend function
friend void operator<<(demo& d, ostream& mycout)
{
// cout assigned to another object mycout
mycout << "Value of dx and dy are \n";
mycout << d.dx << " " << d.dy;
}
};
int main()
{
demo d; // default constructor is called
// calls operator << function and
// pass d and cout as reference
d << cout; // can also be written as operator << (d, cout) ;
}
输入:
g
输出:
g
ostream类:此类负责处理输出流。它提供的函数数量用于处理字符,字符串和对象,如写,放等。
例子:
CPP14
#include
using namespace std;
int main()
{
char x;
// used to scan a single char
cin.get(x);
// used to put a single char onto the screen.
cout.put(x);
}
- 输入:
g
输出:
g
iostream:此类负责处理输入流和输出流,因为istream类和o流类都继承到其中。它提供了istream类和o流类的函数,用于处理字符,字符串和对象,例如get,getline,read,ignore,putback,put,write等。
例子:
CPP14
#include
using namespace std;
int main()
{
// this function display
// ncount character from array
cout.write("geeksforgeeks", 5);
}
输出:
geeks
istream_withassign类:此类是istream的变体,允许对象分配。预定义的对象cin是此类的对象,因此可以在运行时重新分配给其他istream对象。
示例:显示cin是istream类的对象。
CPP14
#include
using namespace std;
class demo {
public:
int dx, dy;
// operator overloading using friend function
friend void operator>>(demo& d, istream& mycin)
{
// cin assigned to another object mycin
mycin >> d.dx >> d.dy;
}
};
int main()
{
demo d;
cout << "Enter two numbers dx and dy\n";
// calls operator >> function and
// pass d and cin as reference
d >> cin; // can also be written as operator >> (d, cin) ;
cout << "dx = " << d.dx << "\tdy = " << d.dy;
}
输入:
4 5
输出:
Enter two numbers dx and dy
4 5
dx = 4 dy = 5
ostream_withassign类:此类是ostream的变体,允许对象分配。预定义的对象cout,cerr,clog是此类的对象,因此可以在运行时重新分配给其他ostream对象。
示例:显示cout是ostream类的对象。
CPP14
#include
using namespace std;
class demo {
public:
int dx, dy;
demo()
{
dx = 4;
dy = 5;
}
// operator overloading using friend function
friend void operator<<(demo& d, ostream& mycout)
{
// cout assigned to another object mycout
mycout << "Value of dx and dy are \n";
mycout << d.dx << " " << d.dy;
}
};
int main()
{
demo d; // default constructor is called
// calls operator << function and
// pass d and cout as reference
d << cout; // can also be written as operator << (d, cout) ;
}
输出:
Value of dx and dy are
4 5
相关文章:
- C++中的基本输入/输出
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。