C has arrays, structs, pointers, functions, macros
C + + has namespaces, references, default parameters, templates, function overloads, custom operators, inline, construct/destructor, private/protected Members, friends, exceptions.
I. Declaration of data types
1. C + + allows data declaration to appear anywhere in the program
C Code (Exception)
1#include <stdlib.h>2#include <stdio.h>3 4 intMainintargcChar*argv[])5 {6 for(intI=0; i<5; i++)7printf"Hello%d\n", i);8 9 return 0;Ten}
C + + code (normal)
1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 intMainintargcChar*argv[])6 {7 for(intI=0; i<5; i++)8printf"Hello%d\n", i);9 Ten return 0; One}
2. C + + allows entities to be defined using struct names
C Code (Exception)
1#include <stdlib.h>2#include <stdio.h>3#include <string.h>4 5 //You cannot define entities directly using struct names in C language6 struct Person7 {8 Charname[ -];9 intAge ;Ten }; One A intMainintargcChar*argv[]) - { - //struct person person; the person person ; - -strcpy (Person.name,"Tom"); -Person.age =5; + -printf"%s is%d\n", Person.name, person.age); + A return 0; at}
C + + code (normal)
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 //You can define entities directly using struct names in C + +7 struct Person8 { 9 Charname[ -];Ten intAge ; One }; A - intMainintargcChar*argv[]) - { the // - person person ; - -strcpy (Person.name,"Tom"); +Person.age =5; - +printf"%s is%d\n", Person.name, person.age); A at return 0; -}
Second, struct
1. C + + allows functions to manipulate data members within a struct as a struct member declaration.
C Code (Exception)
1#include <stdlib.h>2#include <stdio.h>3#include <string.h>4 5 //a function in C that does not allow manipulation of data members within a struct, as a struct member declaration6 struct Person7 {8 Charname[ -];9 intAge ;Ten One // A voidOutput () {printf ("%s is%d\n", name, age); } - }; - the intMainintargcChar*argv[]) - { - structperson person ; - +strcpy (Person.name,"Tom"); -Person.age =5; + A person.output (); at - return 0; -}
C + + code (normal)
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 //a function in C + + that allows manipulation of data members within a struct as a struct member declaration7 struct Person8 {9 Charname[ -];Ten intAge ; One A // - voidOutput () {printf ("%s is%d\n", name, age); } - }; the - intMainintargcChar*argv[]) - { - // + person person ; - +strcpy (Person.name,"Tom"); APerson.age =5; at - person.output (); - - return 0; -}
Linux/C + + differences: type declaration, struct