面向对象 课后答案 第6章
- 格式:doc
- 大小:95.00 KB
- 文档页数:12
第六章课后习题答案(第二版谭浩强)1://xt6-1/cpp#include <iostream> //如用VC++应改为∶#include <iosttram.h>using namespace std; //如用VC++应取消此行#include "cylinder.h"#include "point.cpp"#include "circle.cpp"#include "cylinder.cpp"int main(){Cylinder cy1(3.5,6.4,5.2,10);cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<", y="<<cy1.getY()<<", r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<", volume="<<cy1.volume()<<endl;cy1.setHeight(15);cy1.setRadius(7.5);cy1.setPoint(5,5);cout<<"\nnew cylinder:\n"<<cy1;Point &pRef=cy1;cout<<"\npRef as a point:"<<pRef;Circle &cRef=cy1;cout<<"\ncRef as a Circle:"<<cRef;return 0;}3:解法一#include <iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}~Point(){cout<<"executing Point destructor"<<endl;}private:float x;float y;};class Circle:public Point{public:Circle(float a,float b,float r):Point(a,b),radius(r){} ~Circle(){cout<<"executing Circle destructor"<<endl;} private:float radius;};int main(){Point *p=new Circle(2.5,1.8,4.5);delete p;return 0;}3:解法二#include <iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}~Point(){cout<<"executing Point destructor"<<endl;} private:float x;float y;};class Circle:public Point{public:Circle(int a,int b,int r):Point(a,b),radius(r){}~Circle(){cout<<"executing Circle destructor"<<endl;} private:float radius;};int main(){Point *p=new Circle(2.5,1.8,4.5);Circle *pt=new Circle(2.5,1.8,4.5);delete pt;return 0;}3:解法三#include <iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}virtual ~Point(){cout<<"executing Point destructor"<<endl;}private:float x;float y;};class Circle:public Point{public:Circle(float a,float b,float r):Point(a,b),radius(r){}virtual ~Circle(){cout<<"executing Circle destructor"<<endl;}private:float radius;};void main(){Point *p=new Circle(2.5,1.8,4.5);delete p;}4:#include <iostream>using namespace std;//定义抽象基类Shapeclass Shape{public:virtual double area() const =0; //纯虚函数};//定义Circle类class Circle:public Shape{public:Circle(double r):radius(r){} //结构函数virtual double area() const {return 3.14159*radius*radius;};//定义虚函数protected:double radius; //半径};//定义Rectangle类class Rectangle:public Shape{public:Rectangle(double w,double h):width(w),height(h){} //结构函数virtual double area() const {return width*height;} //定义虚函数protected:double width,height; //宽与高};class Triangle:public Shape{public:Triangle(double w,double h):width(w),height(h){} //结构函数virtual double area() const {return 0.5*width*height;}//定义虚函数protected:double width,height; //宽与高};//输出面积的函数void printArea(const Shape &s){cout<<s.area()<<endl;}//输出s的面积int main(){Circle circle(12.6); //建立Circle类对象circlecout<<"area of circle =";printArea(circle);//输出circle的面积Rectangle rectangle(4.5,8.4); //建立Rectangle类对象rectanglecout<<"area of rectangle =";printArea(rectangle);//输出rectangle的面积Triangle triangle(4.5,8.4); //建立Triangle类对象cout<<"area of triangle =";printArea(triangle); //输出triangle的面积return 0;}5:#include <iostream>using namespace std;//定义抽象基类Shapeclass Shape{public:virtual double area() const =0; //纯虚函数};//定义Circle(圆形)类class Circle:public Shape{public:Circle(double r):radius(r){}//结构函数virtual double area() const {return 3.14159*radius*radius;};//定义虚函数protected:double radius; //半径};//定义Square(正方形)类class Square:public Shape{public:Square(double s):side(s){} //结构函数virtual double area() const {return side*side;} //定义虚函数protected:double side;};//定义Rectangle(矩形)类class Rectangle:public Shape{public:Rectangle(double w,double h):width(w),height(h){} //结构函数virtual double area() const {return width*height;} //定义虚函数protected:double width,height; //宽与高};//定义Trapezoid(梯形)类class Trapezoid:public Shape{public:Trapezoid(double t,double b,doubleh):top(t),bottom(t),height(h){} //结构函数virtual double area() const {return0.5*(top+bottom)*height;} //定义虚函数protected:double top,bottom,height; //上底、下底与高};//定义Triangle(三角形)类class Triangle:public Shape{public:Triangle(double w,double h):width(w),height(h){} //结构函数virtual double area()const {return 0.5*width*height;}//定义虚函数protected:double width,height; //宽与高};int main(){Circle circle(12.6); //建立Circle类对象circleSquare square(3.5); //建立Square类对象squareRectangle rectangle(4.5,8.4); //建立Rectangle类对象rectangleTrapezoid trapezoid(2.0,4.5,3.2); //建立Trapezoid类对象trapezoidTriangle triangle(4.5,8.4); //建立Triangle类对象Shape*pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};//定义基类指针数组pt,使它每一个元素指向一个派生类对象double areas=0.0; //areas为总面积for(int i=0;i<5;i++){areas=areas+pt[i]->area();}cout<<"totol of all areas="<<areas<<endl; //输出总面积return 0;}(学习的目的是增长知识,提高能力,相信一分耕耘一分收获,努力就一定可以获得应有的回报)。
第六章习题答案一、选择填空1、A2、C3、D4、B5、D6、A7、C8、A9、D 10、A 11、C 12、A13、B 14、C 15、C 16、D 17、B 18、C 19、A 20、D21、C 22、B二、判断下列描述的正确性,对者划√,错者划×。
1、√2、×3、×4、×5、√6、√7、×8、√9、× 10、√11、√ 12、√ 13、√ 14、√ 15、× 16、√ 17、√ 18、√ 19、√ 20、×21、× 22、×三、分析下列程序的输出结果。
1、运行该程序输出结果如下所示。
Default constructor calledConstructor calleda=0,b=0a=4,b=82、运行该程序输出结果如下所示。
a=7,b=93、运行该程序输出结果如下所示。
1044、运行该程序输出结果如下所示。
1035,789.5045、运行该程序输出结果如下所示。
1{}{0,1,2,3,4,5,6,7,8}1{11,12,13,14,15,16,17,18,19}{19,18,17,16,15,14,13,12,11}6、运行该程序输出结果如下所示。
Starting1:Default constructor called.Default constructor called.Default constructor called.Eding1:Starting2:Constructor: a=5,b=6Constructor: a=7,b=8Constructor: a=9,b=10Ending2:Destructor called.a=9,b=10Destructor called.a=7,b=8Destructor called.a=5,b=6Destructor called.a=5,b=6Destructor called.a=3,b=4Destructor called.a=1,b=27、运行该程序输出结果如下所示。
第二章2-4#include <iostream>using namespace std;Add(int a,int b);int main(){int x,y,sum;cout<<"please input x and y:";cin>>x>>y;sum = add(x,y);cout <<x<<"+"<<y<<"="<<sum<<endl;}Add(int a,int b){return a+b;}2-5(1)this is a C++ program.(2)x= y=10 z=Ax= y=10 z=Ax= y=2 z=Ax= y=2 z=E(3)x y z500 1000 0500 1500 1500500 200 15002-6#include <iostream>using namespace std;int main(){int *p,*init;int countp=0;int countn=0;p = new int[20];init = p;for(int i=0;i<20;i++){cin>>*p;p++;}p = p-20;for( i=0;i<20;i++){if(*p>0) countp++;if(*p<0) countn++;cout<<*p<<" ";p++;}cout<<"正数有:"<<countp<<endl; cout<<"负数有:"<<countn<<endl; p = init;delete[] p;return 0;}2-7不做要求#include <iostream>rint ();cout<<"最小成绩:"<<endl;st[min].Print ();return 0;}4-11#include <iostream>#include <string>using namespace std;class Book{char *name;char*author;int sale;public:Book(){ name = '\0';author = '\0';sale = -1;}Book(char* a ,char* b,int c){name = new char[strlen(a)+1];strcpy(name,a);author = new char[strlen(b)+1];strcpy(author,b);sale = c;}void print(){cout<<"autor "<<author<<endl;cout<<"name "<<name<<endl;cout<<"price "<<sale<<endl;}~Book(){if(!name ) delete[] name;if(!author)delete[] author;}};int main(){Book b1("c++","li ai hua",12);Book b2;return 0;}第五章5-8改错题答案不唯一(1) class DC {int x;public:DC(){x =100;}};(2)编译无错,但逻辑错误,可改为:class BC{protected:int x;public:BC(int i=0){x = i}};class DC:private BC{public:DC(int i):BC(i){}};(3)将DC构造函数改为:DC(int i):BC(i){y = 0;}5-9(1) base class(2) (10,5)(3,9-18,33)(13,19)(13,19-18,33)(13,19)5-10#include <iostream>using namespace std;class Shape{int x,y;public:Shape(int ix,int iy){x = ix; y = iy;}virtual void show(){cout<<"pos: "<<x<<' '<<y<<endl;}};class Circle :public Shape{int radius;public:Circle(int ix,int iy,int r):Shape(ix,iy),radius(r){}void show() {Shape::show ();cout<<"circle: "<<radius<<endl;}};class Rect :public Shape{int width,higth;public:Rect(int ix,int iy,int iw,int ih):Shape(ix,iy),width(iw),higth(ih){} void show() {Shape::show ();cout<<"width and higth: "<<width<<' '<<higth<<endl;} };int main(){Shape s1(1,1);Rect r1(2,2,8,8);Circle c1(3,3,9);();();return 0;}5-11#include<>class vehicle show ();tru1. show ();}第六章6-4d=3D::fun();6-5C::print(),cinfo=2C::print(),cinfo=2D::print(),dinfo=4B类不能定义对象,否则编译通不过,因为B未定义基类A中的虚函数print(),它也是个虚基类。
第二章2-4#include <iostream>using namespace std;Add(int a,int b);int main(){int x,y,sum;cout<<"please input x and y:"; cin>>x>>y;sum = add(x,y);cout <<x<<"+"<<y<<"="<<sum<<endl; }Add(int a,int b){return a+b;}2-5(1)this is a C++ program.(2)x=50.6 y=10 z=Ax=216.34 y=10 z=Ax=216.34 y=2 z=Ax=216.34 y=2 z=E (3)x y z500 1000 0500 1500 1500500 200 15002-6#include <iostream> using namespace std; int main(){int *p,*init;int countp=0;int countn=0;p = new int[20];init = p;for(int i=0;i<20;i++){cin>>*p;p++;}p = p-20;for( i=0;i<20;i++){if(*p>0) countp++;if(*p<0) countn++;cout<<*p<<" ";p++;}cout<<"正数有:"<<countp<<endl; cout<<"负数有:"<<countn<<endl; p = init;delete[] p;return 0;}2-7不做要求#include <iostream>//#include <string>using namespace std;void checkagescore(string name,int age) {if (name == "exit") throw name;if(age<0||age>50)throw age;}int main(){string name;int age;for(int i=0 ;i<5 ;i++ ){cin.ignore ();getline(cin,name );cin>>age ;try{checkagescore(name,age);}catch( string){cout<<"exception :name is exit"<<endl;continue;}catch(int){cout<<"exception :age is not proper"<<endl;continue;}cout<<"name:"<<name<<"age :"<<age<<endl;}return 0;}第三章3-1(1)A (2)C (3)B (4)C (5)C (6)B (7)B (8)C (9)C3-7(1)main()函数中p1.age = 30;语句是错误的。
软件工程专业《面向对象技术》课程的课后作业参考答案(第6部分)1.1.1第五周课程(10月18日)1、作业题目设计一个实现对下面的EBook数据库表进行CRUD-----“增(CREATE)、删(DELETE)、改(UPDATE)、查(READER)”操作的J2SE程序。
物理数据库系统不限制,可以为MS SQLServer、MYSQL和Oracle等。
(2)作业基本要求提交源代码,并将执行的结果以“拷屏”的方式放入到Word 文档中。
(3)作业提交时间本周的作业在(10月25日)上课时交给授课教师,具体的要求同前面的作业要求。
(4)相关的功能实现的程序代码示例/** DesktopApplication.java* 程序编写说明:* 在完成老师布置的对EBook数据库表进行CRUD-----“增(CREATE)、删(DELETE)、改(UPDATE)、查(READER)的基础上尝试了java的GUI编程,理解了事件的监听机制的原理、桌面程序的布局等知识。
* 编程工具:NetBeans IDE 6.0 Preview (M9, build 070502)。
* 数据库:mysql-5.0.16-win32*/package desktopapplication;import javax.swing.*;import javax.swing.table.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.sql.*;class Table extends JFrame implements ActionListener{/*-------------------------------菜单栏的各项元素定义-----------------------*/ /*下拉菜单*/private JMenuBar menubar;private JMenu menu;private JMenuItem iselect,iinsert,idel,iupdate;/*插入时的输入框*/private JTextField Tebook_id;private JTextField Tebook_name;private JTextField Tebook_kind;private JTextField Tebook_price;/*显示标签*/private JLabel label_title;private JLabel label_id;private JLabel label_name;private JLabel label_kind;private JLabel label_price;private JLabel label_updateid; //显示更新的记录集号的标签//出错后的提示标签,提示出错信息为没有输入数字的标签private JLabel exception_interger=new JLabel("请输入数字!");private JPanel panel; //面板//private JTextArea textarea;//文本区域private Container contrainer;//容器private JButton insert_enter; // 插入确定按钮private JButton del_enter; // 删除确定按钮private JButton update_select_enter; // 更新选择按钮private JButton update_detail_enter; // 更新确定按钮public Table(){ //初始化表格/*-------------------------------初始化界面----------------------*/ menubar= new JMenuBar();menu = new JMenu("对数据表的处理");iselect = new JMenuItem("显示数据表");iinsert = new JMenuItem("插入数据");idel = new JMenuItem("删除数据");iupdate = new JMenuItem("更新数据");//添加监视器iselect.addActionListener(this);iinsert.addActionListener(this);idel.addActionListener(this);iupdate.addActionListener(this);menu.add(iselect);menu.add(iinsert);menu.add(idel);menu.add(iupdate);menubar.add(menu);setJMenuBar(menubar);contrainer=this.getContentPane();this.setTitle("对下面的EBook数据库表进行CRUD-");this.setSize(400,400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);contrainer.validate();}/*显示表格的函数(JFrame)*/public void Show_Table(Vector row, Vector tableHead) {/*表格行向量*/ /*表格头名称*/contrainer.removeAll();/*-------------------------------初始化界面----------------------*/ menubar= new JMenuBar();menu = new JMenu("对数据表的处理");iselect = new JMenuItem("显示数据表");iinsert = new JMenuItem("插入数据");idel = new JMenuItem("删除数据");iupdate = new JMenuItem("更新数据");//添加监视器iselect.addActionListener(this);iinsert.addActionListener(this);idel.addActionListener(this);iupdate.addActionListener(this);menu.add(iselect);menu.add(iinsert);menu.add(idel);menu.add(iupdate);menubar.add(menu);this.setJMenuBar(menubar);/*------------------------------声明表格模型----------------------*/ DefaultTableModel tableModel = new DefaultTableModel();/* setDataVectorpublic void setDataVector(Vector dataVector, Vector columnIdentifiers) 参数:dataVector - 新的数据向量,columnIdentifiers - 列的名称*/ tableModel.setDataVector(row, tableHead);/*表格使用模型*/JTable table = new JTable(tableModel);table.setRowHeight(20);JScrollPane scrollPane = new JScrollPane(table);contrainer=this.getContentPane();contrainer.add(scrollPane);contrainer.add(table);this.setTitle("对下面的EBook数据库表进行CRUD-");this.setSize(400,400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);contrainer.validate();}/*插入新数据到表格的函数(JFrame)*/public void Insert_Table(){contrainer.removeAll();label_title = new JLabel("请输入书信息");label_id= new JLabel("自动编号");label_name= new JLabel("书名");label_kind= new JLabel("类别");label_price= new JLabel("价格");Tebook_name=new JTextField();Tebook_kind=new JTextField();Tebook_price=new JTextField();insert_enter=new JButton("提交");panel=new JPanel();panel.setLayout(null);label_title.setBounds(0, 60, 90, 60);label_id.setBounds(30, 100, 60, 25);label_name.setBounds(95, 100, 60, 25);label_kind.setBounds(160, 100, 60, 25);label_price.setBounds(230, 100, 60, 25);Tebook_name.setBounds(95, 120, 60, 25);Tebook_kind.setBounds(160, 120, 60, 25);Tebook_price.setBounds(230, 120, 60, 25);insert_enter.setBounds(305, 120, 70, 25);//对确定按钮添加侦听器ActioListenerinsert_enter.addActionListener(this);//各个元素添加到面板上panel.add(label_title);panel.add(label_id);panel.add(label_name);panel.add(label_kind);panel.add(label_price);panel.add(Tebook_name);panel.add(Tebook_kind);panel.add(Tebook_price);panel.add(insert_enter);this.setSize(400,400);contrainer=this.getContentPane();contrainer.add(panel);contrainer.validate();}/*删除数据函数(JFrame)*/public void Del_Table() {contrainer.removeAll();label_title = new JLabel("请输入要删除的书的ID号");Tebook_id = new JTextField("请输入要删除的书的ID号");del_enter = new JButton("删除");panel=new JPanel();panel.setLayout(null);label_title.setBounds(0, 60, 190, 60);Tebook_id.setBounds(30, 100, 100, 20);del_enter.setBounds(200, 120, 70, 25);//添加删除按钮的监听:del_enter.addActionListener(this);panel.add(label_title);panel.add(Tebook_id);panel.add(del_enter);this.setSize(400,400);contrainer=this.getContentPane();contrainer.add(panel);contrainer.validate();}/*选择要更新的记录号的函数(JFrame)*/public void Update_Table_Select() {contrainer.removeAll();label_title = new JLabel("请输入要更新的书的ID号");Tebook_id = new JTextField("请输入要更新的书的ID号");update_select_enter = new JButton("选择该书更新");panel=new JPanel();panel.setLayout(null);label_title.setBounds(0, 60, 190, 60);Tebook_id.setBounds(30, 100, 100, 20);update_select_enter.setBounds(200, 120, 160, 25);//添加删除按钮的监听:update_select_enter.addActionListener(this);panel.add(label_title);panel.add(Tebook_id);panel.add(update_select_enter);this.setSize(400,400);contrainer=this.getContentPane();contrainer.add(panel);contrainer.validate();}/*更新该记录的函数(JFrame)*/public void Update_Table_Detail(int ebook_id) { //获取记录集contrainer.removeAll();label_title = new JLabel("请更新该书信息");label_id= new JLabel("编号");label_name= new JLabel("书名");label_kind= new JLabel("类别");label_price= new JLabel("价格");//将文本框中内容,初始化为要修改的记录内容try{Class.forName( "com.mysql.jdbc.Driver" );Connection con =DriverManager.getConnection( "jdbc:mysql://localhost:3306/MySql" ,"root" , "wby666" ); //注册Statement prepstmt =con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);ResultSet rs = prepstmt.executeQuery("select * from EBook");rs.absolute(ebook_id);String id = String.valueOf (ebook_id) ;//不可修改该记录的编号,这是要由系统作为主键的自动编号label_updateid=new JLabel(id);// Tebook_id.setEditable(true);Tebook_name=new JTextField(rs.getString("ebook_name"));Tebook_kind=new JTextField(rs.getString("ebook_kind"));String ebook_pricef = Float.toString(rs.getFloat("ebook_price"));Tebook_price=new JTextField(ebook_pricef);rs.close();con.close();}catch (SQLException e) {System.out.println("SQLState:" + e.getSQLState());System.out.println("Message:" + e.getMessage());System.out.println("Vendor:" + e.getErrorCode()); }catch (ClassNotFoundException e) {System.out.println(e);}update_detail_enter=new JButton("更新");panel=new JPanel();panel.setLayout(null);label_title.setBounds(0, 60, 110, 60);label_id.setBounds(30, 100, 60, 25);label_name.setBounds(95, 100, 60, 25);label_kind.setBounds(160, 100, 60, 25);label_price.setBounds(230, 100, 60, 25);label_updateid.setBounds(30, 120, 60, 25);Tebook_name.setBounds(95, 120, 60, 25);Tebook_kind.setBounds(160, 120, 60, 25);Tebook_price.setBounds(230, 120, 60, 25);update_detail_enter.setBounds(305, 120, 70, 25);//对更新确定按钮添加侦听器ActioListenerupdate_detail_enter.addActionListener(this);//各个元素添加到面板上panel.add(label_title);panel.add(label_id);panel.add(label_name);panel.add(label_kind);panel.add(label_price);panel.add(label_updateid);panel.add(Tebook_name);panel.add(Tebook_kind);panel.add(Tebook_price);panel.add(update_detail_enter);this.setSize(400,400);contrainer=this.getContentPane();contrainer.add(panel);contrainer.validate();}public void PreparedStatementExecSQLSelect(){ //数据库查询函数Vector tableHead; //表格头Vector cell; //表格中一个元组Vector row; //表格中一行tableHead = new Vector();row = new Vector();//--------------------------建立表头----------------------------------//String[] tableHeadName = {"ebook_id","ebook_name","ebook_kind", "ebook_price"};for (int i = 0; i < tableHeadName.length; i++) {tableHead.add(tableHeadName[i]);}//--------------------------建立数据库----------------------------------//try {Class.forName( "com.mysql.jdbc.Driver" );Connection con =DriverManager.getConnection( "jdbc:mysql://localhost:3306/MySql" ,"root" , "wby666" ); //注册Statement prepstmt =con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);//出现下面这一种问题java.sql.SQLException: Before start of result set的原因是:// ResultSet 始终有一个游标指向其当前数据行。
1.概念填空题1.1 数组定义时有3个要素:数组名、数组元素的类型和数组元素的个数。
按元素在数组中的位置进行访问,是通过下标进行的,称为直接或索引访问。
1.2 C++中的多维数组是嵌套定义的,即多维数组的基本定义是数组构成的数组,三维数组的元素是二维数组。
1.3 计算机内存是一维编址的,多维数组在内存中的存储必须转换为一维方式,C++多维数组在内存中的排列是行方式,即越低的下标变化快。
设数组a有m行n列,每个元素占内存u个字节,则a[i][j]的首地址为a[0][0]的地址+ (i*n+j)*u。
1.4数组名是整型常量,数组名赋给同类型的指针后,该指针是指向数组首元素的指针。
数组名在表达式中被转换为指向数组首元素的指针常量。
1.5每个对象都有一个指向自身的指针,称为this指针,通过使用它来确定其自身的地址。
该指针只能读,不能写。
1.6在C++中,对象访问成员所用的运算符是.,通过指针访问对象的成员所用的运算符是->。
1.7 当动态分配失败时,系统采用返回NULL来表示发生了异常。
如果new返回的指针丢失,则所分配的自由存储区空间将无法收回,称为内存泄漏。
这部分空间必须在计算机重启才能找回,这是因为无名对象的生命期为整个程序。
1.8默认复制构造函数只能完成对象成员的赋值,可能会造成重复释放,默认的析构函数可能会产生内存泄漏。
“=”运算也会产生对象重复释放。
因此我们需要自定义复制构造函数完成对象的深复制。
1.9 在用new运算符建立一个三维数组int 4*5*6,使用了3 个下标运算符,用delete运算符释放该数组时使用了1 个下标运算符。
new返回的指针类型是int (*) [5]6]。
2 简答题2.1 用一维数组名作函数参数和用一维数组元素函数参数的含义是否相同?为什么2.2 指针变量与整型量的加减运算代表什么意义?2.3个指向普通变量的指针进行减运算是否有意义?为什么2.4什么是浅拷贝?什么是深拷贝?二者有何异同?何时必须自定义复制构造函数、析构函数,何时必须自定义=运算符中在函数, 自定义的复制构造函数、析构函数应该怎样设计? 2.5从右往左解释int *(*(*pf)[5])(double *)中4个*的含义。
2.6 为什么动态建立类对象数组时,类的定义中一定要有默认的构造函数?3.选择题3.1以下对一维数组a的正确定义是(C )。
A.int n=5, a[n];B.int a (5);C.const int N=5;int a[N];D.int n; cin>>n; int a[n];3.2己知int a[10]=={0,1,2,3,4,5,6,7,8,9},*p=a;则不能表示数组a中元素的选项是( C )。
A.*aB.*pC.aD.a[ p-a]3.3 己知int a[]={0,2,4,6,8,10},*p=a+1;其值等于0的表达式是(D )。
A.*(p++)B.*(++p)C.*(p--)D.*(--p)3.4已知char *a[]=( "fortran",” basic", "pascal", "Java",”c++”;则语句cout<<a[3];的显示结果是( C )。
A.tB.一个地址值C.javaD.javac++3.5 下列关于this指针的叙述中,正确的是(D)。
A.任何与类相关的函数都有this指针B.类的成员函数都有this指针C.类的友元函数都有this指针D.类的非静态成员函数才有this指针3.6对于类型相同的两个指针变量之间,不能进行的运算是(C )。
A.< B.= C.+ D.-3.7若有语句int a=4,*point=&a;下面均代表地址的一组选项是(D)。
A.a,point,*&a B.&*a,&a,*pointC.*&point,*point,&a D.&a,&*point,point3.8 已有定义int k=2;int *ptrl,*ptr2;且ptr1和ptr2均已指向变量k,下面不能正确执行的赋值语句是(B )。
A.k=*ptrl+*ptrl2; B.ptr2=k;B.ptr1=ptr2 D.k=*ptr1*(*ptr2);3.9设有语句int array[3][4];,则在下面几种引用下标为i和j的数组元素的方法中,不正确的引用方式是(D )。
A.array[i][j] B.*(*(array+i)+j)C.*(array[i]+j) D.*(array+i*4+j)3.10 函数原型为fun(int (*p)[3],int),调用形式为fun(a,2),则a的定义应该为(C )。
A.int **a B.int (*a)[ ]C.int a[ ][3] D.int a[3]3.11 已知p是一个指向类Sample数据成员m的指针,S是类Sample中的一个对象。
如果要给m赋值为5,正确的是( C )。
A.S.P=5; B.S->P=5; C.S.*P=5; D.*S.P=5;3.12下面程序段的运行结果是(D )。
char *p=”abcdefgh”;p+=3;cout<<strlen(strcpy(p,”ABCD”));A.8 B.12 C.4 D.出错3.13 当定义const char *p=”ABC”;时,下列语句正确的是(D )A.char *q=p; B.p[0]=’B’; C.*p=’\0’; D.p=NULL;3.14 s0是一个string类串,定义串sl错误的是(A)。
A.string s1(3,”A”); B.string s1(s0,0,3);C.string s1(“ABC”,0,3); D.string s1=”ABC”;4.写出程序运行结果4.1#include<iostream>using namespace std;class Location{int X,Y;public:Location(int initX,int initY){init(initX,initY);}void init (int initX,int initY){X=initX,Y=initY;}int GetX(){return X;}return Y;}};void display(Location& rL){cout<<rL.GetX( )<<" "<<rL.GetY( )<<"\n";}int main(){Location A[5]={Location(0,0),Location(1,1),Location(2,2),Location(3,3),Location(4,4)};Location *rA=A;A[3].init(5,3);rA->init(7,8);for(int i=0;i<5;i++)display(*(rA++));}7 81 12 25 34 44.2#include <iostream>using namespace std;int main(){char w[ ][10]={“ABCD”,”EFGH”,”IJKL”,”MNOP”},k;for(k=1;k<3;k++) cout<<w[k]<<endl;}EFGHIJKL5. 编程题5.1已知求成绩的平均值和均方差公式:ave=,dev=其中n为学生人数,为第i个学生成绩。
求某班学生的平均成绩和均方差。
#include <iostream.h>#include <stdlib.h>#include <time.h>#include <math.h>//using namespace std;double ave(int *,int);double dev(int *,double,int);void create(int *,int);const int N=10;void main(){int a[N],i;double s,d;srand(time(0));for(i=0;i<N;i++)cout<<a[i]<<", ";s=ave(a,N);cout<<"ave= "<<s;d=dev(a,s,N);cout<<" dev= "<<d<<endl;}double ave(int *a,int N){double s=0;int i;for(i=0;i<N;i++)s+=a[i];return s/N;}double dev(int *a,double s,int N){int i;double d=0;for(i=0;i<N;i++)d+=(a[i]-s)*(a[i]-s);return sqrt(d/N);}void create(int *c,int N){int i;for(i=0;i<N;i++)c[i]=rand()%100;}5.2用随机函数产生10个互不相同的两位整数,存放至一维数组中,并输出其中的素数。
#include <iostream.h>#include <stdlib.h>#include <time.h>#include <math.h>//using namespace std;void create(int *,int);void prime(int *,int);const int N=10;void main(){int a[N],i;srand(time(0));create(a,N);for(i=0;i<N;i++)cout<<a[i]<<" ";cout<<endl;}void prime(int *p,int N){int i,j,k;for(i=0;i<N;i++){if(p[i]<2)continue;k=sqrt(p[i]);for(j=2;j<=k;j++)if(p[i]%j==0)break;if(j>k)cout<<p[i]<<" ";}cout<<endl;}void create(int *c,int N){int i,d;int f[100]={0};for(i=0;i<N;i++){do{d=rand()%100;}while(f[d]);c[i]=d;f[d]=1;}}5.3设计一个函数,将一个整型数组按升序排序。