Basic change from C to C++

  • 2020-05-27 06:39:02
  • OfStack

When it comes to the difference between C++ and C, most people think of object orientation and process orientation. But that is not accurate. Object-oriented and process-oriented are two different programming ideas, while C++ and C are two programming languages. Can't C++ be used for process-oriented problem solving? Of course. The object-oriented design philosophy can also be applied to the C language, which I covered in previous articles.

Our goal in this series is to put aside programming ideas and simply introduce from a grammatical point of view what features are added to C++ that are not found in the C language. I hope you will learn this after you have mastered C.

First, let's look at 1 standard C++ code:


  // main.cpp
  #include <iostream>

  using namespace std;

  int main()
  {
    cout << "Hello World!" << endl;

    return 0;
  }

This is a basic program that prints the words "Hello World" on the screen. Is it quite different from C? Today, we'll start with some basic differences between the C++ and C languages.

1. The header files

In C, the standard I/o header file is referenced as follows:

#include <stdio.h>
C++ USES:

#include <iostream>
C++ states that system-supplied header files are not referred to as ".h ". If it is a self-defined header file, it needs to be referenced like this:

#include "myfile.h"
Note that not only is ".h "used, but also double quotation marks are used. The reason is the same as the C language, starting from the current path. In addition, since C++ fully supports the full capabilities of C, we will still use the following when using the header files provided by C:

#include <stdio.h>
The advantage of this definition is that we can tell whether we are referring to the C++ system library or the C language system library or the custom function library just by looking at the inclusion form of the header file.

2. Namespace

You must have noticed this:

using namespace std;
The meaning of this sentence is to introduce the namespace std, where the cout keyword we used to output the string is in std. Functions in the C++ library are divided into different namespaces, and if you need to access them, use this sentence to introduce the namespace first.

If we hadn't written this, we would have manually specified the namespace every time we used these functions, and the previous code would have looked like this:


// main.cpp
#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;

  return 0;
}

It's legal, but it's a little bit of a hassle.

3. Input/output of C++


The input and output of C++ have been significantly modified. Let's take a look at the following example:


#include <iostream>

using namespace std;

int main()
{
  int a, b;

  cout << "Please input two numbers : " << endl;

  cin >> a >> b;

  cout << "a = " << a << ", " << "b = " << b << endl;

  return 0;
}

This program enters two Numbers and prints them on the screen. The execution results of the program are as follows:


cin
Equivalent to scanf, used to save the input characters in subsequent variables. cin must and > > 1 from use, need to input a few variables to write a few variables, between > > Separated.

cout
Equivalent to printf for printing strings or variables on the screen. Elements that need to be printed are used < < Separated, endl stands for newline.

The biggest advantage of these two keywords is that there is no need to focus on the data type of the variable. C++ will automatically match the appropriate data type according to the definition of the variable.

You must include these two keywords when using them < iostream > This library.

4. Definition of variables

One of the most maligned rules in the C language is that the definition of a variable must be placed at the head of a block of code. This often results in the distance between the definition of a variable and its first use, as shown in the following code.


int main()
{
  int i, j;

  // Do something

  for (i = 0; i < 100; i++)
  {
    printf("%d\n", i);
  }

  return 0;
}

In this code, there may be a few 10 lines of code between the declaration of the variable i and its first use. This severely affects the readability of the code and increases the cost of debugging.

In C++, this situation is fundamentally resolved. C++ allows variables to be defined anywhere, as long as they are used before they are used. So here's how to write it:


for (int i = 0; i < 100; i++)
{
  // Do something
}

Does that make it very clear. In C++, we are used to using variables to define them, as are many programmers who only use C.cpp files are written for this convenience.

5. 1 must you learn C before you learn C++

Although I'm taking you through C first and then C++, many programmers follow the same path. But you don't have to learn C before you learn C++.

I always use this example to explain it. C and C++ are just like the difference between manual transmission and automatic transmission. Although most of us learn manual transmission first and then automatic transmission, it cannot be said that those who cannot drive manual transmission will not learn automatic transmission. Automatic transmission is actually better to learn 1. They have a lot in common when it comes to driving, the way you steer, the way you push the gas and brake, the way you turn the signal, the way you turn and line up, and so on, but you learn the common parts in the first place.

C and C++ are the same, more than 80% of the grammar points are the same, and it doesn't make any difference which language you learned them in.

Most schools schedule C before C++, because C is more detail-focused and helps students understand more clearly the relationship between programs and hardware. It is believed that this is the only way to lay a good foundation. But with the development of computer programming technology, the more popular view nowadays is that programming is designed to help us use computers better to solve problems. For this purpose, we can ignore what we don't need to know. More and more people are getting started with programming in hardware-free languages like C++, Java, and even Python. These people can still design the programs they want.

So, please don't worry about who to learn first and who to learn later. Maybe I will launch a series of learning C++ from scratch in the near future.

Stay tuned!


Related articles: