**第三章 使用函数和类进行自顶向下设计**
3.1 根据已有信息构建函数
例子:1.求圆的面积和周长
//Computes and displays the area and circumference of a circle
#include<iostream>
using namespace std;
int main()
{
const float PI=3.14159;
float radius; //输入圆的半径
float area; //输出圆的面积
float circum; //输出圆的周长
//得到圆的半径
cout<<"Enter the circle: ";
cin>>radius;
//计算圆的面积
area=PI*radius*radius;
//计算圆的周长
circum=2*PI*radius;
//显示圆的面积和周长
cout<<"The area of the circle is "<<area<<endl;
cout<<"The circum of the circle is "<<circum<<endl;
return 0;
}
2.计算一批平垫圈的重量
//计算一批平垫圈的重量
#include<iostream>
using namespace std;
int main()
{
const float PI=3.14159;
float holeDiameter; //输入洞的直径
float edgeDiameter; //输入边缘的直径
float thickness; //输入平垫圈的厚度
float density; //输入使用材料的密度
float quantity; //输入作出平垫圈的数量
float weight; //输出平垫圈的重量
float holeRadius;//洞的半径
float edgeRadius; //边缘的半径
float rimArea; //边缘的面积
float unitWeight; //1个平垫圈的重量
//得到里面的直径,外面的直径和厚度
cout<<"Inner diameter in centimeters: ";
cin>>holeDiameter;
cout<<"Outer diameter in centimeters: ";
cin>>edgeDiameter;
cout<<"Thickness in centimeters: ";
cin>>thickness;
//得到材料密度和制作数量
cout<<"Material density in grams per cubic centimeter: ";
cin>>density;
cout<<"Quantity in batch: ";
cin>>quantity;
//计算边缘的面积
holeRadius=holeDiameter/2.0;
edgeRadius=edgeDiameter/2.0;
rimArea=PI*edgeRadius*edgeRadius-PI*holeRadius*holeRadius;
//计算一个平垫圈的重量
unitWeight=rimArea*thickness*density;
//计算平垫圈的重量
weight=unitWeight*quantity;
//显示平垫圈的重量
cout<<"The expected weight of the batch is "<<weight<<" grams."<<endl;
return 0;
}
3.2 库函数
例子:c++中的sqrt函数的使用
#include<cmath> //sqrt function
#include<iostream> //i/0 functions
using namespace std;
int main()
{
float first; //输入:二个数据值之一
float second; //输入:二个数据值之二
float answer; //输出:平方根值
//获取第一个数字并显示其平方根
cout<<"Enter the first number: ";
cin>>first;
answer=sqrt(first);
cout<<"The square root of the first number is "<<answer<<endl;
//获取第二个数字并显示其平方根
cout<<"Enter the second number: ";
cin>>second;
answer=sqrt(second);
cout<<"The square root of the second number is "<<answer<<endl;
//显示第一个和第二个和的平方根
answer=sqrt(first+second);
cout<<"The square root of the sum of both numbers is "<< answer <<endl;
return 0;
}
3.2.1 程序风格——加下划线
3.2.2 c++库函数
3.3 自顶向下设计和结构图
自顶向下设计:一种问题解决方法。程序员用此方法把问题分解成几个主要的子问题,通过子问题的解决方案导出原始问题的解决方案。
结构图:一种表示一个问题的各子问题之间关系的文档工具。
3.4无参函数
例子:画线条图形的函数原型和main函数
#include<iostream>
using namespace std;
//Functions used...
void drawCircle(); //画一个圆圈
void drawTriangle(); //画一个三角形
void drawIntersect(); //画一个相交线
void drawBase(); //画一个水平线
int main()
{
//画一个圆
drawCircle();
//画一个三角形
drawTriangle();
//画一个相交线
drawIntersect();
return 0;
}
格式:fname();
3.4.1函数原型
格式:ftype fname();
注意:若函数不返回任何值,则ftype为void。参数列表“()”表示函数无参数。函数原型必须出现在函数的第一次调用之前。建议在main函数之前给出所有函数的原型。
3.4.2函数定义
语法:ftype fname()
{
局部声明
可执行语句
}
注意:若函数不返回任何值,则ftype为void。参数列表“()”表示函数无参数。
3.4.3函数在程序中的布局
例子:“画线条画”程序
#include<iostream>
using namespace std;
//Functions used...
void drawCircle(); //画一个圆圈
void drawTriangle(); //画一个三角形
void drawIntersect(); //画一个相交线
void drawBase(); //画一个水平线
int main()
{
//画一个圆
drawCircle();
//画一个三角形
drawTriangle();
//画一个相交线
drawIntersect();
return 0;
}
//画一个圆圈
void drawCircle()
{
cout<<" * "<<endl;
cout<<" * *"<<endl;
cout<<" ** "<<endl;
}//结束画圆圈
//画一个三角形
void drawTriangle()
{
drawIntersect();
drawBase();
}//结束画三角形
//画相交线
void drawIntersect()
{
cout<<" /\\ "<<endl;
cout<<" / \\ "<<endl;
cout<<" / \\"<<endl;
}//结束相交线
//画一个水平线
void drawBase()
{
cout<<"_ _ _ _ _ _" <<endl;
}//结束水平线
3.4.4程序风格——在函数声明和定义中的使用注释
例子:// end fname 用于表明fname函数的结束
3.4.5函数的执行顺序
当程序运行时,主函数中的第一个语句最先执行。计算机在执行函数调用语句时,执行控制权转移给被调用函数。当调用函数执行完后,执行控制权交回main函数。
3.4.6使用函数子程序的优点
过程抽象:一种程序设计技术。main函数中包含函数调用序列,每个函数再单独实现。
例子:编写函数显示计算圆面积和周长程序的用户操作指南。
#include<iostream>
using namespace std;
void instruct();
int main()
{
instruct();
return 0;
}
void instruct()
{
cout<<"This program computes the area and "<<endl;
cout<<"circumference of a circle. "<< endl <<endl;
cout<<"To use this program, enter the radius of the "<<endl;
cout<<"circle after the prompt"<< endl << endl;
cout<<"Enter the circle radius: "<< endl << endl;
cout<<"The circumference will be computed in the same"<<endl;
cout<<"units of measurement as the radius.The area "<<endl;
cout<<"will be computed in the same units squared."<<endl<<endl;
}// end instruct()
3.5带输入参数的函数
输入参数:把信息传入函数的参数。
输出参数:把函数返回结果的参数。
3.5.1带输入参数的void函数
在调用函数后的括号里加入数据类型。
3.5.2带参数并有一个返回值
不要用void函数,括号后面加数据类型。
3.5.3程序风格——函数接口注释
前提函数:函数调用前假定为真的条件。
后续函数:函数执行后假定为真的条件。
3.5.4程序风格——问题输入与输入参数
问题输入:通过执行输入语句,接收程序用户输入数据的变量。
输入参数:通过执行函数调用语句来接收数据的。
3.5.5带多个参数的函数
形参和实参。
3.5.6实参和形参列表的对应
3.5.7函数的数据区
3.5.8使用驱动程序测试函数
驱动函数:为测试其他函数而定义的简短函数,驱动函数中要给出被测函数的实参,调用被测函数并显示调用结果。
3.6名称作用域
名称作用域:程序中有特定含义的名称可见或可引用的范围。
3.7通过类扩展c++:使用string类
3.7.1string类
例子:string的操作说明
#include<iostream>
#include<string>
using namespace std;
int main()
{
string firstname,lastname; //输入:第一个和第二个名字
string wholename; //输出:全部的名字
string greeting ="Hello "; //输出一个问候的string
//读第一个和第二个名字
cout<<"Enter your first name: ";
cin>> firstname;
cout<<"Enter your last name: ";
cin>>lastname;
//加入名字到全部名字中
wholename=firstname+" "+lastname;
//显示结果
cout<<greeting<<wholename<<'!'<<endl;
cout<<"You have "<<(wholename.length()-1)<<" letters in your name."<<endl;
//显示缩写
cout<<"Your initials are "<<firstname.at(0)<<lastname.at(0)<<endl;
return 0;
}
3.7.2声明string类
string firstname.//输入-名
3.7.3读取和显示string对象
cin>>firstname.
3.7.4字符串的赋值和连接
wholename=firstname+" “+lastname;
中间的” "是连接字符串值。c++中,表示字符串要用双引号。
3.7.5运算符重载
运算符重载:运算符能根据操作数数据类型完成不同操作的能力。
3.7.6点表示法
例子:firstname.at(0).
注意:成员函数可以改变对象的属性。