c面向对象程序设计教程习题答案
【篇一:c++程序设计教程--面向对象分册(郑秋生)完整
答案】
、 选择题
1.c 2.b 3.c 4.a 5.c 6.a 7.c 8 c 9a 10 c 二、阅读题 1.x=2,y=3 2.x=2,y=3 x!=y 3.
cstatic::va1=0 cs1.vai=1 cs2.val=2 cs1.val=4 cs2.vai=4 四、改错题
#include string.h
#include iostream.h class person {
public:
person(int n,char* nam,char s) {
num=n;
strcpy(name,nam); sex=s;
coutconstructor called.endl; }
~person( ) {
coutdestructor called.endl; }
void display( ) {
coutnum: numendl; coutname: nameendl;
coutsex: sexendlendl; }
private: int num;
char name[10]; char sex; };
int main( ) {
person s1(10010,wang_li,f); s1.display( );
person s2(10011,zhang_fun,m); s2.display( ); return 0; }
五、编程题 5.1
#include iostream
using namespace std; class cbox {
public :
cbox(double l=0,double w=0,double h=0); double area();
double volume (); private :
double lengh; double width; double high; };
cbox::cbox(double l,double w,double h) {
lengh=l; width=w; high=h; }
double cbox::area() {
return 2*(lengh*width+lengh*high+width*high); }
double cbox::volume () {
return lengh*width*high; }
void main() {
cbox box1(4,5,6);
coutbox1.area()endl;
coutbox1.volume()endl; } 5.2
#include iostream
using namespace std; class cpoint {
public :
cpoint(double a=0,double b=0) {
x=a; y=b; }
cpoint(cpoint p) {
x=p.x; y=p.y; }
void print() {
cout(x,y); }
private : double x,y; };
class cline {
public:
cline(double x1=0,double y1=0,double x2=0,double y2=0):p1(x1,y1),p2(x2,y2) { }
cline(cpoint x,cpoint y):p1(x),p2(y)