1、内联函数.
class Point{
public:
void init (int initX,int initY){
x=initX;
y=initY;
}
int getX(){return x;}
int getY(){return y;}
private:
int x,y;
};
2、构造函数
class Clock{
public:
Clock(int newH,int newM,int newS); //构造函数
void setTime(int newH,int newM,int newS);
void showTime();
private:
int hour, minute,second;
};
//构造函数的实现
Clock::Clock(int newH,int newM,int newS){
hour=newH;
minute=newM;
second=newS;
}
//构造函数的实现
int main(){
Clock c(0,0,0); //隐含调用构造函数,将初始值作为实参。
c.showTime();
return 0;
}
3、拷贝构造函数
class Point{
public:
Point(int xx=0,int yy=0){x=xx;y=yy;}
Point(Point& p);
int getX(){return x;}
int getY(){return y;}
private:
int x,y;
};
Point::Point(Point& p){
x=p.x;
y=p.y;
}
4、构造函数和析构函数
class Point{
public:
Point(int xx, int yy);
Point(Point &p);
~Point();
int getX(){return X;}
int getY(){return Y;}
private:
int x,y;
};
Point::Point(int xx, int yy){
x=xx;
y=yy;
}
Point::~Point(){
}
5、组合类的构造函数
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2)
{
cout<<"Line构造函数被调用“<<endl;
double x=double(p1.GetX()-p2.GetX());
double y=double(p1.GetY()-p2.GetY());
len=sqrt(x*x+y*y);
}
6、组合类的拷贝构造函数
Line::Line (Line &Seg):p1(Seg.p1),p2(Seg.p2)
{
cout<<"Line拷贝构造函数被调用“<<endl;
len=Seg.len;
}
7、复数运算
#include<iostream>
using namespace std;
class Complex{. //复数类
private:
float real,imag; //复数的实部和虚部
public:
//构造函数,可以当作隐式类型转换使用
Complex(float real=0,float imag=0):real(real),image(image){}
//复数加法,生成临时对象并返回
Complex add(Complex c){
return Complex(real+c.real,imag+c.imag);
}
//复数减法,生成临时对象并返回
Complex sub(Complex c){
return Complex(real-c.real,imag-c.imag);
}
//复数乘法,生成临时对象并返回
Complex mul(Complex c){
return Complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);
}
//显示复数
void show(){
if(imag>=0)
cout<<real<<"+"<<imag<<'i'<<endl;
else
cout<<real<<"-"<<-imag<<'i'<<end;
}
};
int main(){
Complex z(1,2);
z.add(Complex(3,4)).show(); //Complex(3,4)是临时对象
static_cast<Complex>(5).sub(z).show(); //输出5-Z,使用了显示类型转换
z.mul(-3.0f).show(); //输出值Z*(-3),使用了隐含类型转换
return 0;
}
8、使用友元函数计算两点距离
#include <iostream>
#include <cmath>
class Point{
public:
Point(int x=0,int y=0):x(x),y(y){}
int getX(){return x;}
int getY(){return y;}
friend float dist (Point &a,Point &b);
private:
int x,y;
};
float dist(Point &a,Point &b){
double x=a.x-b.x;
double y=a.y-b.y;
return static_cast<float>(sort(x*x+y*y));
}
int main(){
Point p1(1,1),p2(4,5);
cout<<"The distance is:";
cout<<dist(p1,p2)<<endl;
return 0;
}
9、友元类 例2
class A{
friend class B;
public:
void display(){
cout<<x<<endl;
}
private:
int x;
}
class B{
public:
void set(int i);
void display();
private:
A a;
};
void B::set(int i){
a.x=i;
}
void B::display();
}
10、常对象举例
class A
{
public:
A(int i,int j){x=i;y=j;}
private:
int x,y;
};
A const a(3,4); //a是常对象,不能被更新
11、常成员函数
#include<iostream>
using namespace std;
class R{
public:
R(int r1,int r2):r1(r1),r2(r2){}
void print();
void print() const;
private:
int r1,r2;
};
void R::print(){
cout<<r1<<":"<<r2<<endl;
}
void R::print() const{
cout<<r1<<";<<r2<<endl;
}
int main(){
R a(5,4);
a.print();
const R b(20,52);
b.print();
return 0;
}
12、常数据成员
class A{
public:
A(int i);
void print();
private:
const int a;
static const int b; //静态常数据成员
};
const int A::b=10;
A::A(int i):a(i){} //常成员a不能在函数体中赋值,只能在初始化的时候赋值
void a::print(){
cout<<a<<":"<<b<<endl;
}
int main(){
A a1(100),a2(0);
a1.print();
a2.print();
return 0;
}
13、常引用作形参
float dist(const Point &p1,const Point &p2){
double x=p1.x-p2.x;
double y=p1.y-p2.y;
return static_cast<float>(sort(x*x+y*y));
}
int main()
{
const Point myp1(1,1),myp2(4,5);
cout<<"The distance is:";
cout<<dist(myp1,myp2)<<endl;
return 0;
}