- 实验目的
类使得自定义类型表现和基本类型相似,继承则表示若干类型/概念之间存在相似性的事实,重置则表示若干类型的对象具有接口相同、但语义不一定相同的操作。本次实验通过设计并实现一些程序,了解并熟悉类型继承、虚函数、方法重置机制解决问题的基本方法,提高学生对面向对象方法的理解和应用能力。
- 实验环境
操作系统:Windows10
开发工具:Visual Studio2010
- 实验内容
题目一:
Define
class base {
public:
virtual void iam () { cout << "base\n"; }
void hello () { cout << "base::hello"; }
};
Derive(派生) two classes from base , and for each define iam() to write out the name of the class, and for each define hello() to write out any different text. Create objects of these classes and call iam() and hello() for them. Assign pointers to objects of the derived classes to base* pointers and call iam() and hello() through those pointers.
题目二:
Consider:
class Char_vec {
int sz ;
char element [1];
public :
static Char_vec * new_char_vec (int s );
char & operator [](int i )
{ return element[i]; }
// ...
};
Define new_char_vec() to allocate contiguous memory for a Char_vec object so that the elements can be indexed through element as shown.
题目三:
Given classes Circle, Square, and Triangle derived from a class Shape, define a function intersect() that takes two Shape* arguments and calls suitable functions to determine if the two shapes overlap(重叠). It will be necessary to add suitable (virtual) functions to the classes to achieve this. Don’t bother to write the code that checks for overlap; just make sure the right functions are called. 【This is commonly referred to as double dispatch or a multi-method.】
- 实验步骤
实验一:
- 实验思路:定义 Base 的两个派生类FirstDerived和SecondDerived,要求它们各自重新定义成员函数 iam() 和hello(),然后用测试函数查看调用的情况。
- 数据结构: 无
- 类的接口定义:
class Base{
public:
virtual void iam(){cout << "Base\n";}
};
class FirstDerived :public Base{
public:
virtual void iam(){cout << "FirstDrived\n"; }
void hello() {cout << "first hello" << endl; }
};
class SecondDerived :public Base{
public:
virtual void iam(){cout << "SecondDrived\n";}
void hello() {cout << "Second hello" << endl;}
};
实验源码
https://download.csdn.net/download/weixin_52552833/87229427
问题和解决:无
实验二:
- 实验思路:使用new操作符实现字符缓冲区。
- 数据结构:无
- 类的接口定义:
class Char_vec{
int sz;
char element[1];
public:
static Char_vec * new_char_vec(int s);
char & operator [ ](int i){ return element[i]; }
};
实验源码
https://download.csdn.net/download/weixin_52552833/87229428
5.问题和解决:无
实验三:
1.实验思路:构造shape的抽象类,派生出Circle, Square,和Triangle派生类,最后构造intersect函数进行派生类的调用。
2.数据结构:无
3.函数的接口定义:
class Shape{
public:
virtual bool closed() = 0;
virtual void rotate(int) = 0;
virtual void draw() = 0;
};
class Point{
friend class Shape;
int x;
int y;
public:
Point(int i = 0, int j = 0) :x(i), y(j) {}
};
class Circle :public Shape{
int radius;
Point center;
public:
Circle(int r = 0, int i = 0, int j = 0) :radius(r), center(i, j);
bool closed();
void rotate(int u);
void draw();
};
class Square :public Shape{
int length;
public:
Square(int i) :length(i) { cout << "it is a square!" << endl; }
bool closed();
void rotate(int u);
void draw();
};
class Triangle :public Shape{
int line1;
int line2;
int line3;
public:
Triangle(int i = 0, int j = 0, int k = 0) :line1(i), line2(j), line3(k);
bool closed();
void rotate(int u);
void draw();
};
bool intersect(Shape * a, Shape *b){
a->draw();
b->draw();
return 1;
}
实验源码
https://download.csdn.net/download/weixin_52552833/87229430
问题和解决:无
- 实验结果
题目一:
输入:
Base b;
FirstDerived fd;
SecondDerived sd;
cout << "Base:" << endl;
b.iam();
cout << "First:" << endl;
fd.iam();
fd.hello();
cout << "Second:" << endl;
sd.iam();
fd.hello();
Base *q = &b, *q1 = &fd, *q2 = &sd;
q->iam();
q1->iam();
(*q2).iam();
输出:
Base:
Base
First:
FirstDrived
first hello
Second:
SecondDrived
first hello
Base
FirstDrived
SecondDrived
题目二:
输入:
Char_vec c;
c.new_char_vec(5);
c[1]='A';
cout << c[1] << endl;
输出:
A
题目三:
输入:
Circle c1(3, 4, 5);
Circle c2(7, 8);
Circle *pc = &c2;
Square s1(5);
Square s2(6);
Square *ps = &s2;
Triangle t1(6, 10, 8);
Triangle t2(5, 3, 4);
Triangle *pt = &t2;
intersect(ps, pt);
intersect(pc, pt);
intersect(&c1, &s1);
intersect(&s1, &t1);
输出:
it is a circle!
it is a circle!
it is a square!
it is a square!
it is a triangle!
it is a triangle!
using square draw!
using triangle draw!
using circle draw!
using triangle draw!
using circle draw!
using square draw!
using square draw!
using triangle draw!
- 实验总结
本次实验学习了类的派生,函数的重置,虚函数的使用。加深了对面向对象方法的理解和应用能力。加深了我对基于类的派生的理解;同时明白自己的不足之处,希望以后的日子能逐渐提高关于这方面的能力。