C++中的变量使用详细指南

2021年3月12日13:01:45 发表评论 682 次浏览

变量是赋予存储位置的名称。它是程序中存储的基本单位。

  • 可以在程序执行期间更改存储在变量中的值。
  • 变量只是赋予存储位置的名称, 对变量执行的所有操作都会影响该存储位置。
  • 在C ++中, 必须在使用前声明所有变量。

如何声明变量?

典型的变量声明的形式为:

// Declaring a single variable
type variable_name;

// Declaring multiple variables:
type variable1_name, variable2_name, variable3_name;

变量名称可以由字母(大写和小写), 数字和下划线" _"字符组成。但是, 名称不能以数字开头。

C ++中的变量1

在上图中,

数据类型:可以存储在此变量中的数据类型。 variable_name:给变量的名称。值:它是存储在变量中的初始值。

例子:

// Declaring float variable
float simpleInterest; 

// Declaring integer variable
int time, speed; 

// Declaring character variable
char var;

变量声明和定义之间的区别

的变量声明指变量在首次使用之前首先声明或引入的部分。一种变量定义是为变量分配存储位置和值的部分。大多数情况下, 变量声明和定义是一起完成的。

请参阅以下C ++程序以获得更好的说明:

#include <iostream>
using namespace std;
  
int main()
{
     // declaration and definition
     // of variable 'a123'
     char a123 = 'a' ;
  
     // This is also both declaration and definition
     // as 'b' is allocated memory and
     // assigned some garbage value.
     float b;
  
     // multiple declarations and definitions
     int _c, _d45, e;
  
     // Let us print a variable
     cout << a123 << endl;
  
     return 0;
}

输出如下:

a

变量类型

根据以下三种类型的变量C ++中变量的范围:

  • 局部变量
  • 实例变量
  • 静态变量
C ++中的变量2

现在让我们详细了解这些变量中的每一个。

局部变量

:在块, 方法或构造函数中定义的变量称为局部变量。

  • 这些变量是在输入的块或从该块退出后调用并销毁该函数或从函数返回该调用时创建的。
  • 这些变量的范围仅存在于声明该变量的块内。即我们只能在该块中访问这些变量。
  • 局部变量的初始化是强制性的。

示例程序1:

// C++ program to demonstrate Local variables
  
#include <iostream>
using namespace std;
  
void StudentAge()
{
     // local variable age
     int age = 0;
     age = age + 5;
     cout << "Student age is : " << age;
}
  
// Driver code
int main()
{
     StudentAge();
}

输出如下:

Student age is : 5

在上面的程序中, 变量年龄是一个局部变量到功能StudentAge()。如果我们在StudentAge()函数外部使用变量age, 则编译器将生成一个错误如以下程序所示。

示例程序2:

// C++ program to demonstrate Local variables
  
#include <iostream>
using namespace std;
  
void StudentAge()
{
     // local variable age
     int age = 0;
     age = age + 5;
}
  
// Driver code
int main()
{
     StudentAge();
     cout << "Student age is : " << age;
}

实例变量

:实例变量是非静态变量, 并且在任何方法, 构造函数或块之外的类中声明。

  • 当实例变量在类中声明时, 这些变量在创建类的对象时创建, 而在对象被销毁时被销毁。
  • 与局部变量不同, 我们可以对实例变量使用访问说明符。如果我们未指定任何访问说明符, 则将使用默认的访问说明符。
  • 实例变量的初始化不是强制性的。
  • 只能通过创建对象来访问实例变量。

样例程序:

// C++ program to demonstrate Local variables
  
#include <iostream>
using namespace std;
  
class Marks {
  
public :
     // This is a class variable
     static int studentNumber;
  
     // These variables are instance variables.
     // These variables are in a class
     // and are not inside any function
     int engMarks;
     int mathsMarks;
     int phyMarks;
  
public :
     Marks()
     {
  
         // Modify the class variable
         ++studentNumber;
     };
};
  
// Setting the class variable of Marks
int Marks::studentNumber = 0;
  
// Driver code
int main()
{
  
     // first object
     Marks obj1;
     obj1.engMarks = 50;
     obj1.mathsMarks = 80;
     obj1.phyMarks = 90;
  
     // second object
     Marks obj2;
     obj2.engMarks = 80;
     obj2.mathsMarks = 60;
     obj2.phyMarks = 85;
  
     // displaying marks for first object
     cout << "Marks for first object:\n" ;
     cout << Marks::studentNumber << endl;
     cout << obj1.engMarks << endl;
     cout << obj1.mathsMarks << endl;
     cout << obj1.phyMarks << endl;
  
     // displaying marks for second object
     cout << "Marks for second object:\n" ;
     cout << Marks::studentNumber << endl;
     cout << obj2.engMarks << endl;
     cout << obj2.mathsMarks << endl;
     cout << obj2.phyMarks << endl;
}

输出如下:

Marks for first object:
2
50
80
90
Marks for second object:
2
80
60
85

如你在上面的程序中看到的变量, 英文商标, 数学标记, 物理标记是实例变量。如果上面的程序中有多个对象, 则每个对象将有其自己的实例变量副本。从上面的输出中可以清楚地看到, 每个对象都有自己的实例变量副本。

静态变量

:静态变量也称为类变量。

  • 这些变量的声明方式与实例变量类似, 不同之处在于, 静态变量是使用静态关键字在任何方法构造函数或块之外的类中。
  • 与实例变量不同, 无论我们创建多少个对象, 每个类只能有一个静态变量的副本。
  • 静态变量在程序执行开始时创建, 并在执行结束时自动销毁。
  • 静态变量的初始化不是强制性的。其默认值为0
  • 如果我们通过对象访问诸如实例变量之类的静态变量, 则编译器将显示警告消息, 并且不会停止程序。编译器会自动将对象名称替换为类名称。
  • 如果我们访问不带类名的静态变量, 则编译器将自动附加类名。

要访问静态变量, 我们不需要创建该类的对象, 我们可以简单地以以下方式访问变量:

class_name::variable_name;

样例程序:

// C++ program to demonstrate Static variables
  
#include <iostream>
using namespace std;
  
class Marks {
  
public :
     // This is a class variable
     static int studentNumber;
  
     // These variables are instance variables.
     // These variables are in a class
     // and are not inside any function
     int engMarks;
     int mathsMarks;
     int phyMarks;
  
     Marks()
     {
  
         // Modify the class variable
         ++studentNumber;
     };
};
  
// Setting the class variable of Marks
int Marks::studentNumber = 0;
  
// Driver code
int main()
{
  
     // object of Marks
     Marks obj1;
     obj1.engMarks = 50;
     obj1.mathsMarks = 80;
     obj1.phyMarks = 90;
  
     // displaying marks for first object
     cout << "Marks for object:\n" ;
  
     // Now to display the static variable, // it can be directly done
     // using the class name
     cout << Marks::studentNumber << endl;
  
     // But same is not the case
     // with instance variables
     cout << obj1.engMarks << endl;
     cout << obj1.mathsMarks << endl;
     cout << obj1.phyMarks << endl;
}

输出如下:

Marks for object:
1
50
80
90

实例变量Vs静态变量

  • 每个对象都有其自己的副本的实例变量, 而我们只能一本每个类的静态变量的大小, 无论我们创建多少个对象。
  • 使用一个对象在实例变量中所做的更改将不被反映在其他对象中, 因为每个对象都有自己的实例变量副本。如果是静态的, 则进行更改将被反映在其他对象中, 因为静态变量是类的所有对象共有的。
  • 我们可以访问实例变量通过对象引用和静态变量可以访问直接使用类名。
  • 静态变量和实例变量的语法:
    class Example
    {
        static int a; // static variable
        int b;        // instance variable
    }
    

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C ++ STL通过激烈的问题解决过程来训练和掌握这些概念。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: