一、类的基本结构
注意:在(::)运算符之前必须使用类名,在(.)运算符之前必须使用对象名
#include <iostream>
using namespace std;
class Box
{
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
// 成员函数声明
double get(void);
void set( double len, double bre, double hei );
};
// 成员函数定义
double Box::get(void)
{
return length * breadth * height;
}
void Box::set( double len, double bre, double hei)
{
length = len;
breadth = bre;
height = hei;
}
int main( )
{
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
Box Box3; // 声明 Box3,类型为 Box
double volume = 0.0; // 用于存储体积
// box 1 详述
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 详述
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// box 1 的体积
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Box1 的体积:" << volume <<endl;
// box 2 的体积
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Box2 的体积:" << volume <<endl;
// box 3 详述
Box3.set(16.0, 8.0, 12.0);
volume = Box3.get();
cout << "Box3 的体积:" << volume <<endl;
return 0;
}
二、类的成员函数
函数的定义
可以在类的内部定义
class Box
{
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
double getVolume(void)
{
return length * breadth * height;
}
};
也可以在类的外部定义
class Box
{
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
// 成员函数声明
double get(void);
void set( double len, double bre, double hei );
};
// 成员函数定义
double Box::get(void)
{
return length * breadth * height;
}
void Box::set( double len, double bre, double hei)
{
length = len;
breadth = bre;
height = hei;
}
函数的调用
Box Box1; // 声明 Box1,类型为 Box
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
三、构造函数和析构函数
1、执行时间
构造函数:会在每次创建类的新对象时执行。
析构函数:会在每次删除所创建的对象时执行。
2、定义规则
构造函数:没有返回值,可以带参数
析构函数:没有返回值,不能带参数
示例
# include <iostream>
using namespace std;
class Line{
public:
void setLength(double len);
Line(double len); // 构造函数
~Line(double len); // 析构函数
private:
double length;
};
Line::line(double len){
length = len;
}
~Line::line(double len){
cout<<"Object is being destroy";
}
void Line::setLength( double len ){
length = len;
}
四、类的静态成员
静态成员变量
# include <iostream>
using namespace std;
class BOX{
public:
static int Count;
};
int main(void){
cout << BOX::Count;
}
静态成员函数
# include <iostream>
using namespace std;
class BOX{
public:
static int sayHello(){
cout << "Hello,World."
}
};
int main(void){
cout << BOX::sayHello();
}
五、继承
class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,…
// 基类
class Animal {
public:
void eat(){
cout << "Animal must eat food."
}
void sleep(){
cout << "Animal must sleep."
}
};
//派生类
class Dog : public Animal {
public:
void bark(){
cout << "Dog can bark."
}
};
访问控制和继承
派生类可以访问基类中所有的非私有成员。因此基类成员如果不想被派生类的成员函数访问,则应在基类中声明为 private。
六、接口(抽象类)
如果类中至少有一个函数被声明为纯虚函数,则这个类就是抽象类。纯虚函数是通过在声明中使用 “= 0” 来指定的,如下所示:
#include <iostream>
using namespace std;
// 基类
class Shape
{
public:
// 提供接口框架的纯虚函数
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// 派生类
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
int getArea()
{
return (width * height)/2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
// 输出对象的面积
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// 输出对象的面积
cout << "Total Triangle area: " << Tri.getArea() << endl;
return 0;
}