c++小知识3_内存分配和重载

c++内存分配

  c++内存分配利用的是newdelete运算符。下面介绍其在为变量赋予空间和为数组赋予空间的不同操作。如果想要清楚C++内存空间划分的话可以看这个博客https://blog.youkuaiyun.com/cherrydreamsover/article/details/81627855
今天这里仅仅讲操作。

1.为变量赋予空间

Type* pointer = new Type;
//...
delete pointer;

example:

#include<iostream>
using namespace std;

typedef struct {
	int age;
	string name;
} ID;

int main() {
	//仅分配一个类型单位的空间
	int* test1 = new int;
	ID* test2 = new ID;
	*test1 = 1;
	test2->age = 12;
	test2->name = "lopnor";
	cout << *test1 << endl;
	cout << test2->name << "的年龄是:" << test2->age << endl;
	delete test1;
	delete test2;
	//cout << *test1 << endl;
	//cout << test2->name << "的年龄是:" << test2->age << endl;
	return 0;

}

结果:
在这里插入图片描述

2.为数组赋予空间

语法:

Type* pointer = new Type[N];
//...
delete[] pointer;

表示用于分配类型类型的元素的块(数组),其中N是表示这些元素的量的整数值。

#include<iostream>
using namespace std;

typedef struct {
	int age;
	string name;
} ID;

int main() {
	//仅分配一个类型单位的空间
	int* test1 = new int[3];
	ID* test2 = new ID[3];
	*test1 = 1;
	*(test1 + 1)= 2;
	test1[2] = 3;
	test2->age = 12;
	test2->name = "lopnor";
	(test2 + 1)->age = 13;
	(test2 + 1)->name = "morpol";
	(test2+2)->age = 14;
	(test2+2)->name = "pplccom";
	cout << *test1 << endl;
	cout << test1[1] << endl;
	cout << test1[2] << endl;
	cout << test2->name << "的年龄是:" << test2->age << endl;
	cout << (test2 + 1)->name << "的年龄是:" << test2->age << endl;
	cout << test2[2].name << "的年龄是:" << test2[2].age << endl;
	delete[] test1;
	delete[] test2;
	//cout << *test1 << endl;
	//cout << test2->name << "的年龄是:" << test2->age << endl;
	return 0;

}

结果:
在这里插入图片描述


重载

  c++中的重载包括函数重载和运算符重载两种,函数重载相对比较简单,无非就是同名函数实现不同的功能,当然其形参必须是不一样的。而运算符重载相对较难,当然也仅仅是相较而已,运算符重载说到底也就是利用让运算符实现更多的功能(这个功能是自己定义的),下面详细讲讲其要怎么实现吧。

1.函数重载

  在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。您不能仅通过返回类型的不同来重载函数。

下面的实例中,同名函数 print() 被用于输出不同的数据类型:

#include <iostream>
using namespace std;
 
class printData
{
   public:
      void print(int i) {
        cout << "整数为: " << i << endl;
      }
 
      void print(double  f) {
        cout << "浮点数为: " << f << endl;
      }
 
      void print(char c[]) {
        cout << "字符串为: " << c << endl;
      }
};
 
int main(void)
{
   printData pd;
 
   // 输出整数
   pd.print(5);
   // 输出浮点数
   pd.print(500.263);
   // 输出字符串
   char c[] = "Hello C++";
   pd.print(c);
 
   return 0;
}

在这里插入图片描述

2.运算符重载

  您可以重定义或重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符。

重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

Box operator+(const Box&);

声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数,如下所示:

Box operator+(const Box&, const Box&);

举例:

#include <iostream>
using namespace std;
 
class Box
{
   public:
 
      double getVolume(void)
      {
         return length * breadth * height;
      }
      void setLength( double len )
      {
          length = len;
      }
 
      void setBreadth( double bre )
      {
          breadth = bre;
      }
 
      void setHeight( double hei )
      {
          height = hei;
      }
      // 重载 + 运算符,用于把两个 Box 对象相加
      Box operator+(const Box& b)
      {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }
   private:
      double length;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
};
// 程序的主函数
int main( )
{
   Box Box1;                // 声明 Box1,类型为 Box
   Box Box2;                // 声明 Box2,类型为 Box
   Box Box3;                // 声明 Box3,类型为 Box
   double volume = 0.0;     // 把体积存储在该变量中
 
   // Box1 详述
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);
 
   // Box2 详述
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);
 
   // Box1 的体积
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;
 
   // Box2 的体积
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;
 
   // 把两个对象相加,得到 Box3
   Box3 = Box1 + Box2;
 
   // Box3 的体积
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;
 
   return 0;
}

在这里插入图片描述
再举一个例子:[ ]重载

#include <iostream>
using namespace std;
const int SIZE = 10;
 
class safearay
{
   private:
      int arr[SIZE];
   public:
      safearay() 
      {
         register int i;
         for(i = 0; i < SIZE; i++)
         {
           arr[i] = i;
         }
      }
      int& operator[](int i)
      {
          if( i > SIZE )
          {
              cout << "索引超过最大值" <<endl; 
              // 返回第一个元素
              return arr[0];
          }
          return arr[i];
      }
};
int main()
{
   safearay A;
 
   cout << "A[2] 的值为 : " << A[2] <<endl;
   cout << "A[5] 的值为 : " << A[5]<<endl;
   cout << "A[12] 的值为 : " << A[12]<<endl;
 
   return 0;
}

在这里插入图片描述
此外想看更多关于重载的细节请看博客:https://www.runoob.com/cplusplus/subscripting-operator-overloading.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值