面向对象C++实验报告

上传人:飞*** 文档编号:26978392 上传时间:2018-01-04 格式:PDF 页数:36 大小:887.58KB
返回 下载 相关 举报
面向对象C++实验报告_第1页
第1页 / 共36页
面向对象C++实验报告_第2页
第2页 / 共36页
面向对象C++实验报告_第3页
第3页 / 共36页
面向对象C++实验报告_第4页
第4页 / 共36页
面向对象C++实验报告_第5页
第5页 / 共36页
点击查看更多>>
资源描述

《面向对象C++实验报告》由会员分享,可在线阅读,更多相关《面向对象C++实验报告(36页珍藏版)》请在金锄头文库上搜索。

1、面向对象程序设计 -C+ 上机实验报告第 一 次:熟悉 Visual C+ 开发环境及上机过程实验实验时间: 2015 年 3 月 11 日 实验室: S311 实验题目:使用 Visual C+ 6.0 集成环境来编辑、编译并运行程序 1:#include using namespace std; class TAdd public: TAdd(int a,int b) x=a;y=b; cout #include using namespace std; class student public: student() student(char *id,char *name,int age)

2、 strcpy(stuId,id); strcpy(stuName,name); stuAge = age; void display() cout # include using namespace std; class Date private: int year,month,day; public: void set(int y,int m,int d); bool isLeapYear() return (year%4=0 friend Date void print(); void Date:set(int y,int m,int d) if(!(m12) month=m; if(m

3、=12|m=10|m=8|m=7|m=5|m=3|m=1) if(!(d31) day=d; else if(!(d30) day=d; else exit(1); else exit(1);year=y; void Date:print() cout=1;n-)s*=n;return s; int cnr(int n,int r) int s;for(s=1;nr;n-) s*=n;return s; ( 4)主函数实现: #include 函数原型 .h #include 主函数声明 .h #include using namespace std; void main() cout #in

4、clude using namespace std; const float PI=3.14f; /几何类class Geometry public: void getR(float r) this-r=r; protected: float r; ; /球类class Qiu:public Geometry public: float getArea(float r) getR(r); s=4*PI*r*r; return s; float getVolume(float r) getR(r); v=(4*PI*r*r*r)/3; return v; private: float s,v;

5、; /柱类class Zhu:public Geometry public: float getArea(float r,float h) getR(r); this-h=h; s=2*PI*r*h+2*PI*r*r; return s; float getVolume(float r,float h) getR(r); this-h=h; v=PI*r*r*h; return v; private: float s,v,h; ; /锥类class Zhui:public Geometry public: float getArea(float r,float h) getR(r); floa

6、t l=sqrt(h*h+r*r); s=PI*r*l+PI*r*r; return s; float getVolume(float r,float h ) getR(r); this-h=h; v=(PI*r*r*h)/3; return v; private: float s,v,h; ; void main() Qiu a; Zhu b; Zhui c; float s1,s2,s3; float v1,v2,v3; s1=a.getArea(3); v1=a.getVolume(3); s2=b.getArea(3,2); v2=b.getVolume(3,2); s3=c.getA

7、rea(3,2); v3=c.getVolume(3,2); cout #include using namespace std; class person public: void print() coutnumber; coutname; void show() coutNO; coutscore; void show() person:show(); couttitle; coutdept; void show() person:show(); cout using namespace std; class B1 public: B1(int i) b1 = i; cout using

8、namespace std; class B1 public: B1(int i) b1 = i; cout using namespace std; class Fenshu/ 定义一个 Fenshu 类public: Fenshu(int zi=0,int mu=1):fenzi(zi),fenmu(mu); void reduction(); void display(); Fenshu operator+(const Fenshu /两个分数相加,结果要化简Fenshu operator-(const Fenshu /两个分数相减,结果要化简Fenshu operator*(const

9、 Fenshu /两个分数相乘,结果要化简Fenshu operator/(const Fenshu /两个分数相除,结果要化简private: int fenzi; /分子int fenmu; /分母; void Fenshu:reduction() int m,n,r,small,max; m=abs(fenmu); / 取绝对值n=abs(fenzi); if(m using namespace std; class Fenshu public: Fenshu(int zi=0,int mu=1):fenzi(zi),fenmu(mu); void reduction(); void d

10、isplay(); friend Fenshu operator+(const Fenshu / 友元函数实现两个分数相加friend Fenshu operator-(const Fenshu / 友元函数实现两个分数相减friend Fenshu operator*(const Fenshu / 友元函数实现两个分数相乘friend Fenshu operator/(const Fenshu / 友元函数实现两个分数相除private: int fenzi; /分子int fenmu; /分母; void Fenshu:reduction() int m,n,r,small,max; m=

11、abs(fenmu); n=abs(fenzi); if(m #include using namespace std; class Base public: Base() cout) coutf(1.0); b.g(); d.g(); pb-g(); delete pb; return 0; 2、录入下面程序,分析运行结果:#include using namespace std; class Base public: Base():data(count) cout(pb)+2)-data1g(); 基类的指针指向派生类分析:sizeof(class )计算类的大小所有的虚函数共享一个字节(

12、 4)故:sizeof(Base)=4 ;自身的函数一般不占有字节,但是 base的基础上增加了两个 int 类数据故:sizeof(Derived)=12 构造函数的顺序先基类(按定义) ,在对象成员(按定义) ,后派生类(按定义) ;解析函数的顺序与构造函数顺序相反心得体会:多态是指同样的消息被不同类型的对象接受时导致不同的行为, 多态分为编译时多态和运行时多态两种。虚函数是不占有内存但可被继承的函数。第 八 次: 抽象类的使用 实验实验时间: 2015 年 6 月 3 日 实验室: S311 实验题目 :抽象类的使用实验目的 :1、了解抽象类的概念2 、灵活应用抽象类。实验要求:1、 写

13、出程序,并调试程序,要给出测试数据和实验结果。2、 整理上机步骤,总结经验和体会。3、 完成实验报告和上交程序。实验要求:1、录入下面程序,分析编译错误信息。#include #include #include using namespace std; class Abstract public: Abstract() cout f(); return 0; 2、基类 shape 类是一个表示形状的抽象类, area( ) 为求图形面积的函数。请从 shape 类派生三角形类 (triangle) 、圆类( circles )、并给出具体的求面积函数。#include class shape

14、public: virtual float area( )=0 ;; 3、定义一个抽象类 Base, 在该类中定义一个纯需函数 virtual void abstractMethod() = 0; 派生一个基于 Base 的派生类 Derived , 在派生类 Derived的 abstractMethod 方法中输出 Derived:abstractMethod is called ,最后编写主函数,其内容如下:int main() Base* pBase = new Derived; pBase-abstractMethod(); delete pBase; return 0; 实验结果:

15、1、 virtual void f() = 0; 为纯虚函数故 Abstract *p = new Abstract; 抽象类不能实例化2. #include using namespace std; const double pi=3.14159; class shape public: virtual double area( )=0 ; ; class circle:public shape public: circle(double r):r(r) double area() coutarea(); p1= p1-area(); 3. #include using namespace std; class Base public: virtual void abstractMethod() = 0; ; class Derived:public Base public: virtual void abstractMethod() coutabstractMehtod(); void abstractMehtod() coutabstractMetho

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 研究报告 > 技术指导

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号