这是真的,有没有必要学习C,因为C ++中包含的一切吗? [英] Is it true that there is no need to learn C because C++ contains everything?

查看:127
本文介绍了这是真的,有没有必要学习C,因为C ++中包含的一切吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一类C ++编程和教授告诉我们,没有必要学习C,因为C ++包含C以及面向对象的功能应有尽有。然而,也有人告诉我,这未必是真实的。任何人都可以阐明这一些轻?


解决方案

概述:

这几乎是事实,C ++是C的超集,和你的教授是在正确的没有必要单独学习C。

C ++增加整个面向对象的方面中,通用编程的方面,以及具有不那么严格的规则(如需要在各函数的顶部被声明变量)。 C ++的确改变了一些C方面,比如结构的定义,虽然仍然在一个超集的方式。

为什么它不是一个严格的超例子:

这个维基百科文章有这样的差异几个很好的例子:


  

一个经常遇到的区别是
  这C允许的隐式转换
  void *的其它指针类型,但是C ++
  才不是。因此,以下是有效的C
  code:

 为int * i = malloc的(sizeof的(INT)* 5);


  
  

...但是,使其在C工作,
  C ++人会需要使用明确
  投:

 为int * I =(INT *)malloc的(的sizeof(int)的* 5)


  
  

另一种常见的便携性问题
  那C ++定义了许多新的关键字,
  如新的类,这可能是
  用作标识符(例如变量
  名称)在C程序中。


此Wikipedia文章有进一步的差异,以及:


  

C ++编译器禁止转到越过初始化,如下面的C99 code:


 无效FN(无效)
 {
  转到高炮;
  INT I = 1;
 弗莱克:
   ;
 }

你应该先了解?

您应该学习C ++第一,不是因为学习C首先会伤害你,不是因为你必须忘掉什么(你不会),但因为在第一次学习C没有任何好处。你会最终学会只是关于C反正一切,因为它是多还是少了C ++。

I am taking a class in C++ programming and the professor told us that there is no need to learn C because C++ contains everything in C plus object-oriented features. However, some others have told me that this is not necessarily true. Can anyone shed some light on this?

解决方案

Overview:

It is almost true that C++ is a superset of C, and your professor is correct in that there is no need to learn C separately.

C++ adds the whole object oriented aspect, generic programming aspect, as well as having less strict rules (like variables needing to be declared at the top of each function). C++ does change the definition of some terms in C such as structs, although still in a superset way.

Examples of why it is not a strict superset:

This Wikipedia article has a couple good examples of such a differences:

One commonly encountered difference is that C allows implicit conversion from void* to other pointer types, but C++ does not. So, the following is valid C code:

int *i = malloc(sizeof(int) * 5);

... but to make it work in both C and C++ one would need to use an explicit cast:

int *i = (int *) malloc(sizeof(int) * 5)

Another common portability issue is that C++ defines many new keywords, such as new and class, that may be used as identifiers (e.g. variable names) in a C program.

This wikipedia article has further differences as well:

C++ compilers prohibit goto from crossing an initialization, as in the following C99 code:

 void fn(void)
 {
  goto flack;
  int i = 1;
 flack:
   ;
 }

What should you learn first?

You should learn C++ first, not because learning C first will hurt you, not because you will have to unlearn anything (you won't), but because there is no benefit in learning C first. You will eventually learn just about everything about C anyway because it is more or less contained in C++.

这篇关于这是真的,有没有必要学习C,因为C ++中包含的一切吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆