第二天笔记
1.5自有数据类型第二部分
friend:友元 derived class:派生类 base class:基类 protected:保护性成员
1.enum类型:
枚举常量只能以标识符形式表示,而不能是整型、字符型等文字常量。例如,以下定义非法:
enum letter_set {'a','d','F','s','T'}; //枚举常量不能是字符常量
enum year_set{2000,2001,2002,2003,2004,2005}; //枚举常量不能是整型常量
可改为以下形式则定义合法:
enum letter_set {a, d, F, s, T};
enum year_set{y2000, y2001, y2002, y2003, y2004, y2005};
2.被设定为friend的函数和类,该函数和类可以访问此类的私有成员。
3.derived class可以访问base class的共有成员,为了方便derived class对base class数据成员的访问,引入了protected,derived class 可以访问base class的protected。
4.头文件中常常包含了类的声明和实现细节,为了避免重复定义,应在此头文件的开头加上
#ifndef<标识>
#define<标识>
结尾加上
#endif
如此,文件就只会被编译一次。
1.6异常类
异常类的定义与使用:
#include<iostream>
#include<string>
using namespace std;
class illegalParameterValue //1.定义异常类
{
private:
string message;
public:
illegalParameterValue()://定义无参构造函数
message("Illegal parameter value"){}
illegalParameterValue(char *theMessage)//定义有参构造函数
{
message = theMessage;
}
void outputMessage(){//输出
cout<<message<<endl;
}
};
int abc(int a, int b, int c)
{
if(a <= 0 || b <= 0 || c <= 0)
throw illegalParameterValue("All parameter should be > 0"); //2.抛出异常,调用有参构造函数
return a + b * c;
}
int main()
{
try{
cout<<abc(2, 0, 4)<<endl; //3.使用try catch捕捉异常
}
catch(illegalParameterValue e)
{
cout<<"The parameters to abc were 2, 0, and 4"<<endl;
cout<<"illegalParameterValue exception thrown"<<endl;
e.outputMessage();
return 1;
}
}
综合代码:
#ifndef _CURRENCY_H #define _CURRENCY_H #include<iostream> #include<string> using namespace std; typedef enum signType{plus_, minus_}signType; class currency { public: //构造函数 friend ostream &operator<<(ostream&, const currency&); currency(signType theSign = plus_, unsigned long thheDollars = 0, unsigned int theCents = 0){} ~currency(){}//析构函数 void setValue(signType, unsigned long, unsigned int); void setValue(double);//函数重载 signType getSign() const { if(amount < 0)return minus_; else return plus_; } unsigned long getDollars() const { if(amount < 0)return (-amount)/100; else return amount/100; } unsigned int getCents()const { if(amount < 0)return -amount - getDollars() * 100; else return amount -getDollars() * 100; } currency operator+(const currency& x)const; currency operator+=(const currency& x) { amount += x.amount;return *this; } private: long amount; }; class illegalParameterValue { public: illegalParameterValue(): message("illegal parameter value"){ }; illegalParameterValue(char *str) { message = str; } void outputMessage() { cout<<message<<endl; } private: string message; }; #endif
#include <iostream> #include"currency.h" using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ currency currency::operator+(const currency& x)const//重载运算+ { currency result; result.amount = amount + x.amount; return result; } ostream &operator<<(ostream& out, const currency &x)//重载运算符<< { long theAmount = x.amount; if(theAmount < 0){ cout<<'-'; theAmount = -theAmount; } long dollars = theAmount/100; out<<'$'<<dollars<<'.'; int cents = theAmount - dollars * 100; if(cents < 10)out<<'0'; out<<cents; return out; } int main(int argc, char** argv) { currency g, h(plus_, 3, 50), i, j; //使用两种方式来赋值 g.setValue(minus_, 2, 25); i.setValue(-6.45); //调用成员函数add和output j = h + g; cout<<h<<"+"<<g<<"="<<j<<endl; //连续两次调用成员函数add j = i + g + h; cout<<i<<"+"<<g<<"+"<<h<<"="<<j<<endl; //调用成员函数increment和add cout<<"Increment"<<i<<"by"<<g<<"and then add"<<h<<endl; j = (i += g) + h; cout<<"Result is"<<j<<endl; cout<<"Incremented object is"<<i<<endl; //测试异常 cout<<"Attempting to initialize with cents = 152"<<endl; try { i.setValue(plus_, 3, 152); } catch(illegalParameterValue e) { cout<<"Caught thrown exception"<<endl; e.outputMessage(); } return 0; }
1.7递归
recursive function:递归函数 direct recursion:直接递归 indirect recursion:间接递归
base component:基础部分 recursive component:递归部分
递归输出排列:
#include<iostream>
#include<algorithm>
#include<iterator>
using namespace std;
template<class T>
void permutations(T list[], int k, int m)
{
if(k == m)//只有一个元素时直接输出
{
copy(list, list + m + 1, ostream_iterator<T>(cout, " "));
cout<<endl;
}
else//否则采用递归求解
{
for(int i = k; i <= m; i++)
{
swap(list[k], list[i]);
permutations(list, k + 1, m);
swap(list[k], list[i]);
}
}
}
template<class T>
swap(T &a, T &b)//交换两个元素
{
T c;
c = a;
b = a;
b = c;
}
int main()
{
int a[5] = {1, 2, 3, 5, 4};
permutations(a,0, 4);
return 0;
}
1.8标准模板库
1.accumulate(a, a+n, initialValue);//计算数组a[0:n-1]的和加上initialValue
2.accumulate(a, a+n, initialValue, multiplies<T>());//计算数组a[0:n-1]*initialValue的乘积,multiplies<T>(),可以换成其他的函数
3.copy(star, end, to)//元素从star到end-1复制到以to开始的位置
4.next_permutation(star, end, cmp),按cmp规则产生下一个排列,并返回true,没有下一个排列时返回false
1.9测试与调试
program testing:程序测试 test data:测试数据 test set:测试集
quadratic function:二次函数 root:根
black box method:黑盒法:(此处指I/O分类)把输入数据和输出数据分为若干类,不同类的数据使程序结果有质的不同,而相同的数据使程序结果在本质上类似。
White box method:白盒法:statement coverage语句覆盖、decision coverage:分支覆盖、clause coverage:从句覆盖、execution path coverage:执行路径覆盖
boundary condition:边界条件
debugging:调试
incremental testing and debugging:增量测试与调试