作业

作业1:
1、编写一个乐器(Instrument)类。
子类有钢琴(Piano)和小提琴(Violin),各种乐器的弹奏(play)方法各不相同,编写main函数测试乐器。


2、电脑类,子类是笔记本和台式机,虚函数是showInfo();


3、测试哪个答案是正确的。




4 求表面积和体积
设计一个抽象类CSolid,含有求表面积及体积的纯虚函数。设计个派生类CCube、CBall、CCylinder,分别表示正方体、球体及圆柱体。在main()函数中,定义基类的指针p(CSolid *p;),利用p指针,输出正方体、球体及圆柱体对象的表面积及体积。


5 有一个交通工具类vehicle,将它作为基类派生小车类car、卡车类truck和轮船类boat,定义这些类并定义一个虚函数用来显示各类信息。


6 矩形法(rectangle)积分近似计算公式为:
    Δx(y0+y1+….yn-1) =deltax*y0+deltax*y1
   梯形法(1adder)积分近似计算公式为:    
        [y0+2(y1+….yn-1)+yn]
   辛普生法(simpson)积分近似计算公式(n为偶数)为:
[y0+ yn +4(y1+y3….yn-1)+2(y2+y4+…yn-2)]
被积函数用派生类引入,定义为纯虚函数。基类(integer)成员数据包括积分上下限b和a,分区数n,步长step=(b-a)/n,积分值result。定义积分函数integerate()为虚函数,它只显示提示信息。派生的矩形法类(rectangle)重定义integerate(),采用矩形法做积分运算。        派生的梯形法类(1adder)和辛普生法(simpson)类似。试编程,用3种方法对下列被积函数进行定积分计算,并比较积分精度。
       (1)sin(x),下限为0.0,上限为pir/2。
       (2)exp(x),下限为0.0,上限为1.0。    
(3)4.0/(1+x×x),下限为0.0,上限为1.0。




7 某学校对教师每月工资的计算公式如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干教师的月工资。
//#include <iostream>
//using namespace std;
//class Instrument
//{
//public:
// virtual void play()
// {
// cout<<"play Instrument"<<endl;
// }
//};
//class Pinao : public Instrument
//{
//public:
// void play()
// {
// cout<<"play Pinao"<<endl;
// }
//};
//class Violin : public Instrument
//{
//public:
// void play()
// {
// cout<<"play Violin"<<endl;
// }
//};
//int main()
//{
// Instrument *a = new Pinao;
// a->play ();
// Instrument *b = new Violin;
// b->play();
// system("pause");
// return 0;
//}






//#include <iostream>
//using namespace std;
//class Computer
//{
//public:
// virtual void showInfo()
// {
// cout<<"我是一台电脑"<<endl;
// }
//};
//class Note : public Computer
//{
//public:
// void showInfo()
// {
// cout<<"我是一台笔记本电脑"<<endl;
// cout<<"型号   :HP3700V"<<endl;
// cout<<"厂商   :Compag"<<endl;
// cout<<"价格     :6900元"<<endl;
// cout<<"内存容量 :512M"<<endl;
// cout<<"出厂日期 :2008.8.8"<<endl;
// cout<<"电池容量 :150分钟"<<endl;
// }
//};
//class Taishi : public Computer
//{
//public:
// void showInfo()
// {
// cout<<"我是一台台式机"<<endl;
// cout<<"型号   :HP68T"<<endl;
// cout<<"厂商   :Compag"<<endl;
// cout<<"价格     :4799元"<<endl;
// cout<<"内存容量 :512M"<<endl;
// cout<<"出厂日期 :2007.7.23"<<endl;
// cout<<"机箱类型 :立式"<<endl;
//
// }
//};
//int main()
//{
// Computer *a = new Note;
// a->showInfo ();
// Computer *b = new Taishi;
// b->showInfo ();
// system("pause");
// return 0;
//}






//#include <iostream>
//using namespace std;
//class ClassA
//{
//public:
// virtual ~ClassA(){};
// virtual void FunctionA(){cout<<"1"<<endl;};
//};
//class ClassB
//{
//public:
// virtual void FunctionB(){cout<<"2"<<endl;}
//};
//class ClassC : public ClassA,public ClassB
//{
//public:
//};
//int main()
//{
// ClassC aObject;
// ClassA *pA = &aObject;
// pA->FunctionA ();
// ClassB *pB = &aObject;
// pB->FunctionB ();
// ClassC *pc = &aObject;
// system("pause");
// return 0;
//}






//#include <iostream>
//using namespace std;
//class CSolid
//{
//public:
// virtual double S() = 0;
// virtual double V() = 0;
//};
//class CCube:public CSolid
//{
//public:
// double S();
// double V();
// CCube(double n):a(n){}  
//private:  
//    double a;  
//};
//double CCube::S ()
//{
// return 6*a*a;
//}
//double CCube::V ()
//{
// return a*a*a;
//}
//class CBall:public CSolid
//{
//public:
// double S();
// double V();
// CBall(double n):r(n){}
//private:
// double r;
//};
//double CBall::S()
//{
// return 4*3.14*r*r;
//}
//double CBall::V ()
//{
// return 3.14*r*r*r*4/3;
//}
//class CCylinder:public CSolid
//{
//public:
// double S();
// double V();
// CCylinder(double n,double s):r(n),h(s){}
//private:
// double r,h;
//};
//double CCylinder::S()
//{
// return 2*3.14*r*r+2*3.14*r*h;
//}
//double CCylinder::V()
//{
// return 3.14*r*r*h;
//}
//int main()
//{
// double s,v;
// CSolid *p = new CCube(5);
// s = p->S();
// v = p->V();
// cout<<"面积是 "<<s<<" 体积是 "<<v<<endl;
// p = new CBall(1);
// s = p->S ();
// v = p->V ();
// cout<<"面积是 "<<s<<" 体积是 "<<v<<endl;
// p = new CCylinder(1,5);
// s = p->S ();
// v = p->V ();
// cout<<"面积是 "<<s<<" 体积是 "<<v<<endl;
// system("pause");
// return 0;
//
//}








//#include <iostream>
//using namespace std;
//class vehicle
//{
//public:
// virtual void app(){cout<<"1"<<endl;}
//
//};
//class car : public vehicle
//{
//public:
// void app()
// {
// cout<<"car"<<endl;
// }
//};
//class truck : public vehicle
//{
//public:
// void app()
// {
// cout<<"truck"<<endl;
// }
//};
//class boat:public vehicle
//{
//public:
// void app()
// {
// cout<<"boat"<<endl;
// }
//};
//int main()
//{
// vehicle *p = new car;
// p->app ();
// p = new truck;
// p->app ();
// p = new boat;
// p->app ();
// system("pause");
// return 0;
//}








//#include <iostream>
//#include <cmath>
//using namespace std;
//class G
//{
//public:
// virtual long a() = 0;
//};
//class J:public G
//{
//public:
// J(int m):n(m){}
// long a()
// {
// return 5000+50*n;
// }
//private:
// int n;
//};
//class Fj:public G
//{
//public:
// Fj(int c):b(c){};
// long a()
// {
// return 3000+30*b;
// }
//private:
// int b;
//};
//class Js:public G
//{
//public:
// Js(int s):t(s){};
// long a()
// {
// return 2000+20*t;
// }
//private:
// int t;
//};
//int main()
//{
// G *p = new J(20);
// cout<<p->a ()<<endl;
// p = new Fj(20);
// cout<<p->a ()<<endl;
// p = new Js(20);
// cout<<p->a ()<<endl;
// system("pause");
// return 0;
//}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值