C++ Primer Plus 第2章,开始学习c++

2、1进入c++

使用c++工具cout生成字符输出。

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    cout <<"you wont regret it!" << endl;
    return 0;
}

c++也能够使用printf()、scanf()和其他所有标准c输入输出函数,只需要包含常规c语言的stdio。h文件。

c++程序比如包含一个名为main()的函数。

c++注释://注释一行

/* */注释多行

如果程序要使用c++输入输出工具:提供这样两行代码

#include<iostream>

using namespace std;

#include<iostream>使用#include编译指令,将iostream文件的内容添加到程序中。

将源代码文件和iostream组合成一个复合文件,编译的下一阶段将使用该文件。

使用cin和cout进行输入输出必须包含iostream文件

名称空间:如果使用iostream而不是iostream.h则应该使用下面的名称空间编译指令来使iostream中的定义对程序可用:

using namespace std;

类、函数、变量时c++编译器的标准组件,他们现在被放在名称空间std中。仅当头文件中没有扩展名h时,情况才如此。

iostream中定义的cout变量实际是            std::cout           std::endl

可以省去using,用用下面方式编码:

std::cout<<"hello world !" << std::end;

2、2c++语句

变量声明:

int a = 0;它将为变量声明内存空间。

cin >> 变量名

复习题:

1、c++程序的模块叫什么?
函数

2、

包含iostream头文件,将头文件内容添加到源代码中

 3、

using预编译指令, 使用命名空间std

4、 

cout << "hello world " << endl;

5、 

int cheeses

6、 

int cheeses = 32;

7、 

cin >> cheeses;

8、 

cout << "we have" << X <<"varieties of cheese" << endl;

9、

创建了一个返回值为int函数名是froop传入参数是double类型的函数

创建了一个无返回值,传入参数是int 名字为rattle的函数

创建了一个返回值为int类型的无参 名为prune的函数。

10、当函数无返回值时

 11、

 1、using namepace std;

2、std::cout

3、using std::cout

编程练习:

1、

#include <iostream>

using namespace std;

int main()
{
    cout << "我的名字是xxx" << endl;
    cout <<"我居住在xxx" << endl;
    return 0;
}

 2、

#include <iostream>

using namespace std;

int main()
{
    double distance;
    cout << "请输入一个用long为单位的距离" << endl;
    cin >> distance;
    cout <<"long距离对应的码="<<220*distance << endl;
    return 0;
}

3、

#include <iostream>

using namespace std;

void print_mice(void);
void print_run(void);

int main()
{
    print_mice();
    print_run();
    print_mice();
    print_run();

    return 0;
}
void print_mice(void)
{
    cout << "three blind mice" << endl;

}

void print_run(void)
{

cout << "see how they run " << endl;

}

4、

#include <iostream>

using namespace std;



int main()
{

    int age;
    cout << "请输入你的年龄" << endl;
    cin >> age;
    cout << "你已经出生"<< age*12 << "个月了!!"<< endl;
    return 0;
}

 5、

#include <iostream>

using namespace std;

double fanhui( double a);

int main()
{

    double celsius;
    cout << "please enter a celsius value : ";
    cin >> celsius;
    double fahrenheit;
    fahrenheit = fanhui(celsius);
    cout << celsius << " degree celsius is "<< fahrenheit << " fashrenheit"<< endl;
    return 0;
}
double fanhui( double a)
{

    return (1.8*a+32);

}

6、

#include <iostream>

using namespace std;

double fanhui( double a);

int main()
{

    double light;
    cout << "enter the number of light years : ";
    cin >> light;
    double units;
    units = fanhui(light);
    cout << light << " light years = "<< units << " astronomical units"<< endl;
    return 0;
}
double fanhui( double a)
{

    return (a*63240);

}

7、

#include <iostream>

using namespace std;

void fanhui(int a ,int b);

int main()
{

    int hours,minutes;
    cout << "enter the number of hours : ";
    cin >> hours;
    cout << "enter the number of minutes : ";
    cin >> minutes;
    fanhui(hours,minutes);
    return 0;
}
void fanhui( int a ,int b)
{

   cout <<"time: " << a <<":" << b <<endl;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值