Records: The concepts of cin.get..

If you are n't cookie, please skip it ... Thanks

from now on, I get two methods or ways to use it:

cin.get(name,ArSize).get()

cin.get(ch);

It's really confuse me right now.  I Know C language, the single arguments tha's not allowed in C-style.

There is a explation about it, which is quoted from the C++  primer plus (The Fifth Edition)

Once again it is time for those of you familiar with C to get excited or confused. In C if a function takes a pointer-to-char and an int as arguments, you can't successfully use the same function with a single argument of a different type. But you can do so in C++ because the language supports an OOP feature called function overloading. Function overloading allows you to create different functions that have the same name, provided that they have different argument lists. If, for example, you use cin.get(name, ArSize) in C++, the compiler finds the version of cin.get() that uses a chr* and an int as arguments. But if you use cin.get(ch), the compiler fetches the version that uses a single type char argument. And if the code provides no arguments, the compiler uses the version of cin.get() that takes no argumetns. Function overloading enables you to use the same name for related functions that perform the same basic task in different ways or for different types. This is another topic awaiting you  in Chapter 8. Meanwhile, you can get accustomed to function overloading by using the get() examples that come with the istream class. To distinguish between the different function versions, we'll include the argument list when referring to them. Thus, cin.get() means the version that takes no arguments, and cin.get(char) means the version that takes one argument...

......

the OOP features: Function overloading....

Quoted from the C++ primer Plus

cin.get(ch) Versus cin.get()

Propertycin.get(ch)ch=cin.get()
Method for conveying input characterAssign to argument chUse function return value to assign to ch
Function return value for character inputA class istream object(true after bool conversion)Code for character as type int value
Function return value at EOFA class istream object(false after bool conversion)EOF
The form with the character argument is integrated more fully into the object approach because its return value is an istream object. This means, for example, that you can chain uses. For example, the following code means read the next input character into ch1 and the following input character into ch2:

cin.get(ch1).get(ch2);

This works because the function call cin.get(ch1) returns the cin object, which then acts as the object to which get(ch2) is attached.

Probably the main use for the get() form is to let you make quick-and-dirty conversions from the getchar() and putchar() functions of stdio.h to the cin.get() and cout.put() methods of iostream. You just replace one header file with the other and globally replace getchar() and putchar() with their act-alike method equivalents. (If the old code uses a type int variable for input, you have to make further adjstments if your implementation has multiple prototypes for put().)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值