You need to know the difference between C and C + +

Source: Internet
Author: User
Tags function prototype

You need to know the difference between C and C + +

If you want to say the difference between C and C + +, you may be able to list a lot of aspects, but there are many aspects of the difference is that after we finish learning these two languages, we can

Well understood and differentiated, such as C is a process-oriented programming language, C + + although mainly based on the basis of C development of a new language, but it is not

C's alternatives, not C's upgrades, C + + and C are sibling relationships, which can be well-designed for object-oriented programming. I'm not going to talk about these comparisons.

But the difference which is easy to see, but which is easily overlooked or confused by us.

Difference One: Overloading problems

I use more IDE is VC6.0, writing code is usually built "Win32 Console application", now add test.cpp to the project,

Write the code in test.cpp with the following code:

1#include <stdio.h>2 3 intADD (intAintb)4 {5    returnA +b;6 }7 intADD (intAintBintc)8 {9    returna+b+C;Ten } One  A voidMain () - { -ADD (1,2); theADD (1,2,3); -    return; -}

The program compiles and runs correctly. This means that a C + + program can implement overloading, with the following conditions:
A. The function name is the same, the number of parameters is different;

B. The function name is the same, the number of parameters is the same but the parameter type is different.

While writing the same code in TEST.C, the following error message will appear at compile time:

So you can see that the C language is not support overloading, while C + + supports overloading.

Difference two: The void parameter of C + +

In a C program if there are no arguments when declaring a function, then you need to define the parameter as void to qualify the function to not pass any parameters, and if you do not make the parameter table default to null its meaning is to pass any parameter, the problem is actually due to compatibility with the earlier k& C standard caused by. K&c The parameter table is empty when declaring a function, that is, the function prototype signature information at function declaration does not contain parameter information, if it is necessary to explicitly define the behavior of the null parameter, it is necessary to deal with it yourself.

I write the C code in test01.c, the code is as follows:

1#include <stdio.h>2#include <conio.h>3 

3voidSayHello ()// is a variable-parameter function

5{6printf"\nhello World!");7 return;8 } 9 Ten voidMain () One { ASayHello (1); -SayHello (1,2,3); -SayHello ('a','b'); the - getch (); - return; -}

The code compiles and runs correctly. The definition of a function in the C language without arguments does not mean that the function does not pass arguments at the time of the call, but that the function
will be a variable parameter function that can pass different number of arguments when called. If you want to restrict this function from being able to pass parameters, you need to add the void keyword to the definition.

void SayHello (void).

Then write the code in Test02.cpp, the code is exactly the same as the code above, but there was an error at compile time:

Visible in the C + + language, a function without parameters can be called with a different number of arguments than C, and the function cannot pass any arguments.

void SayHello () or void SayHello (void) in C + + and void SayHello (void) in C are the same at this point.

Reference : http://www.cnblogs.com/thinkingfor/archive/2010/09/14/1825842.html

Difference Three: the function of struct

We all know that struct structs can be defined in C and C + + code, whereas C can only declare variables (such as int type, char type) in a struct.

But the C + + language can not only declare variables in a struct, but also define functions, and the advantages are similar to the ability of a class to have both a member variable and a method.

The code I wrote in Test_struct.cpp is as follows:

1#include <stdio.h>2#include <conio.h>3 4 structpeople5 {6    Char*name;//variables7    intAge ;8 9   voidSayHello (Char*name,intAge//function BodyTen   { OneName=name; AAge=Age ; -printf"\ n My name is%s,i am%d years old.", name,age); -           return; the    } - }; -  - voidMain () + { - people p1;  +P1. SayHello ("Mike", -); A  at getch (); -     return; -}


The program compiles and runs successfully, running the result:

If you add the above code in a. c file, you will be prompted with an error when compiling: "function cannot be a member of struct struct"

So it can be said that C + + in the use of the struct is the C has been extended.

Appendix: If there are important differences not given, I hope everyone can put forward, I can continue to improve.

You need to know the difference between C and C + +

Related Article

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.