分为传变量值及传变量地址值两种。c++使用传值及传引用。传引用就是传对象的地址。
(1)对象作为函数参数(不会影响原来的实参,使用指针就可以改变原来的值。)
(4)一个默认参数需要指定一个特定值,切在之前必须赋值。
(5)将函数为返回一个引用(为将该函数用在赋值运算符的左边)
#include <iostream>
using namespace std;
int a[]={2,4,6,8,10,12};
int& index(int i);
void main()
{
index(3)=16;
cout<<index(3)<<endl;
}
int& index(int i)
{
return a[i];
}
(6)内联函数,可以节约空间,类似于宏定义
(7)函数模板
#include <iostream>
using namespace std;
//函数模板以template和一个形参表<class T>开头
template <class T>
//定义模板函数
T max(T m1,T m2)
{
return (m1>m2)?m1:m2;
}
void main()
{
cout<<max(2,5)<<"\t"<<max(2.0,5.1)<<"\t"
<<max('w','a')<<"\t"<<max("ABC","ABD")<<endl;
//max<int>(2,5)显示的给出比较规则,如果不标准的参数写法就使用这种方式,例如max(2.5,5);
}
二.类和对象
在类中声明的任何成员不能使用extern,auto,register关键字修饰。
1.使用引用来作为构造函数的参数用来使用一个对象来初始化另一个对象;
A::A(A& );
(2)每一个成员函数都有一个隐形的参数this指针,可以使用该类的资源;
void Point:Setxy(int a,int b,(Point *)this)
{
this->x=a;
this->y=b;
}
(3)空类:大型项目中进行先期测试时使用。
(4)在类中成员函数中使用可能重复的参数时,可以
MyClass::number=i;
当初始化const成员和引用成员时,必须通过成员初始化列表进行。
(5)静态变量的初始化:在函数体外int TEXT::x=25;
静态成员初始化只能说明一次,且静态数据成员通常被说明为私有的,通过定义共有的静态成员函数来
访问静态数据成员。
在类中定义的静态成员函数是内联的。
2.友元函数(可以存取私有成员,公有成员,保护成员)
(1)类本身的友元函数
#include <iostream>
#include <cmath>
using namespace std;
class Point{
private:
double x,y;
public:
Point(double xi,double yi){x=xi;y=yi;}
friend double distances(Point&,Point&);
};
double distances(Point& a,Point& b)
{
double dx=a.x-b.x;
double dy=a.y-b.y;
return dx*dx+dy*dy;
}
void main()
{
Point p1(3,5),p2(4,6);
cout<<distances(p1,p2)<<endl;
}
(2)将成员函数用作友元
#include <iostream>
using namespace std;
class Two;
class One{
private:
int x;
public:
One(int a){x=a;}
void func(Two& );
};
class Two{
public:
int y;
Two(int b){y=b;}
friend void One::func(Two& );
};
void One::func(Two& r)
{
cout<<r.y<<endl;
r.y=x;
cout<<r.y<<endl;
}
void main()
{
One Obj1(5);
Two Obj2(8);
Obj1.func(Obj2);
}
(3)将一个类说明为另一个类的友元
#include <iostream>
using namespace std;
class Two{
int y;
public:
friend class One;
};
class One{
int x;
public:
One(int a,Two&r,int b)
{x=a;r.y=b;}
void Display(Two &);
};
void One::Display(Two &r)
{
cout<<x<<" "<<r.y<<endl;
}
void main()
{
Two Obj2;
One Obj1(23,Obj2,55);
Obj1.Display(Obj2);
}
3.const
在常成员函数里,不能更新对象的数据成员,也不能调用该类中没有用const修饰的成员函数。
常量对象只能使用常量成员函数。
4.指向类成员函数的指针
#include <iostream>
using namespace std;
class A{
private:
int val;
public:
A(int i){val=i;}
int value(int a){return val+a;}
};
void main(void)
{
int(A::*pfun)(int);
pfun=A::value;
A obj(10);
cout<<(obj.*pfun)(15)<<endl;//必须用括号括起来
A *pc=&obj;
cout<<(pc->*pfun)(15)<<endl;//必须用括号括起来
}
三.继承(派生),类模板和向量,多态性和虚函数
1.继承和派生
(1)从一个或多个基类产生新类的过程称为派生。类的继承是指派生类继承其类的数据成员和成员函数。继承用来表示
亲属关系。派生类可以重新定义已有的成员函数,改变基类成员的访问权限。
(2)类的保护成员
#include <iostream>
using namespace std;
class Point{
protected:
int x,y;
public:
Point(int a,int b){x=a;y=b;}
void Show(){cout<<"x="<<x<<",y="<<y<<endl;}
};
class Rectangle:public Point{
private:
int h,w;
public:
Rectangle(int,int,int,int);
void Show(){cout<<"x="<<x<<",y="<<y<<",h="<<h<<",w="<<w<<endl;}
};
Rectangle::Rectangle(int a,int b,int h0,int w0):Point(a,b)//构造函数初始化列表
{h=h0;w=w0;}
void main(){
Point a(3,4);
Rectangle r1(3,4,5,6);
a.Show();//基类对象调用基类Show()
r1.Show();//派生类对象调用派生类Show()
}
(3)向量是以为数组的类版本,向量中存储元素的多少可以在运行中动态增长或缩小。用size()方法,动态的获得vector
对象当前存储元素的数量。
vector提供了四种构造函数,用length表示长度,数据类型用type表示,对象名为name:
vector<type>name;//定义type的向量空表
vector<type>name(length);//具有length个type的向量,元素初始化为0
vector<type>name(length,a);//元素初始化为a
vector<type>name1(name);//使用已定义的向量name构造向量name1
向量之间使用赋值运算符"=",容许同类型的向量列表相互赋值,目标的元素数目与赋值源的元素数目相同。
可以先初始化一个数组,然后把数组的内容赋值给向量:
int IA[10]={0,1,2,3,4,5,6,7,8,9};
vector<int>VB(IA,IA+10);
2.多态性和虚函数
(1)虚函数
#include <iostream>
#include <stdio.h>
using namespace std;
class base
{
private:
virtual void print()
{
printf("base\n");
}
public:
void doprint()
{
print();
}
virtual ~base(){}
};
class derived:public base
{
virtual void print()
{
printf("derived\n");
}
};
int main(int argc,char *argv[])
{
derived d;
base& b=d;
b.doprint();
return 0;
}
(2)纯虚函数
纯虚函数定义如下: virtual functionname (parameter ) =0 ;
让C++知道该函数并无意义,它的作用只是为了让派生类进行函数重载保留位置。纯虚函数的定义方法就是在类的虚函数后面加上 “ =0 ” 标记,类中一旦出现了纯虚函数的定义,那么此类为抽象类。
实例 1.
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
//抽象类定义
class abstractcls
{
public:
abstractcls (float speed, int total) //构造函数
{
abstractcls::speed=speed;
abstractcls::total=total;
}
virtual void showmember()=0; //纯虚函数的定义
protected:
float speed;
int total;
};
class car public : abstractcls //抽象类派生类
{
public:
//派生类的构造函数,实现基类的构造函数
car(int aird, float speed, int total):abstractcls(speed,total)
{
car::aird=aird;
}
virtual void showmember() //派生类函数重载
{
cout<<speed<<"1"<<total<<"2"<<endl;
}
protected:
int aird;
};
调用如下:
int main(void)
{
car b(250,150,4);
b.showmember();
return 0;
}
总结,什么情况下需要使用纯虚函数:
1,当想要在基类中抽象出一个方法,且该类被继承类而不能被实例化时。
2,基类的方法必须在派生类中被实现时。
3,多个对象具有公共的抽象属性,但却有不同的实现要求时。