欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c 语言 基本概念_C ++的基本概念

程序员文章站 2024-02-02 15:12:46
...

c 语言 基本概念

C ++的基本概念 (Basic Concepts of C++)

In this section we will cover the basics of C++, it will include the syntax, variable, operators, loop types, pointers, references and information about other requirements of a C++ program. You will come across lot of terms that you have already studied in C language.

在本节中,我们将介绍C ++的基础知识,包括语法,变量,运算符,循环类型,指针,引用以及有关C ++程序其他要求的信息。 您会遇到很多用C语言学习过的术语。

C ++程序的语法和结构 (Syntax and Structure of C++ program)

Here we will discuss one simple and basic C++ program to print "Hello this is C++" and its structure in parts with details and uses.

在这里,我们将讨论一个简单且基本的C ++程序,以打印“您好,这是C ++”及其部分结构,并附带详细信息和用法。



第一个C ++程序 (First C++ program)

#include <iostream.h>
using namespace std;
int main()
{
    cout << "Hello this is C++";
}

Header files are included at the beginning just like in C program. Here iostream is a header file which provides us with input & output streams. Header files contained predeclared function libraries, which can be used by users for their ease.

像C程序一样, 头文件也包含在开头。 iostream是一个头文件,可为我们提供输入和输出流。 头文件包含预定义的函数库,用户可以方便使用。

Using namespace std, tells the compiler to use standard namespace. Namespace collects identifiers used for class, object and variables. NameSpace can be used by two ways in a program, either by the use of using statement at the beginning, like we did in above mentioned program or by using name of namespace as prefix before the identifier with scope resolution (::) operator.

使用名称空间std ,告诉编译器使用标准名称空间。 命名空间收集用于类,对象和变量的标识符。 可以在程序中以两种方式使用NameSpace,或者像在上面提到的程序中那样在开头使用using语句,或者通过使用名称空间的名称作为具有作用域解析(::)运算符的标识符之前的前缀。

Example: std::cout << "A";

示例: std::cout << "A";

main(), is the function which holds the executing part of program its return type is int.

main()是保存程序执行部分的函数,其返回类型为int

cout <<, is used to print anything on screen, same as printf in C language. cin and cout are same as scanf and printf, only difference is that you do not need to mention format specifiers like, %d for int etc, in cout & cin.

cout << ,用于在屏幕上打印任何内容,与C语言中的printf相同。 cincoutscanfprintf相同,唯一的区别是您不需要在coutcin提及格式说明符,例如int等的%d

C ++程序中的注释 (Comments in C++ Program)

For single line comments, use // before mentioning comment, like

对于单行注释,请在提及注释之前使用// ,例如

cout<<"single line";   // This is single line comment
For multiple line comment, enclose the comment between /* and / **/ * /之间
/*this is 
  a multiple line 
  comment */

用C ++创建类 (Creating Classes in C++)

Classes name must start with capital letter, and they contain data variables and member functions. This is a mere introduction to classes, we will discuss classes in detail throughout the C++ tutorial.

类名称必须以大写字母开头,并且它们包含数据变量和成员函数。 这仅仅是对类的介绍,我们将在整个C ++教程中详细讨论类。

class Abc
{
    int i;           //data variable
    void display()         //Member Function
    { 
        cout << "Inside Member Function";
    }
}; // Class ends here

int main()
{
    Abc obj;  // Creatig Abc class's object
    obj.display();  //Calling member function using class object
}

This is how a class is defined, once a class is defined, then its object is created and the member functions are used.

这是定义类的方式,一旦定义了类,便创建其对象并使用成员函数。

Variables can be declared anywhere in the entire program, but must be declared, before they are used. Hence, we don't need to declare variable at the start of the program.

变量可以在整个程序中的任何位置声明,但必须在使用它们之前声明。 因此,我们不需要在程序开始时声明变量。

Don't worry this is just to give you a basic idea about C++ language, we will cover everything in details in next tutorials.

不用担心,这只是给您有关C ++语言的基本概念,我们将在下一个教程中详细介绍所有内容。

翻译自: https://www.studytonight.com/cpp/basics-of-cpp.php

c 语言 基本概念