C + + C + + C language differences the second speaking

Source: Internet
Author: User

//differences in ④:struct type enhancement (c + + version)#include <iostream>using namespacestd;//a struct in C + + is a new type of definition declaration//default access rights for structs in C + + public, unlike classesstructteacher{//char name[20]= "millet";//Error C2536: "Teacher::teacher::name": Cannot specify an explicit initializer for an array      //Error Reason: The above initialization, the allocation of memory space and the initialization of the content. //However, the struct does not allocate memory at the time of type definition, so error, for int age=1; maybe C + + compiler does special processing     Charname[ -]; intAge =1;//in C + +, when a struct is defined, a partial member variable can be assigned an initial value//C + + can be in the structure of the body to define functions;    intGetage () {returnAge ; }    intR; //int s = r*r; Do not make this dangerous definition, because when the teacher variable is defined, it will perform the r*r operation, at this time R may not be assigned a value//At this time S is a dirty data, if you need to use s, then S will not do r*r operation again, cause you never get the correct s value// The best way to write a function    intgets () {returnr*R; }};voidMain () {//in the C + + definition struct, you can use the struct keyword or do not use theTeacher T2; structTeacher T1; printf ("age=%d\n", T1.age); System ("Pause");}
//differences in ④:struct type enhancement (C language version)#include <stdio.h>#include<stdlib.h>#include<string.h>//The C-language struct defines a set of variables that the C compiler does not consider to be a new typestructteacher{Charname[ -]; //int age=1; Error: In the C language structure definition, you can not assign an initial value,    intAge ; //A function cannot be defined in a C language structure    /*int Getage () {return age; }*/};voidMain () {//Teacher T1; In C, defining a struct must use the struct keyword because the C compiler does not consider a struct to be a new type    structTeacher T1; System ("Pause");}

//differences in ⑤:struct type enhancement (c + + version)#include <iostream>using namespacestd;//all variables and functions in C + + must have a type//The default type in C is not legal in C + +//f (i) {//printf ("i=%d\n", I);//}intGo () {printf (""); //Error 1 C4716: "Go": Must return a value//in C + +, the function defines the return value type, so the value of the corresponding type must be returned, or the error will be    return 5;}//Summary://in C + +, int f (), and int f (void) have the same meaning, all of which represent an parameterless function that returns a value of int//C + + More emphasis type, any program element must display the specified typevoidMain () {System ("Pause");}
//differences in ⑤:struct type enhancement (C language version)#include <stdio.h>#include<stdlib.h>#include<string.h>//in C, a function's formal parameter can be set without a type, or the return value can be set without a typef (i) {printf ("i=%d\n", i);}intGo () {printf (""); //Warning warning C4716: "Go": Must return a value//in C, the function defines the return value type and can not return a value of the corresponding type, except that the compiler warns}//Summary://in C, int f (); a function that takes an arbitrary argument with a return value of int//int f (void); Indicates an argument function with a return value of intvoidMain () {//C, from the language compiler does not limit how many parameters can a function f accept? F1,2,3,4,5); System ("Pause");}

//difference ⑥:bool Type (c + + version)#include <iostream>using namespacestd;voidMain () {BOOLb =true; printf ("the size of the bool type is%d bytes \ n",sizeof(BOOL)); //bool Type accounts for 1 bytes of memory space of a small sizeb =4;//type implicit conversion warning C4305: "=": Truncation from "int" to "bool"printf"b=%d\n", b);//Printing 1b =0; printf ("b=%d\n", b);//Printing 0System"Pause");}

C + + C + + C language differences the second speaking

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.