1. function overloading: writetofile ()
C++程序的命名规则
C++输入和输出
C++布尔类型(bool)
C++定义变量的位置
C++函数的缺省参数
C++动态内存分配
In the C language, dynamic memory allocation with malloc () function to release memory using free () function
C++函数重载
above wording is C, the following is written C ++ function overloading
function overloading rules: 1. The name of the function must be the same, 2 parameter list must. different (not just different function names).
How do C ++ is function overloading: C ++ code to rename the function at compile time based on the parameter list
2. Class and Object: vfprintf ()
Full above, the following two lines in the uplink to improve the downlink, the same effect, but does not cover the object class and
the following two functions with the same name show achieve function overloading
structure upgraded to Class
below into three classes do not show all statements relate to the structure, struct st_girl -> class CGirl
following three kinds of calls Show
below show three kinds of function overloading implemented as strings also be defined as char name [10], name is a character array name itself is a character pointer, char name not , char type is a single character, so a char *, think strcpy function
object pointer: class is a custom data type, the object is a variable memory, there are memory addresses, of course, will have a pointer to the class.
Defined pointer to this address, the Girl.m_name (class.) Replace pst-> m_name
object array: an array of objects may be defined as an object, and essentially no difference between other types of array variable
objects as a parameter: Structure and Like the body, the object can be passed as parameters, is the best way to pass the address of the object, as ox123 ... & * must be
below the pst and Girl is the same, just different names
initialization and memory-intensive objects the size of
object-oriented programming not on code execution efficiency of any advantage, its main purpose is to facilitate the programmer to organize and manage code, C language is process-oriented, does not support the concept of classes and objects. C as a function of the package, the C ++ class is more than one package, it is the C ++ object-oriented
access to members of a class
named member variables
constructor
CFile a class, CFile () is the function, not the object (). Following return 0; return directly
The following two constructors (when the class object is created, compiled object allocation system memory, and automatically call the constructor, the completion of initialization by the constructor members), are members of the function
when the object is instantiated as CFile File selected by the parameter which constructor
destructor
constructors can be overloaded, a class can have multiple constructors
on knowledge of other classes, such as this pointer, static static member, friend, etc. temporarily introduced. The following are all CFile class member function () to achieve
the following rewrite fprintf (...), call vfprintf (...) to write data to the open file
va_list pointer, va_start macro, va_end macro, vfprintf (...) The macro analysis outputs the result to a file
extraction member functions realized by the above Fprintf following variable parameters, man sprintf as not function overloading, ellipsis variable parameters
man vfprintf shown below, the figures above function is not a function of the following functions (the va_list analysis the results output to the screen, file ...)
3. Quote: Alias
Like reference aliases, typedef, macro define. Operation and direct operation of the variable identical reference
declaration method references: type identifier reference name = & target variable name; product
reference may be modified by const, for read-only, this reference declaration, not by reference to the target value of the variable is modified
with a referenced structure, but does not refer to an array of
transmission addresses and references are confused
4.string类:string str,str=,str.c_str()
The overloaded operator string: = direct assignment, ==,>, <,> =, <=, =, and compare strings, + + = operator to connect or two strings, [] acquired specific character! array-like
string characterization function: int size (); // return the current size of the string
int length (); // return the current length of the string
void clear (); // empty string
string is essentially a class, dynamic allocating memory to achieve the storage of the character string, string objects memory for storing a character address is varied. That is no longer at the address stored redistribution can not fit on the re-allocation of addresses.
Recommended string to store temporary data dynamically allocated memory, avoiding the pit to bring dynamic memory technology, and then converted to a C string. String C is not so rich string class member function, this is not a problem, we can write your own. Instead of spending time researching string member function, it is better to write a
5.vector容器:std::vector<.>.;
The container defined
vector container can store the basic data type of C language, classes and structures can be stored, do not speak the list
container used: 1.存放整数
elements in the form of access to the container can be like an array as
2.存放字符串
3.存放结构体
in the above book225.cpp, using a function memcpy (memory ), which is the C language library functions, memory for data replication, the following statement:
4.存放类:
storing string, string is based
vector other member functions: 1.定位的函数
2.增加元素的函数
3.删除元素的函数
4.判断容器的大小
BOOL empty (): determining whether the container is empty
int size (): returns the number of elements in the container
5.作业题:封装随机数
/*
此程序用于生成一组随机数, 指定数组范围和是否重复
*/
#include"_public.h"
class CRand
{
public:
CRand();
~CRand();
vector <int> m_val; //m_val容器
bool checkexit(const int aryyval, const int aryysize); // 用于检查是否为重复数据,aryyval为重复的值,这函数不单用,用于Rand成员函数里
void Rand(const int minvalue,const int maxvalue,bool brep=true, const int nog=5); //brep为是否允许重复; 默认为允许重复,nog指定生成多少个随机数
};
/////////////////////////////////////////////////////////////////////////////////////////////
void CRand::Rand(const int minvalue,const int maxvalue,bool brep,const int nog)
{
int len = maxvalue-minvalue;
int ii=0, itmp=0, jtmp=0; // ii生成第几个,jtmp生成共多少个,itmp生成的值
m_val.clear();
if(brep==true) // 允许重复
{
jtmp = nog;
for(ii=0;ii<jtmp;ii++)
{
itmp = rand()%(len+1)+minvalue; // (0~len)+minvalue,itmp就是min~max之间的值,不是len长度
m_val.push_back(itmp);
}
return;
}
jtmp = nog; // 不允许重复
if (nog>len) jtmp = len + 1; // 比如5-1=4,但1到5可以生成5个,所以如果nog大于len的话就取len+1个,前提不允许重复。
while(1)
{
if (jtmp == m_val.size()) break; //生成满了跳出循环
itmp = rand()%(len+1)+minvalue;
if (ii==0) // 生成第一个不用管checkexit重不重复
{
m_val.push_back(itmp);
ii++;
continue;
}
if (checkexit(itmp,ii) == false) continue; // checkexit为false则不允许重复
m_val.push_back(itmp); ii++;
}
return;
}
//////////////////////////////////////////////////////////////////
bool CRand::checkexit(const int aryyval, const int aryysize) // aryyval重复的值,aryysize允许多少个重复
{
for (int ii=0; ii<aryysize; ii++)
{
if (aryyval == m_val[ii]) return false;
}
return true;
}
///////////////////////////////////////////////////////
CRand::~CRand()
{
m_val.clear();
}
////////////////////////////////////////////////////
CRand::CRand()
{
struct timeval begin;
gettimeofday(&begin, 0);
srand(begin.tv_usec);
}
///////////////////////////////////////////////////
int main() //如何用CRand这个类
{
CRand CrtRand;
CrtRand.Rand(0, 10, false); // 若false为true允许重复,不管范围多少取nog个
for(int ii=0;ii<CrtRand.m_val.size();ii++)
{
printf("%d\n",CrtRand.m_val[ii]);
}
return 0;
}
6. Dynamic Memory: new, delete
Little dynamic memory allocation scenarios, string and vector are easy enough
dynamic memory to apply the new keyword, C ++, dynamic memory allocation is performed based on the data type, delete keys for memory deallocation, the following syntax
if the new class is out equivalent to creating an object, it will call the constructor, delete time will call the destructor