template function
*********illustrate***************
模板函数(通常用在功能相同,只是类型不同的程序)
#include <iostream.h>
template<class t, class y>
t add(t a, y b)
{
return a+b;
}
int main()
{
int a=3;
float b=4.4;
double d=7.4;
char c='a';
cout<<add(b,a)<<endl;
cout<<add(d,b)<<endl;
cout<<add(b,b)<<endl;
cout<<add(a,a)<<endl;
return 0;
}
*********over***************
template class
类模板的定义,方法和函数模板的定义方法相同
template <class T>
class tarray
{
private:
T elem;
int n;
public:
tarray(T data, int x)
{
elem=data;
n=x;
}
~tarray();
tarray(const tarray &p);
T find(T a, T b);
}
******template object define method*******
classname<template parameters> class object(parameters)
*********illustrate***************
#include <iostream.h>
template<class t, int i>
class sample
{
private:
t array[i];
public:
sample();
~sample();
void set(t a,int b);//initialize array variable
void output();
};
*********function define out of class***************
template<class t,int i> // the line can't omit, if omit ,it will error
sample::sample()
{
cout<<"construction a function"<
}
template
sample::~sample()
{
cout<<"now,delete the function"<
}
template // the line can't omit, if omit ,it will error
void sample<t,i>::set(t a,int b)
{
if(b>=0&&b<i)
array[b++]=a;
else cout<<"set function error"<<endl;
}
template<class t,int i> // the line can't omit, if omit ,it will error
void sample::output()
{
for(int k=0;k
cout<
cout<
cout<<"over"<
}
int main()
{
sample S1;//class template define
for(int i=0;i<5;i++)
S1.set(i+1,i);
S1.output();
sample S2;
for(int i=0;i<4;i++)
S2.set(3.1*i,i);
S2.output();
return 0;
}
*********over***************
*********friend***************
*********illustrate***************
#include
class A;//class declaration
class B
{
public:
void bfun(A &);
};
class C
{
public:
void cfun(A &);
};
class A
{
friend void display(A);// declaration a friend function
friend void B::bfun(A &); //menber function as friend
friend class C; //declaration a friend class
private:
int a;
int b;
public:
A(int x=0,int y=0)
{
a=x;
b=y;
}
};
void display(A e)
{
cout<<"a="<
}
void B::bfun(A &e)
{
e.a=3;
}
void C::cfun(A &e)
{
e.b=2;
}
int main()
{
A a(1,1);
display(a);
B b;
b.bfun(a);
display(a);
C c;
c.cfun(a);
display(a);
return 0;
}
*********over***************
*******static data member*******
静态数据成员必须在main函数之前初始化,且不属于某一个类对象,而是属于整个类。
静态函数只能访问静态成员和全局变量;在使用静态成员函数时,要指出其作用在哪个对象,或用限定符"::"加在类名之后,作为它的限定词。因为它不属于特定的对象,因此不能像一般的成员函数那样随意地访问对象中的非静态的数据内容。
*********illustrate***************
#include
class student
{
//private: //default is private
int score;
static int total; //static variable
public:
student()
{total++;}
static void show_total() //static function
{
cout<<"this is the static invoking"<
}
};
int student::total=0;//don't need static
int main()
{
student stu;
stu.show_total();
return 0;
}
********over**********
*******const object and const function*******
const object invoking const function only_____常对象只能调用常函数
一句话,常对象和常函数都是不能改变的
*******illustrate*******
#include <iostream.h>
class student
{
private:
long sno;
int age;
const int class=2;
public:
student(int sno,int age)
{
this->sno=sno;
this->age=age;
}
student()
{
}
void print()
{
cout<<"this is a common function"<<sno<<" "<<age<<endl;
}
void print() const
{
cout<<"this is const member function"<<sno<<" "<<age<<endl;
}
const int fun() const
{
return class;
}
};
int main()
{
student stu1(04061228,23); //define a common class object
const student stu2=stu1; //define a const object
student const stu3(0406122,21);
stu1.print();
stu2.print();
stu3.print();
cout<<stu1.fun()<<endl;
return 0;
}
*******class object array*******
*******illustrate*******
#include <iostream.h>
class student
{
private:
int sno;
int age;
char *name;
public:
student(int sno,int age,char *name);
student()
{
}
int getsno();
int getage();
char* getname();
};
student::student(int sno,int age,char *name)
{
this->sno=sno;
this->age=age;
this->name=name;
}
int student::getsno()
{
return sno;
}
int student::getage()
{
return age;
}
char *student::getname()
{
return name;
}
int main()
{
//note: if follow array changed pointer, it will error
char p[10];//这数组不能改为指针,如果改了后,后面的对象数组将会出错
cin>>p;
student stu[2]={student(23,25,p),student(34,35,"how")};
cout<<stu[0].getage()<<endl;
cout<<stu[1].getage()<<endl;
cout<<stu[0].getsno()<<endl;
cout<<stu[1].getsno()<<endl;
for(int i=0;i<2;i++)
{
cout<<stu[i].getsno()<<" "<<stu[i].getage()<<" "<<stu[i].getname()<<endl;
}
return 0;
}
**********over**********
******pointer*******
*(*b+j)=*(b[0]+j)
*b+j=&b[0][j]
*******illustrate********
******pointer array***********
指针数组一般用于字符串
#include <iostream.h>
int main()
{
int a1[2]={1,9};
int a2[2]={2,6};
int a3[2]={12,23};
int *b[3];
b[0]=a1;
b[1]=a2;
b[2]=a3;
for(int i=0;i<3;i++)
for(int k=0;k<2;k++)
cout<<b[i][k]<<endl;
return 0;
}
字符串处理函数
strlen
strcmp
strncmp //compare the former n character 比较前n个字符
strcat
strncat
strcpy(*dest, *resource)
strncpy
above all need include the head file <string.h>
在字符串中查找字符函数,如果没有找到,后面的代码也停止执行
char strchr(const char *s, char c);
在字符串中逆序查找字符函数
char strrchr(const char *s, char c)
****************
在字符串查找子串
char *strstr(char (*s1, const char *s2);
char *index(const char *, int)
***********illustrate**********
#include <iostream.h>
#include <string.h>
int main()
{
char *p="hello, are you ok";
char ch='p';
char *sub="you";
cout<<strchr(p,ch)<<endl;
cout<<strrchr(p,ch)<<endl;
cout<<strstr(p,sub)<<endl; //search the sub string
cout<<"hello"<<endl;
return 0;
}
指针函数
函数可以返回指针值,这样的函数称为指针函数。
函数指针
指向函数的指针称为函数指针
*******illustrate*********
********function pointer*********
#include <iostream.h>
void fun1()
{
cout<<"this the fun1"<<endl;
}
void fun2()
{
cout<<"this the fun2"<<endl;
}
void fun3()
{
cout<<"this the fun3"<<endl;
}
int main()
{
void (*p[3])()={fun1,fun2,fun3};
for(int i=0;i<3;i++)
{
(*p[i])();
}
return 0;
}
output:
this the fun1
this the fun2
this the fun3
函数指针所指的对象必须作为全局函数,不能在main()里定义,如果在main()里定义就会出错
************over*************
指针可以被引用,而引用不可以被引用
*****illustrate**********
char *P;
char *&p1=p;
can not define like this:
char &*p1=p //this is error define
常量指针-----指向常量的指针
*****const pointer****
int a=5;
int b=8;
const int *po=&a; or int const *po=&a; //const pointer
po=&b; //right 该指针不是常量
*po=4; //error指针指向的是常量,不能修改该常量
指针常量
******pointer const*********
int a=5;
int b=8;
int *const po=&b;
po=&a; //error, because the pointer is a const
*po=11; //right, *po不是常量
指针常量指针
int a=2;
const int *const p=&a;
//now, cann't modify anything of *p or p
现在*P or p的任何地方将不能被修改了
object pointer-------对象指针(omit)
*******this pointer*********
变量名相同时,必须用this pointer
class test
{
private:
int x;
int y;
public:
test(int x, int y)
{
this->x=x; //must do it
this->y=y; //must do it, can't "y=y"
}
};
new and delete
这两个必须成对出现,当使用了new后不用delete,会出现内存泄漏
int *p1;
int *p2;
p1=new int(2);
p2=new int[4];
delete p1;
delete p2;
|