1.构造函数
默认构造函数
#include<iostream>
using namespace std;
class clock//class 类名
{
public://任何类都必须有外部接口
clock(int h, int m, int s);//构造函数(函数名必须与类名相同)
clock()//默认构造函数
{
hour = 24;
minute = 0;
second = 0;
}//内联函数的显示表达方式
void settime(int h = 0, int m = 0, int s = 0);//函数成员
inline void showtime()//内联函数的显式表达方式(inline)
{
cout << hour << ":" << minute << ":" << second << endl;
}
private://若private紧接着类名称,则关键字private可以省略
int hour, minute, second;//数据成员
};
clock::clock(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
//构造函数的实现也可写作:clock::clock(int h,int m,int s):hour(h),minute(m),second(s){}
//同样,默认构造函数没有内联情况时可写作初始化列表:clock::clock():hour(24),minute(0),second(0){}
void clock::settime(int h, int m, int s)//成员函数名需要用类名来限制
{
hour = h;
minute = m;
second = s;
}
int main()
{
clock myclock;//类名 对象名
myclock.settime();//调用成员函数进行访问时使用操作符“.”
myclock.showtime();
myclock.settime(8, 30, 0);
myclock.showtime();
clock c1;//调用无参数的构造函数(默认构造函数)
c1.showtime();//调用有参数的构造函数
clock c2(12, 30, 0);
c2.showtime();
system("pause");
return 0;
}
复制构造函数
#include<iostream>
using namespace std;
class point
{
public:
point(int xx=0, int yy=0)//构造函数
{
x = xx;
y = yy;
}
point(point&p);//复制构造函数
int get()
{
return x;
}
private:
int x, y;
};
point::point(point&p)//复制构造函数的实现
{
x = p.x;
y = p.y;
cout << "calling the copy constructor" << endl;//每输出这条语句,意味着调用了一次复制构造函数
}
void fun1(point p)//形参为point类对象的函数
{
cout << p.get() << endl;
}
point fun2()//返回值为point类对象的函数
{
point a(1, 2);
return a;
}
int main()
{
point a(8, 30);
point b = a;//复制构造函数调用情况一:用a初始化b
cout << b.get() << endl;
fun1(b);//复制函数调用情况二:对象b作为fun1的实参
b=fun2();//复制函数调用情况三:函数的返回值是类对象
cout << b.get() << endl;
system("pause");
return 0;
}
复制构造函数也可以实现放大复制,缩小复制,修改复制构造函数可以实现点的移动:
point::point(point&p)
{
x = p.x+1;
y = p.y+1;
cout << "calling the copy constructor" << endl;
}
2.析构函数
析构函数作用与构造函数相反,用来完成对象被删除前的一些清扫工作,它调用完成后,对象被删除,相应内存空间被释放。与构造函数相同,析构函数通常是类的公共函数成员,名称由类的名称前加“~”构成,没有返回值。
class clock
{
public:
clock();//构造函数
void settime(int h, int m, int s);
void showtime();
~clock();//析构函数
private:
int hour, minute, second;
};
例题
#include<iostream>
#include<cmath>
using namespace std;
#define pi 3.1415926//圆周率
#define fence_price 35//栅栏单价
#define concrete_price 20//水泥单价
class circle
{
public://外部接口
circle(float R);//构造函数
float circumference();//计算圆周长
float area();//计算圆面积
private:
float radius;
};
circle::circle(float R)//构造函数初始化数据成员radius
{
radius = R;
}
float circle::circumference()
{
return 2 * pi*radius;
}
float circle::area()
{
return pi*pow(radius, 2);
}
int main()
{
float r;
cout << "Enter the radius of the pool:";
cin >> r;
circle pool(r);//游泳池边界对象
circle poolfence(r + 3);//栅栏对象
float fencecost, concretecost;
fencecost = fence_price*poolfence.circumference();//计算栅栏价格
cout << "The fence cost is $" << fencecost << endl;
concretecost = concrete_price*(poolfence.area() - pool.area());//计算过道价格
cout << "The concrete cost is $" << concretecost << endl;
system("pause");
return 0;
}
3.类的组合
#include<iostream>
#include<cmath>
using namespace std;
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)
{
cout << "Calling the copy constructor of Point" << endl;
}
class Line//类的组合
{
public:
Line(Point xp1, Point xp2);
Line(Line&l);
double getlen() { return len; }
private:
Point p1, p2;//point类的对象
double len;
};
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2)
{
cout << "Calling constructor of Line" << endl;
double x = static_cast<double> (p1.getx() - p2.getx());//类型转换
double y = static_cast<double>(p1.gety() - p2.gety());
len = sqrt(pow(x, 2) + pow(y, 2));
}
Line::Line(Line&l):p1(l.p1),p2(l.p2)
{
len = l.len;
cout << "Calling the copy constructor of Line" << endl;
}
int main()
{
Point myp1(1, 1), myp2(4, 5);//建立Point类对象
Line line(myp1, myp2);//建立Line类对象
cout << "Then length of line is:";
cout << line.getlen() << endl;
Line line2(line);//利用复制构造函数建立一个新对象
cout << "The length of line2 is:";
cout << line2.getlen() << endl;
system("pause");
return 0;
}