Experience Sharing from C to C ++ (1) (1)

Source: Internet
Author: User

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:

 
 
  1. //test01.cpp  
  2. #include <iostream.h>  
  3. //I'm a C++user!  
  4. //…and C is out of date.  
  5. void main()  
  6. {  
  7. cout<<"Hello world!\n"; //prints a string  
  8. }  
  9. 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:

 
 
  1. cout << "ASCII code of "<< 'a' << " is:" <<97;   
  2. ASCII code of a is:97  

How to output an address value? In C, the format controller "% p" can be used, for example:

 
 
  1. printf ("%p,&i); 

Similarly, C ++ is like this:

 
 
  1. cout << & i; 

But it is different for strings! Because:

 
 
  1. char * String="Waterloo Bridge";  
  2. cout << String; //prints ‘Waterloo Bridge' 

Only the String content is output. There are still some methods, such as forced type conversion:

 
 
  1. 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:

 
 
  1. cin>>i; //ok  
  2. 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:

 
 
  1. cout << ”Hello world!”< 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.