一、C++ 类定义

定义格式:

实例:

使用关键字 class 定义 Box 数据类型

class Box
{
   public:
      double length;   // 盒子的长度
      double breadth;  // 盒子的宽度
      double height;   // 盒子的高度
};

二、C++ 对象定义

声明类的对象与声明基本类型的变量同理

比如:声明类Box的两个对象

Box Box1;          // 声明 Box1,类型为 Box
Box Box2;          // 声明 Box2,类型为 Box

对象 Box1 和 Box2 都有它们各自的数据成员

三、访问数据成员

实例:

#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;   // OK: 因为 height 是公有的
   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;
}

当上面的代码被编译和执行时,它会产生下列结果:

Box1 的体积:210
Box2 的体积:1560
Box3 的体积:1536

类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样,如上例

 四、类的成员修饰符

关键字 public、private、protected 称为访问修饰符

1.公有(public)成员

公有成员在程序中类的外部是可访问的,如上例

可以不使用任何成员函数来设置和获取公有变量的值

2.私有(private)成员

私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。

只有类和友元函数可以访问私有成员。

默认情况下,类的所有成员都是私有的。

例如:

在下面的类中,width 是一个私有成员

class Box
{
   double width;
   public:
      double length;
      void setWidth( double wid );
      double getWidth( void );
};

实际操作中,我们一般会在私有区域定义数据,在公有区域定义相关的函数,以便在类的外部也可以调用这些函数

实例:

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      double length;
      void setWidth( double wid );
      double getWidth( void );
 
   private:
      double width;
};
 
// 成员函数定义
double Box::getWidth(void)
{
    return width ;
}
 
void Box::setWidth( double wid )
{
    width = wid;
}
 
// 程序的主函数
int main( )
{
   Box box;
 
   // 不使用成员函数设置长度
   box.length = 10.0; // OK: 因为 length 是公有的
   cout << "Length of box : " << box.length <<endl;
 
   // 不使用成员函数设置宽度
   // box.width = 10.0; // Error: 因为 width 是私有的
   box.setWidth(10.0);  // 使用成员函数设置宽度
   cout << "Width of box : " << box.getWidth() <<endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Length of box : 10
Width of box : 10

3.protected(受保护)成员

protected(受保护)成员变量或函数与私有成员相似,即在类的外部不可访问

但是,protected(受保护)成员在派生类(即子类)中是可访问的

实例:

#include <iostream>
using namespace std;
 
class Box
{
   protected:
      double width; // protected : 类的外部只有子类可以访问
};
 
class SmallBox:Box // SmallBox 是派生类
{
   public:
      void setSmallWidth( double wid );
      double getSmallWidth( void );
};
 
// 子类的成员函数
double SmallBox::getSmallWidth(void)
{
    return width ; // 子类可以访问父类 protected 修饰的成员
}
 
void SmallBox::setSmallWidth( double wid )
{
    width = wid;
}
 
// 程序的主函数
int main( )
{
   SmallBox box;
 
   // 使用成员函数设置宽度
   box.setSmallWidth(5.0);
   cout << "Width of box : "<< box.getSmallWidth() << endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Width of box : 5

五、类的构造函数

1.类的构造函数

类的构造函数也是类的成员函数,它会在每次创建类的新对象时自动调用执行

构造函数的名称与类的名称是完全相同的;

不会返回任何类型,也不会返回 void;

例如:

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();  // 这是构造函数
 
   private:
      double length;
};
 
// 成员函数定义,包括构造函数
Line::Line(void)
{
    cout << "Object is being created" << endl;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}
// 程序的主函数
int main( )
{
   Line line;
 
   // 设置长度
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Object is being created
Length of line : 6

2.带参数的构造函数

默认的构造函数没有任何参数,但如果需要,构造函数也可以带有参数,这样在创建对象时就会给对象赋初始值

例如:

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line(double len);  // 这是构造函数
 
   private:
      double length;
};
 
// 成员函数定义,包括构造函数
Line::Line( double len)
{
    cout << "Object is being created, length = " << len << endl;
    length = len;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}
// 程序的主函数
int main( )
{
   Line line(10.0);
 
   // 获取默认设置的长度
   cout << "Length of line : " << line.getLength() <<endl;
   // 再次设置长度
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Object is being created, length = 10
Length of line : 10
Length of line : 6

3.使用初始化列表初始化成员

构造函数的定义后面加冒号来初始化

假设有一个类 C,具有多个字段 X、Y、Z 等需要进行初始化

C::C( double a, double b, double c): X(a), Y(b), Z(c)
{
  ....
}

实例:

Line::Line( double len): length(len)
{
    cout << "Object is being created, length = " << len << endl;
}

等同于

Line::Line( double len)
{
    length = len;
    cout << "Object is being created, length = " << len << endl;
}

六、类的析构函数

类的析构函数也是类的成员函数,它会在每次删除所创建的对象时自动调用执行;

析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀;

它不会返回任何值,也不能带有任何参数;

析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源

实例:

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();   // 这是构造函数声明
      ~Line();  // 这是析构函数声明
 
   private:
      double length;
};
 
// 成员函数定义,包括构造函数
Line::Line(void)
{
    cout << "Object is being created" << endl;
}
Line::~Line(void)
{
    cout << "Object is being deleted" << endl;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}
// 程序的主函数
int main( )
{
   Line line;
 
   // 设置长度
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Object is being created
Length of line : 6
Object is being deleted

七、C++this指针

每一个对象都能通过 this 指针来访问自己的地址

在成员函数内部,它可以用来指向调用对象

实例:

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      // 构造函数定义
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      int compare(Box box)
      {
         return this->Volume() > box.Volume(); // 这里的 this 指向的是调用对象 Box1
      }
   private:
      double length;     // Length of a box
      double breadth;    // Breadth of a box
      double height;     // Height of a box
};
 
int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // Declare box1 参数传入构造函数中,给对象赋予初值
   Box Box2(8.5, 6.0, 2.0);    // Declare box2
 
   if(Box1.compare(Box2))      // 对象 Box1 调用函数 compare
   {
      cout << "Box2 is smaller than Box1" <<endl;
   }
   else
   {
      cout << "Box2 is equal to or larger than Box1" <<endl;
   }
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Constructor called.
Constructor called.
Box2 is equal to or larger than Box1

八、C++类的静态成员

静态成员在类的所有对象中是共享的

注意:

静态成员的初始化不能放置在类的定义中,应该在类的外部通过使用范围解析运算符 :: 来重新声明静态变量从而对它进行初始化;

如果没有初始化语句,在创建第一个对象时,会被初始化为零

实例:

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      static int objectCount;
      // 构造函数定义
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // 每次创建对象时增加 1
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
   private:
      double length;     // 长度
      double breadth;    // 宽度
      double height;     // 高度
};
 
// 初始化类 Box 的静态成员
int Box::objectCount = 0;
 
int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // 声明 box1
   Box Box2(8.5, 6.0, 2.0);    // 声明 box2
 
   // 输出对象的总数
   cout << "Total objects: " << Box::objectCount << endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

Constructor called.
Constructor called.
Total objects: 2

 

Posted on 2021-12-22 10:04  choco莉特  阅读(90)  评论(0编辑  收藏  举报