c++编程实例

1、

#include
using namespace std;
int main()
{cout << "Hello world!" << "\n";return 0;
}

结果

Hello world!
2、
#include
#include
using namespace std;extern int a, b;
extern int c;
extern float f;
int main()
{//定义变量int a, b;int c;float f;//实际初始化a = 10;b = 20;c=a+b;cout << c << endl;f = 70.0/3.0;cout << f << endl;return 0;
}

结果:

30
23.3333

3、

#include
using namespace std;
#define LENGTH 10
#define WIDTH 5
//#define NEWLINE '\n'int main()
{int area;area = LENGTH * WIDTH;cout << area;
//	cout << NEWLINE;return 0;
}

结果:

50

3、常量定义demo2

int main()
{const int LENGTH = 10;const int WIDTH = 5;int area;area = LENGTH * WIDTH;cout << area;
//	cout << NEWLINE;return 0;
}

结果:

50

4、static存储类用法

void func(void);
static int count = 10;
int main()
{while(count--){func();}return 0;
}
void func(void)
{static int i = 5;i++;std::cout << "变量i为" << i;std::cout << ",变量count为" << count << std::endl;
}

结果:

变量i为6,变量count为9
变量i为7,变量count为8
变量i为8,变量count为7
变量i为9,变量count为6
变量i为10,变量count为5
变量i为11,变量count为4
变量i为12,变量count为3
变量i为13,变量count为2
变量i为14,变量count为1
变量i为15,变量count为0
static int i = 5;

改为

int i = 5;

结果:

变量i为6,变量count为9
变量i为6,变量count为8
变量i为6,变量count为7
变量i为6,变量count为6
变量i为6,变量count为5
变量i为6,变量count为4
变量i为6,变量count为3
变量i为6,变量count为2
变量i为6,变量count为1
变量i为6,变量count为0

4、extern存储类

main.cpp

#include int count;
extern void write_extern();int main()
{
count = 5;
write_extern();
}

support.cpp

#include extern int count;void write_extern(void)
{
std::cout << "Count is" << count << std::endl;
}

运行:

$ g++ main.cpp support.cpp -0 write
$ ./write
结果:
Count is 5

5、循环

while语句

int main()
{int a = 10;while( a < 20 ){cout << a << endl;a++;}
}

结果:

10
11
12
13
14
15
16
17
18
19

for语句(基于范围的)

int my_array[5] = {1, 2, 3, 4, 5}; // 每个数组元素乘于 2 
for (int &x : my_array) 
{ x *= 2; cout << x << endl; 
} // auto 类型也是 C++11 新标准中的,用来自动获取变量的类型 for (auto &x : my_array) { x *= 2; cout << x << endl; }

结果:

/tmp/381229462/main.cpp:29:1: error: expected unqualified-id
for (int &x : my_array) 
^
1 error generated.exit status 1

原因:

程序运行是从main函数入口执行的,把程序语句放在全局空间里不被执行。要放在某个函数里或宏里等等


rang for 遍历字符串,将小写变成大写

#include 
#include 
#include 
using namespace std;
//string str("guo zehui");
string str = "guozehui";
int main()
{for (auto &c : str){c = toupper(c);}cout << str << endl;
}

结果:

GUOZEHUI

#include 的函数

6、生成随机数

#include 
#include 
#include 
using namespace std;int main()
{int i,j;srand( (unsigned)time( NULL ));for (i = 0; i < 10; i++){j=rand();cout << j << endl;}
}

结果:

1338961599
1566172925
1291431620
1754761110
805075681
329934179
1769264811
1209019267
1283590728
579234732

先调用srand()函数,再使用rand()返回随机数。

7、class类的定义

#include 
using namespace std;class Box
{public:double length;double breadth;double heigth;
};
int main()
{Box box1;Box box2;double volume = 0.5;box1.length = 1;box1.breadth = 2;box1.heigth = 3;box2.length = 4;box2.breadth = 5;box2.heigth = 6;volume = box1.length*box1.breadth*box1.heigth;cout << volume << endl;
}

结果:

6

class定义成员函数

#include 
using namespace std;class Box
{public:double length;double breadth;double heigth;double getVolume(void){return length*breadth*heigth;}void setLength(double len){length = len;}void setBreadth(double bre){breadth = bre;}void setHeigth(double hei){heigth = hei;}
};
int main()
{Box box1;Box box2;double volume = 0.5;box1.setLength(1.0);box1.setBreadth(2.0);box1.setHeigth(3.0);volume = box1.getVolume();cout << volume << endl;
}

结果:

6


疑点:

1、explicit构造函数

2、string str ("guozehui");跟string str = "guozehui";区别

C++中的string str问题.

string是个类,string str,是定义一个名叫str的字符串对象。str内部保存着字符串的内容,通过str.c_str()可以获取这个字符串的首地址。
string str = "ABC",这也不是将str赋值为"ABC"的首地址,而是使用"ABC"为值来初始化一个string类。
在字符串变量中存放的是字符串的指针(即字符串的地址)。在vc++6.0环境下,string变量在内存中占4个字节,指针变量在内存中也是占4个字节,它们都用于存放变量的首地址。

字符串的首地址不是字符串的第一个字符!字符串的首地址是字符串的第一个字符的地址,这第一个字符的地址的内容才是这个字符串的第一个字符。

4、函数定义中return_type,当无返回值时,用void

5、main函数的参数

包含一个整型和一个指针数组

形式:

int main(int argc, char* argv[])

6、&x,引用调用,将引用的地址复制给形式参数,修改形参会影响实际参数。

7、ctime

#include 包含有什么函数??

定义time_t型t变量

time_t t;

7、定义静态常量

C++中如何在类中定义静态const的string?

只有静态整数(注意,不仅仅是整型)常量可以在类中声明并初始化,其他的都必须在类外初始化。
8、

关于C++的class中的public,protected,private;

要给“别人”使用的变量和函数就用public
只给自己使用的变量和函数就用private
protected。。。这个只能说概念了。。。就是想要允许他的子类直接访问,就用protected

8、类::成员(参数)

eg:Box::get(double len);

9、#include <>与#include ""区别

include <>引用的是编译器的类库路径里面的头文件。

#include ""引用的是程序目录的相对路径中的头文件。

#include< > 和 #include” ” 的区别

10、.hpp(Header Plus Plus)c++程序头文件格式

.hpp与.h区别


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部