1. 随机数的生成:
srand( (unsigned)time( NULL ) );
int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)+range_min;//特定范围的数
或
srand((unsigned)time(NULL));
int ran = (double)rand()/(RAND_MAX+1)*1000;
2. 判断输入,使输入的数字大于1
while(!(cin>>c)||c<2)
{
cin.clear();
cin.ignore(1024,'\n');
cout<<"Not a legal input,Pleaseinput again:";
}
3. 文件操作
#include <fstream>
ofstream os("file.txt");
os << i<< endl;
4. 枚举变量和数组变量的对应,
enum color{red,yellow,blue,white,black};
string col[] = {"red","yellow","blue","white","black"} ;
5. 任意空格数的输出:
cout <<setiosflags(ios::left)<<setw(7*(i-1))<<"";//7*(i-1)个空格;
cout << setw(3)<<i*j;//输出占用特定位数的整数;
6. 构造函数的调用顺序:
1) 类A里面有类B的成员变量,则初始化A对象时,顺序为:成员B的构造->A的构造->A的析构->B的析构;
2) 如果A,B,C的派生关系如下:A->B->(派生)C;则调用顺序为:A的构造 、B的构造、C的构造、C的析构、B的析构、A的析构;
7. 对象是无法直接访问自己的私有成员变量的;公有派生也不能访问基类的私有成员?
8. 友元可以访问类的非公有成员,跟成员函数的访问权限一致;另外只在类声明中有关键字friend,在函数的实际定义部分没有此关键字;
9. 复制构造函数可以访问类的私有成员变量;?
10. 多维数组的动态分配和释放;
array = new int*[lines];
for(int i = 0;i<lines;i++)
array[i] = new int[rows];
for(int i = 0;i <lines;i++)
{
delete [] array[i];
array[i] = NULL;
}
delete []array;
11. A类中有一个变量area和GetArea()函数,A->B->C,B和C对GetArea()不改写但都重写area变量,C调用GetArea,返回的是谁中的值;
12. 动态联编规定,只能通过指向基类的指针或基类对象的引用来调用虚函数,
13. 重载操作符:
1) 自加自减操作符;
Point& operator ++(); //重载++前置运算符
Point operator ++ (int ); //重载++后置运算符
2Point& Point::operator--()
{
this->x--;
this->y--;
return *this;
}
PointPoint::operator++(int)
{
return Point(this->x++,this->y++);
}
2) 运算:+操作符能否直接访问b的私有成员呢;
Point operator+(Point&b);
Point operator-(Point &b);
3) 2
14. 往文件中输入十进制,八进制,十六进制数字:
fOutput<<dec<<num<<endl<<oct<<num<<endl<<hex<<num<<endl<<endl;
15. 打开一个输入的文件名:
char* s = new char[100];
cin>> s;//cin.getline(s,100);
ofstreamf(s);
//f.open(s);
16. 从一个文件输入到另外一个文件:
ofstreamf("of.txt");
ifstreamin("f.txt");
while(in.getline(s,100))//in.eof();
{
f<< i++;
f<< s;
f<< '\n';
}
17. 文件中的定位和读写:
fstreamfile(file_name,ios::out|ios::in);
file.seekg((num-1)*sizeof(temp),ios::beg);
temp.num= num;
temp.used= 0;
file.write((char*)&temp,sizeof(temp));
fle.read((char*)&temp,sizeof(temp));
file.close();