[C / C ++ C ++] _1_ basis function overloading, classes and objects, references, / string class, vector vessel, dynamic memory


1. function overloading: writetofile ()

C++程序的命名规则
Here Insert Picture Description
C++输入和输出
Here Insert Picture Description
C++布尔类型(bool)
Here Insert Picture Description
C++定义变量的位置
Here Insert Picture Description
C++函数的缺省参数
Here Insert Picture Description
Here Insert Picture Description
C++动态内存分配
In the C language, dynamic memory allocation with malloc () function to release memory using free () function
Here Insert Picture Description
C++函数重载
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
above wording is C, the following is written C ++ function overloading
Here Insert Picture Description
Here Insert Picture Description
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
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

2. Class and Object: vfprintf ()

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Full above, the following two lines in the uplink to improve the downlink, the same effect, but does not cover the object class and
Here Insert Picture Description
the following two functions with the same name show achieve function overloading
Here Insert Picture Description
Here Insert Picture Description
structure upgraded to Class
Here Insert Picture Description
below into three classes do not show all statements relate to the structure, struct st_girl -> class CGirl
Here Insert Picture Description
following three kinds of calls Show
Here Insert Picture Description
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
Here Insert Picture Description
Here Insert Picture Description
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.
Here Insert Picture Description
Here Insert Picture Description
Defined pointer to this address, the Girl.m_name (class.) Replace pst-> m_name
Here Insert Picture Description
Here Insert Picture Description
object array: an array of objects may be defined as an object, and essentially no difference between other types of array variable
Here Insert Picture Description
Here Insert Picture Description
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
Here Insert Picture Description
Here Insert Picture Description
below the pst and Girl is the same, just different names
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
initialization and memory-intensive objects the size of
Here Insert Picture Description
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
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
access to members of a class
Here Insert Picture Description
Here Insert Picture Description
named member variables
Here Insert Picture Description
constructor
Here Insert Picture Description
CFile a class, CFile () is the function, not the object (). Following return 0; return directly
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
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
Here Insert Picture Description
Here Insert Picture Description
when the object is instantiated as CFile File selected by the parameter which constructor
Here Insert Picture Description
destructor
Here Insert Picture Description
Here Insert Picture Description
constructors can be overloaded, a class can have multiple constructors
Here Insert Picture Description
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
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
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
Here Insert Picture Description
extraction member functions realized by the above Fprintf following variable parameters, man sprintf as not function overloading, ellipsis variable parameters
Here Insert Picture Description
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 ...)
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

3. Quote: Alias

Here Insert Picture Description
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
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
reference may be modified by const, for read-only, this reference declaration, not by reference to the target value of the variable is modified
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
with a referenced structure, but does not refer to an array of
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
transmission addresses and references are confused
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

4.string类:string str,str=,str.c_str()

Here Insert Picture Description
Here Insert Picture Description
The overloaded operator string: = direct assignment, ==,>, <,> =, <=, =, and compare strings, + + = operator to connect or two strings, [] acquired specific character! array-like
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
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.
Here Insert Picture Description
Here Insert Picture Description
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
Here Insert Picture Description

5.vector容器:std::vector<.>.;

Here Insert Picture Description
The container defined
Here Insert Picture Description
vector container can store the basic data type of C language, classes and structures can be stored, do not speak the list
Here Insert Picture Description
container used: 1.存放整数
Here Insert Picture Description
Here Insert Picture Description
elements in the form of access to the container can be like an array as
Here Insert Picture Description
Here Insert Picture Description
2.存放字符串
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
3.存放结构体
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
in the above book225.cpp, using a function memcpy (memory ), which is the C language library functions, memory for data replication, the following statement:
Here Insert Picture Description
4.存放类:storing string, string is based
vector other member functions: 1.定位的函数
Here Insert Picture Description
2.增加元素的函数
Here Insert Picture Description
3.删除元素的函数
Here Insert Picture Description
4.判断容器的大小
BOOL empty (): determining whether the container is empty
int size (): returns the number of elements in the container
5.作业题:封装随机数
Here Insert Picture Description

/* 
此程序用于生成一组随机数, 指定数组范围和是否重复
*/
#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
Here Insert Picture Description
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
Here Insert Picture Description
if the new class is out equivalent to creating an object, it will call the constructor, delete time will call the destructor
Here Insert Picture Description

Published 18 original articles · won praise 113 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_43435675/article/details/102998320