- 49
- 0
- 约 11页
- 2017-01-12 发布于重庆
- 举报
实验报告3继承与多态.
计 算 机 课 程 实 验 报 告
2012—2013学年度 第一 学期
系别:数学与计算机科学学院
实验课程 C++面向对象程序设计 班 级 级计算机科学与技术 学 号 11 姓 名 蔡兴明 指导教师 马学梅 实验题目 继承与多态(一) 日 期 2012-9-29 实验目的
及要求 1、理解继承在面向对象程序中的重要作用、继承和派生的概念;
2、掌握#include iostream.h
class COMPLEX
{ public:
COMPLEX (double r=0,double i=0);
COMPLEX(const COMPLEX other);
void print( );
COMPLEX add(const COMPLEX other);
COMPLEX subs(const COMPLEX other);
protected:
double real, image;
};
COMPLEX:: COMPLEX ( double r, double i)
{ real = r; image =i; return; }
COMPLEX:: COMPLEX( const COMPLEX other )
{ real = other.real; image =other.image; return;}
void COMPLEX:: print( )
{ cout real;
if (image0) cout “+”image“i”;
else if (image 0) cout image “i”;
cout “\n”; return;
}
COMPLEX COMPLEX ::add (const COMPLEX other )
{ real = real+other.real;
image =image+other.image;
return *this;
}
COMPLEX COMPLEX ::subs(const COMPLEX other )
{ real = real-other.real;
image =image-other.image;
return *this;
}
int main ( )
{
COMPLEX c1(1,2) ;
COMPLEX c2(2) ;
COMPLEX c3(c1) ;
c3.print ( );
c2.add(c1);
c3. subs(c2);
c3.print( );
return 0;
}
给出输出结果,分析this的用途.在此程序的基础上实现运算符”+”和”-“的重载
this是自身的地址,但是*this就是自身了.是按值回返了.如果函数的回返值是带有号的,那么回返值就是引用了一种情况就是,在类的非静态成员函数中返回类对象本身的时候,直接使用 return *this;另外一种情况是当参数与成员变量名相同时,如this-n = n (不能写成n = n)。
++的前置和后置重载,但实际上并没有实现。试分析原因,并作出修改,以实现此功能。
#include iostream.h
class OperClass
{ int x;
public:
OperClass( );
OperClass operator ++( );
OperClass operator ++(int );
void display( );
};
OperClass:: OperClass( ) { x=0; return;}
OperClass OperClass :: operator++( )
{OperClass A; ++x; A.x=x; return A;}
OperClass OperClass :: operator++(int )
{OperClass A; x ++;
您可能关注的文档
- 实验心理学串讲笔记..doc
- 实验心理学报告差别阈限..doc
- 实验心理学报告迷宫实验..doc
- 实验心理学笔记y..doc
- 实验心理学笔记汇总..doc
- 实验心理学考试重点..doc
- 实验心理干预实验报告给学生..doc
- 实验必须遵循的三大基本原则..doc
- 实验性家兔酸碱平衡紊乱及其治疗..doc
- 实验总结报告--西津古渡温度控制..doc
- 2025-2026学年天津市和平区高三(上)期末数学试卷(含解析).pdf
- 2025-2026学年云南省楚雄州高三(上)期末数学试卷(含答案).pdf
- 2025-2026学年甘肃省天水市张家川实验中学高三(上)期末数学试卷(含答案).docx
- 2025-2026学年福建省厦门市松柏中学高二(上)期末数学试卷(含答案).docx
- 2025-2026学年广西钦州市高一(上)期末物理试卷(含答案).docx
- 2025-2026学年河北省邯郸市临漳县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省石家庄二十三中七年级(上)期末历史试卷(含答案).docx
- 2025-2026学年海南省五指山市九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省唐山市玉田县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省邢台市市区九年级(上)期末化学试卷(含答案).docx
原创力文档

文档评论(0)