C ++Although the technology is very fashionable, many C users want to label themselves as C ++ in the shortest time. There are a lot of books about C ++, but only those users who are lucky enough to get started will occasionally flip over. There are still a lot of hooligans wandering at the door of C ++.
This article only targets C users, preferably a very good old user. For example, he tries to use pointers when encountering the simplest problem ), through some C and better C ++, this article uses Borland C ++ 3.1) routines to introduce some knowledge about C ++, so that readers and friends can "dive deeper and deeper ", easy to use C to C ++!
I. tags! Tag!
Quickly add a C ++ label to your program, making you look like a qualified C ++ user ......
1. comment)
The C ++ annotation can be in two forms. The first is the/* and */adopted by traditional C, and the other is the //, which indicates that the comments are from // to the end of the line. Readers and friends can use // to make your code take the C ++ breath, such as test0l:
- //test01.cpp
- #include <iostream.h>
- //I'm a C++user!
- //…and C is out of date.
- void main()
- {
- cout<<"Hello world!\n"; //prints a string
- }
- Hello-world!
If you try to find these advanced annotations in test0l. exe, it is very simple and they will not be there.
2. cincout
You may have sniffed out something from test0l. In C ++, cout is the second gender, not the old printf ). The meaning of the Left shift operator '<' is rewritten, which is called "output operator" or "insert operator ". You can use '<' to concatenate a large amount of data like a sugar gourd, and then use cout to output:
- cout << "ASCII code of "<< 'a' << " is:" <<97;
- ASCII code of a is:97
How to output an address value? In C, the format controller "% p" can be used, for example:
- printf ("%p,&i);
Similarly, C ++ is like this:
- cout << & i;
But it is different for strings! Because:
- char * String="Waterloo Bridge";
- cout << String; //prints ‘Waterloo Bridge'
Only the String content is output. There are still some methods, such as forced type conversion:
- cout<<(void *)String;
Cin adopts the '>' operator, which is called "input operator" or "extract operator ". The header file iostream. h contains the prototype definition of cin cout. The writing format of cin statements is exactly the same as that of cout:
- cin>>i; //ok
- cin>>&i; //error. Illegal structure operation
See? Don't give it a common '&' address of scanf.
C ++ also provides an operation operator endl, which has the same functions as '\ n'. For example, the cout statement in test0l can be changed:
- cout << ”Hello world!”<