1、 endl;CBook() delete p_book;void main()char book_title60;CBook *p_book_obj; cout p;CBook abook(p); p_book_obj = &abook; p_book_obj-print();三、根据如下所示程序,回答下列问题(共10分)class CJournalCJournal() cout “Journal default constructor” virtual void subscribe() = 0;void read() cout “Read paper” CJournal() cout “Jo
2、urnal default destructor” class CComputerDesign : public CJournalCComputerDesign () cout “Computer Designdefault constructor” virtual void subscribe() cout “Subscribing Computer Design” void read() cout “Reading Computer Design” CComputerDesign() cout “Computer Designdefault destructor” subscribe();
3、read();1.当程序运行到处时,写出程序运行的输出结果 (3分)2. 当程序结束时,程序会在第1问的基础上增加哪些输出。 (4分)3. 若在主函数中定义一个对象CJournal journal;程序编译时会否出错?为什么?(3分)四、编程题(共40分)1. 定义一个商品类CGoods,其中包含商品号(long no)、商品名(char *p_name)、商品价格(double price)三个数据成员,以及相应的构造函数、拷贝构造函数、析构函数、打印数据成员的成员函数。(10分)iostreamstring.husing namespace std;class CCourselong no
4、;char *p_name;float credit;CCourse(long no_val,char p_val,float credit_val); CCourse(const CCourse &r_course);CCourse()delete p_name;void print() const;CCourse:CCourse(long no_val,char p_val,float credit_val)no=no_val;p_name=new charstrlen(p_val)+1;strcpy(p_name,p_val);credit=credit_val;CCourse(cons
5、t CCourse &r_course)no=r_course.no;p_name=new charstrlen(r_course.p_name)+1;strcpy(p_name,r_course.p_name);credit=r_course.credit;void CCourse:print() constcoutCourse numbernoendl;Course namep_nameCourse creditcredit2. 为CGoods类增加一个商品总数(int count)数据成员,并增加一个成员函数getCount()获取count的值,编写一个友元函数getName()获取商
6、品名称p_name。做如上修改后,重新实现CGoods类(与第1问相同的不用再重复)。3. 为CGoods类定义小于运算符(=)两个运算符重载函数。CGoods类对象大小的比较是根据其商品价格(price)的值的大小来实现的。(与第2问相同的不用再重复)(10分)4. 以CGoods类为基类,派生出服装类CClothes和食品类CFood两个派生类,并在这两个类中分别增加一个表示品牌的指针数据成员(char *p_brand)和表示用途的成员函数(void usedFor()可分别输出一条表示服装和食品用途的信息)。写出CClothes类和CFood类的完整定义(包括构造、析构和usedFor
7、()成员函数的实现)。二、下面的程序是关于CComplex类的一个定义,试改正程序中出现的5处错误。class CComplexdouble real = 0.0, image;CComplex(double a=0.0, double b=0.0) real = a; image = b;static double getReal() const return real;int getImage() const return image;CComplex & operator + (const CComplex &r_x, const CComplex &r_y);r_y) CComplex
8、 temp; temp.real = r_x.real + r_y.image; return temp;CComplex a(3.0, 4.0), b(5.0), c; c = a + b; c.getReal(); c.getImage();错误为:第5行,real=0.0,改为real, 第8行,static double getReal()改为double getReal() 第9行,int getImage()改为double getImage() 第10行,CComplex & operator+改为friend CComplex operator+ 第12行,CComplex &
9、 operator+改为CComplex operator+protected: static int count;CBook() count+; cout “Book constructor called ” count virtual void order() = 0; “Read book” virtual CBook() cout “Book destructor called ” count-;static int getCount() return count;int CBook:count = 0;class CTextBook : public CBookCTextBook()
10、 cout “Textbook constructor called ”void order() cout “Order textbook” “Read textbook” CTextBook() cout “Textbook destructor called ”CTextBook oop;CBook *p_book;oop.order();oop.read();cout CTextBook:getCount() order();delete p_book;1当程序运行到处时,写出程序运行的输出结果 (3分)2 当程序结束时,程序会在第1问的基础上增加哪些输出。 (5分)3 若在主函数中定义
11、一个对象CBook book;(2分)1. 定义一个课程类CCourse,其中包含课程号(long no)、课程学分(float credit)两个数据成员,以及相应的构造函数和打印数据成员的成员函数print()。(8分)2. 为CCourse类增加一个课程总数(int total_course)数据成员,并增加一个成员函数getTotalCourse()获取total_course的值,编写一个友元函数getCourseNo()获取课程号no。做如上修改后,重新实现CCourse类(与第1问相同的不用再重复,注意说明数据成员和成员函数的存储类型,以便能够用类名来调用getTotalCour
12、se()。3. 为CCourse类定义小于运算符()运算符重载函数。CCourse类对象大小的比较是根据其课程学分(credit)的值的大小来实现的。(与第2问相同的不用再重复)(5分)4. 以CCourse类为基类,派生出面向对象程序设计课程类COOP,并在该类中增加一个表示开课单位的指针数据成员(char *p_openby)和根据学生学号判断能否选课的成员函数bool select(const char *p_xh)(只有学号前4位为2006的学生可选面向对象程序设计课程)。写出COOP类的完整定义(包括构造、拷贝构造、赋值运算符重载、析构和select()成员函数的实现)。(17分)面向对象程序设计试卷 A 卷 第 4 页 共 4 页
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2