1:第一章 :C++ 环境安装 (略)
2:开始学 C++
2.1 : 使用cout 来
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
// 预处理指令
#include <iostream>
using namespace std;
// 对于库函数,需要定义这个函数的原型 prototype
void add(int, int);
// 定义一个函数:输入用户的年龄,输出用户的月数
int calculateMonth(int);
int main()
{
// C++ 大学基本知识
// 1:使用 cout 进行输出 (将一条字符串插入到输出流中:cout 表示这种流,其名字位于 std 这个空间 << 表示插入运算符 将右边信息插入到输入流中)
std:: cout << "hello world C++ \n";
// 1.1 : 使用 cin 从输出流中抽取数据 ,通过 >>运算符 从输入流中抽取数据
int money;
cout << "请在屏幕上输入money : ";
cin >> money;
// 等待用户输入money
cout << "抽取用户输入的 money:" << money;
// 2: 控制符 endl (endl是 C++的一个特殊符号,标识重起一行,它在 iostream中定义,且位于名称空间 std中)
std::cout << std::endl;
// 显然如果每次使用一个对象或者特殊符号,都需要给予命名空间std 很麻烦,我们可以使用 using namespace std;表示
cout << "欢迎来到C++世界";
// 3: 使用 cout 进行拼接
cout << endl;
cout << "你好" << "欢迎来到C++世界 \n";
// 4: 使用 printf 输出
printf("使用printf函数打印 \n");
std::cout << std::endl;
// 5: 函数学习---标准函数:sqrt 求平方根
int x;
x = sqrt(4);
cout << "4的平方根是:" << x;
// 5: 函数学习---标准函数:函数变体,pow (接受两个参数,返回值为:第一个参数为底,第二个参数为指数的幂)
std::cout << std::endl;
int y;
y = pow(3, 2);
cout << "3的2次方是:" << y;
add(3, 4);
std::cout << std::endl;
// 6:习题
int year;
cout << "请在屏幕上输入年龄 : ";
cin >> year;
// 等待用户输入year
cout << "抽取用户输入的 year:" << year;
// 计算 year对于的月数
cout << "计算用户的年龄对应的月数:" << calculateMonth(year);
}
// 5:函数学习 ----用户自定函数
void add(int i, int j) {
std::cout << std::endl;
cout << "这是一个求和函数求函数参数之后:" << i + j;
}
// day01 总结
// 1: 什么是库函数,为什么自定义函数需要在做一个原型声明
// 2: iostream理解,定义空间,名称空间
// 3: C++标准输出,换行
// 4:cout cin 这两个对象的功能.
// 5: 习题:编写一个程序,让用户输入年龄,然后输出他出生了几个月
int calculateMonth(int month) {
if (month <= 0)
{
return 0;
}
return month * 12;
}