- 实验目的
类使得自定义类型表现和基本类型相似,操作符重载机制则可使得自定义类型的一些操作的使用与习惯一致。本次实验通过使用类、操作符重载机制来设计并实现一些程序,了解并熟悉使得自定义类型更便于使用的手段,加深学生对基于类的操作符重载机制的理解,进一步提高面向对象编程能力。
- 实验环境
操作系统:Windows7
开发工具:Visual Studio2010
- 实验内容
题目一:
Define a class INT that behaves exactly like an int. Hint: Define INT::operator int().
定义类INT并使之在行为上完全像int。提示:定义INT::operator int()
题目二:
Modify the pattern match facility from §11.14[11] to work on the standard library string. Note that you cannot modify the definition of string.
修改取自11.14[11]的模式匹配功能函数,使之能对标注库string使用,请注意,你不能修改string的定义。
题目三:
Define a type Vec4 as a vector of four floats. Define operator[] for Vec4. Define operators +, -, *, /, =, +=, =, *=, and /= for combinations of vectors and floating point numbers.
要求定义一个类(名为 Vec4),它表示由四个float分量构成的“向量”。该类型除了定义:缺省构造函数、拷贝构造函数、转换构造函数、读取/修改数据成员的那些接口操作之外,其他操作都是用题中给出的操作符命名的。
题目四:
Complete class Ptr_to_T from §11.11 and test it. To be complete, Ptr_to_T must have at least the operators *, ->, =, ++, and -– define. Do not cause a run-time error until a wild pointer is actually dereferenced.
教材中的 Ptr_to_T 类实际上就是表示 “指向数组 T [n] 的指针”这一概念的类型,该类型提供指针相关的若干运算,并分别将它们用操作符命名。且该类型定义的指针运算应当提供“访问越界”检查,并在发现越界时报告错误。
- 实验步骤
实验一:
- 实验思路:按照符号重载规则,模拟int的属性构造类INT的符号重载。
- 数据结构: 无
- 类的接口定义:
class INT{
int x;
public:
INT(int x1 = 0) { x = x1; }
~INT() {};
INT operator++();
INT operator++(int);
INT operator--();
INT operator--(int);
operator int() const { return x; } //类型转换函数
void print() { cout << "x= " << x << endl; }
};
实验源码:
https://download.csdn.net/download/weixin_52552833/87229408
5.问题和解决:无
实验二:
- 实验思路:按照符号重载规则,模拟string的属性构造类String的符号重载。
- 数据结构:无
- 类的接口定义:
class String{
friend bool operator == (const String &, const String &);
friend bool operator != (const String &, const String &);
friend bool operator < (const String &, const String &);
friend bool operator > (const String &, const String &);
friend bool operator <= (const String &, const String &);
friend bool operator >= (const String &, const String &);
friend ostream &operator <<(ostream &os, const String &s);
friend istream &operator >> (istream &os, String &s);
public:
String(){ pstr_ = new char[1];}
String(const char *s){pstr_ = new char[strlen(s) + 1];strcpy(pstr_, s); }
String(const String &rhs);
String &operator = (const String &rhs);
String &operator = (const char *s) ;
String & operator +=(const String & rhs);
String &operator += (const char *pstr);
char &operator[](size_t index);
const char &operator[](size_t index) const;
size_t size() const{ return strlen(pstr_); }
const char* c_str()const{return pstr_;}
~String(){delete[]pstr_; }
void print();
private:
char *pstr_;
};
实验源码
https://download.csdn.net/download/weixin_52552833/87229416
5.问题和解决:无
实验三:
1.实验思路:运用函数重载规则和4维向量的操作属性实现类Vec4的操作符号重载。
2.数据结构:无
3.函数的接口定义:
class Vec4{
float s[4];
class Error { };
public:
Vec4(float a = 0.0, float b = 0.0, float c = 0.0, float d = 0.0)
void check(int i)
float operator[ ](int i)
friend Vec4 operator+(const Vec4& a, const Vec4& b)
friend Vec4 operator-(const Vec4& a, const Vec4& b)
friend Vec4 operator*(const Vec4& a, const Vec4& b)
friend Vec4 operator/(const Vec4& a, const Vec4& b)
Vec4& operator=(Vec4& a)
Vec4 operator+=(Vec4& a)
Vec4 operator-=(Vec4& a)
Vec4 operator*=(Vec4& a)
Vec4 operator/=(Vec4& a)
}
4.实验源码
https://download.csdn.net/download/weixin_52552833/87229421
问题和解决:无
实验四:
1.实验思路:运用函数重载规则和指针操作性质实现类Ptr_to_T的操作符重载。
2.数据结构:无
3.函数的接口定义:
class Ptr_to_T{
private:
T v[n];
T* p;
int size;
public:
Ptr_to_T() :size(n), p(v);
Ptr_to_T(const Ptr_to_T& other);
~Ptr_to_T(){ delete[] p; }
Ptr_to_T& operator++()
Ptr_to_T operator++(int)
Ptr_to_T operator--()
Ptr_to_T operator--(int)
T operator*()
T operator->()
Ptr_to_T& operator=(Ptr_to_T& other)
};
4.实验源码
https://download.csdn.net/download/weixin_52552833/87229424
5.问题和解决:无
- 实验结果
题目一:
输入:
INT a(10), b(2), c(4), d(32), e(15), f(63), g(27), h(38);
c = a + b; c.print();
d = h - g; d.print();
e = c*e; e.print();
f = h / b; f.print();
g = h%c; g.print();
a++; a.print();
--b; b.print();
输出:
x= 12
x= 11
x= 180
x= 19
x= 2
x= 11
x= 1
题目二:
输入:
String s1;
s1.print();
String s2 = "hello";
s2.print();
String s3 = s2;
s3.print();
String s6 = "hello";
String s7 = "world";
s6 += s7;
s6.print();
String s10 = "hello";
String s11 = "world";
if (s10 != s11)
{
cout << "unequal" << endl;
}
String s14 = "hello";
String s15 = "world";
cout << s14 + s15 << endl;
输出:
string : /n
string :hello
string :hello
string :helloworld
unequal
helloworld
题目三:
输入:
Vec4 a(2, 5, 4), b(3, 8, 9, 4), c;
c = a + b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;
c = a - b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;
c = a*b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;
c += b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;
c -= b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;输出:
5 13 13 4
-1 -3 -5 -4
6 40 36 0
9 48 45 4
6 40 36 0
题目四:
输入:
Vec4 a(2, 5, 4), b(3, 8, 9, 4), c;
c = a + b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;
c = a - b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;
c = a*b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;
c += b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;
c -= b;cout << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << endl;输出:
5 13 13 4
-1 -3 -5 -4
6 40 36 0
9 48 45 4
6 40 36 0
- 实验总结
本次实验通过使用类、操作符重载机制来设计并实现一些程序,让我了解并熟悉了使得自定义类型更便于使用的手段,加深了我对基于类的操作符重载机制的理解;同时明白自己的不足之处,希望以后的日子能逐渐提高关于操作