当前位置:首页 > 实验指导(C++面向对象部分)
高级程序设计语言
本 学 期 实 验 指
辽宁科技大学
导
实验内容
实验一 类与对象基本程序设计(一) 【实验类型】验证性 【实验要求】必做
【实验目的】
1.了解C++面向对象程序设计的基本概念以及了解C++程序的基本结构。 2.了解类,对象的概念,掌握类及类中成员函数的定义及使用方法。 3.掌握对象的定义及使用方法。
4.了解构造函数,析构函数,拷贝构造函数的作用,特点,定义方式及使用方法。
【实验内容】
实验题1.定义一个FDAccount类,用以描述一个定期存折(fixed deposit),实现现金支取。余额合计。信息显示等。存折基本信息包括帐号,账户名称,存款余额,存取期限(以月为单位),存款利率(以百分点为单位)等。
提示:存折的基本信息定义为存折类的私有的数据成员,利用构造函数的初始化存折类对象,在利用构造函数的参数表传入实参,初始化存折对象的数据成员。
实验题2.设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个顶点的坐标,该类能够计算矩形的面积。
提示:两个点的坐标作为矩形类的数据成员。利用坐标计算矩形长和宽,然后求面积。
实验题3.设计一个person类,其属性包括name和id,其中name属性为指针,分别利用person类构造函数和拷贝构造函数建立对象,打印每个person类对象的信息。要求分别编写浅拷贝构造函数和深拷贝构造函数调试程序。
提示:要在构造函数中为person对象的name属性分配动态内存,在析构函数中,释放掉申请的动态内存。注意不要让同一块动态内存被释放多次。 【参考程序】
实验题1参考程序 #include
public:
//构造函数
FDAccount(char *ID,char *depositor,double amount,int period,double rate);
double fetch(char *ID,char *depositor,double amount); //支取到期存款 void update();//计算到期账户余额 void show(); //显示账户基本信息 protected:
double interest_rate;
private:
char *accounts; char *name;
double balance; //存款余额 int term; //存款期限 };
FDAccount::FDAccount(char *ID,char *depositor,double amount,int period,double rate) {
name=depositor; accounts=ID;
if((amount<0||rate<0)) {
cout<<\数据不正确\ exit(1); }
balance=amount; term=period;
interest_rate=rate; }
double FDAccount::fetch(char *ID,char *depositor,double amount) {
cout<<\帐号 \账户名称 \支取金额 \
cout< void FDAccount::update() { balance=balance+balance*(interest_rate/100.00)*(term/12.0); } void FDAccount::show() { cout<<\显示账户基本信息:\ cout<<\帐号 \帐号名称 \期限 \利率 \ cout< void main() { FDAccount depositor(\王涛\ depositor.show(); cout< cout<<\存款已到期!\\n\ depositor.update(); depositor.show (); cout< cout<<\支取存款:\ depositor.fetch(\王涛\ cout< depositor.show(); } 实验题2参考程序 #include public: Rectangle(int top ,int left,int bottom,int right); ~Rectangle(){} int GetTop() const {return itsTop;} int GetLeft() const {return itsLeft;} int GetBottom() const {return itsBottom;} int GetRight() const {return itsRight;} void SetTop(int top) {itsTop=top;} void SetLeft(int left) {itsLeft=left;} void SetBottom(int bottom) {itsBottom=bottom;} void SetRight(int right) {itsRight=right;} int GetArea() const; private: int itsTop; int itsLeft; int itsBottom; int itsRight; }; Rectangle::Rectangle(int top,int left,int bottom,int right) { itsTop=top; itsLeft=left; itsBottom=bottom; itsRight=right; } int Rectangle::GetArea()const {
共分享92篇相关文档