《C++ primer plus 英文版 第六版》Chapter 2
阿新 • • 發佈:2018-02-27
expec spa could review code clu body gre res
Chapter Review
- They are called functions.
- It causes the contents of the
iostream
file to be substituted for this directive before final compilation.
- It makes definitions made in the
std
namespace available to a program.
cout << "Hello, world" << endl;
orcout << "Hello, world\n";
int cheeses;
cheeses = 32;
cin >> cheeses;
cout << "We have " << cheeses << " varieties of cheese\n";
- The function
froop()
expects to be called with one argument, which will be typedouble
, and that the function will return a typeint
value. For instance, it could be used as follows:int gval = froop(3.14159);
The functionrattle()
has no return value and expects anint
argument. For instance, it could be used as follows:
rattle(37);
The functionprune()
returns anint
and expects to be used without an argument. For instance, it could be used as follows:
int residue = prune();
- You don‘t have to use
return
in a function when the function has the return typevoid
. However, you can use it if you don‘t give a return value:
return;
- The likely cause it that the function loses a statement
using namespace std;
to declare a directive of output.
placeusing namespace std;
above themain()
function;
placeusing std::cout;
in themain()
function;
type as following:using std::cout << "Please enter your PIN: " << std::endl;
Programming Exercises
1
#include <iostream>
int main()
{
using namespace std;
cout << "Name: NaRiSu;\n"
<< "Address: BeiJing;" << endl;
return 0;
}
2
#include <iostream>
int main()
{
using namespace std;
int furlong;
cin >> furlong;
cout << furlong << " furlong(s) = "
<< 220 * furlong << " yard(s)" << endl;
return 0;
}
3
#include <iostream>
void f1();
void f2();
using namespace std;
int main()
{
f1();
f1();
f2();
f2();
return 0;
}
void f1()
{
cout << "Three blind mice" << endl;
}
void f2()
{
cout << "See how they run\n"; // 也可以用f1()函數中的endl形式。
return; // void f2()表明無返回值,故可以這樣寫;也可以如f1()不寫return語句。
}
4
#include <iostream>
int main()
{
using namespace std;
cout << "Enter your age: ";
int age;
cin >> age;
cout << "Your age in months is " << 12 * age << "." << endl;
return 0;
}
5
#include <iostream>
double fahrenheit(double);
int main()
{
using namespace std;
cout << "Please enter a Celsius value: ";
int celsius;
cin >> celsius;
cout << celsius << " degrees Celsius is "
<< fahrenheit(celsius) << " degrees Fahrenheit." << endl;
return 0;
}
double fahrenheit(double n)
{
return 1.8 * n + 32.0;
}
6
#include <iostream>
double astronomical(double);
int main()
{
using namespace std;
cout << "Enter the number of light years: ";
double light;
cin >> light;
cout << light << " light years = "
<< astronomical(light) << " astronomical units." << endl;
return 0;
}
double astronomical(double n)
{
return 63240 * n;
}
7
#include <iostream>
void f(int, int);
int main()
{
using namespace std;
cout << "Enter the number of hours: ";
int hour;
cin >> hour;
cout << "Enter the number of minutes: ";
int minute;
cin >> minute;
f(hour, minute);
return 0;
}
void f(int h, int m)
{
using namespace std;
cout << "Time: " << h << " : " << m << endl;
}
《C++ primer plus 英文版 第六版》Chapter 2