2.
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
double prices[5] = {5.00,10.00,1.87,7.99,8.49};
for (double x : prices)
cout << x << std::endl;
for (double &x : prices)
{
x = x*0.80; //20% off sale
}
for (double x : prices)
cout << x << std::endl;
return 0;
}
- 类中的静态变量
由于声明为static的变量只被初始化一次,因为它们在单独的静态存储中分配了空间,因此类中的静态变量**由对象共享。**对于不同的对象,不能有相同静态变量的多个副本。也是因为这个原因,静态变量不能使用构造函数初始化。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
class Apple
{
public:
static int i;
Apple()
{
// Do nothing
};
};
int Apple::i = 1;
int _tmain(int argc, _TCHAR* argv[])
{
int j;
cout << "fuck you !" << endl;
Apple obj1;
Apple obj2;
obj1.i = 2;
obj2.i = 3;
cout << obj1.i << " " << obj2.i;
cin >> j;
return 0;
}