面向对象程序的设计C课后题答案

第一章:面向对象程序设计概述[1_1]什么是面向对象程序设计?面向对象程序设计是一种新型的程序设计范型。

这种范型的主要特征是:程序=对象+消息。

面向对象程序的基本元素是对象,面向对象程序的主要结构特点是:第一:程序一般由类的定义和类的使用两部分组成,在主程序中定义各对象并规定它们之间传递消息的规律。

第二:程序中的一切操作都是通过向对象发送消息来实现的,对象接受到消息后,启动有关方法完成相应的操作。

面向对象程序设计方法模拟人类习惯的解题方法,代表了计算机程序设计新颖的思维方式。

这种方法的提出是软件开发方法的一场革命,是目前解决软件开发面临困难的最有希望、最有前途的方法之一。

[1_2]什么是类?什么是对象?对象与类的关系是什么?在面向对象程序设计中,对象是描述其属性的数据以及对这些数据施加的一组操作封装在一起构成的统一体。

对象可以认为是:数据+操作在面向对象程序设计中,类就是具有相同的数据和相同的操作的一组对象的集合,也就是说,类是对具有相同数据结构和相同操作的一类对象的描述。

类和对象之间的关系是抽象和具体的关系。

类是多个对象进行综合抽象的结果,一个对象是类的一个实例。

在面向对象程序设计中,总是先声明类,再由类生成对象。

类是建立对象的“摸板”,按照这个摸板所建立的一个个具体的对象,就是类的实际例子,通常称为实例。

[1_3]现实世界中的对象有哪些特征?请举例说明。

对象是现实世界中的一个实体,其具有以下一些特征:(1)每一个对象必须有一个名字以区别于其他对象。

(2)需要用属性来描述它的某些特性。

(3)有一组操作,每一个操作决定了对象的一种行为。

(4)对象的操作可以分为两类:一类是自身所承受的操作,一类是施加于其他对象的操作。

例如:雇员刘名是一个对象对象名:刘名对象的属性:年龄:36 生日:1966.10.1 工资:2000 部门:人事部对象的操作:吃饭开车[1_4]什么是消息?消息具有什么性质?在面向对象程序设计中,一个对象向另一个对象发出的请求被称为“消息”。

当对象接收到发向它的消息时,就调用有关的方法,执行相应的操作。

消息是一个对象要求另一个对象执行某个操作的规格的说明,通过消息传递才能完成对象之间的相互请求或相互协作。

消息具有以下3个性质:(1)同一个对象可以接收不同形式的多个消息,做出不同的响应。

(2)相同形式的消息可以传递给不同的对象,所做出的响应可以是不同的。

(3)消息的发送可以不考虑具体的接收者,对象可以响应消息,也可以不响应。

[1_5]什么是方法?消息和方法的关系是什么?在面向对象程序设计中,要求某一对象作某一操作时,就向该对象发送一个响应的消息,当对象接收到发向它的消息时,就调用有关的方法,执行响应的操作。

方法就是对象所能执行的操作。

方法包括界面和方法体两部分。

方法的界面也就是消息的模式,它给出了方法的调用协议;方法体则是实现某种操作的一系列计算步骤,也就是一段程序。

在C++语言中方法是通过函数来实现的,称为成员函数。

消息和方法的关系是:对象根据接收到的消息,调用相应的方法;反过来,有了方法,对象才能响应相应的消息。

[1_6]什么是封装和抽象?请举例说明。

在现实世界中,所谓封装就是把某个事物包围起来,使外界不知道该事物的具体内容。

在面向对象程序设计中,封装是指把数据和实现操作的代码集中起来放在对象内部,并尽可能隐蔽对象的内部细节。

对象好象是一个不透明的黑盒子,表示对象属性的数据和实现各个操作的代码都被封装在黑盒子里,从外面是看不见的,更不能从外面直接访问或修改这些数据及代码。

使用一个对象的时候,只需要知道它向外界提供的接口形式而无需知道它的数据结构细节和实现操作的算法。

封装机制可以将对象的使用者与设计者分开,使用者不必知道对象行为实现的细节,只需要使用设计者提供的接口让对象去做。

抽象是人类认识问题的最基本的手段之一。

它忽略了一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。

抽象是对复杂世界的简单表示,抽象强调感兴趣的信息,忽略了不重要的信息。

例如,设计一个学籍管理程序的过程中,考察某个学生对象时,只关心他的姓名、学好、成绩等,而对他的身高、体重等信息就可以忽略。

以一般观点而言,抽象是通过特定的实例(对象)抽象共同性质以后形成概念的过程。

抽象是对系统的简化描述或规范说明,它强调了系统中的一部分细节和特性,而忽略了其他部分。

抽象包括两个方面:数据抽象和代码抽象(或称为行为抽象)。

前者描述某类对象的属性或状况,也就是此类对象区别于彼类对象的特征物理量;后者描述了某类对象的共同行为特征或具有的共同操作。

在面向对象程序设计方法中,对一个具体问题的抽象分析的结果,是通过类来描述和实现的。

现在以学生管理程序为例,通过对学生进行归纳、分析,抽取出其中的共性,可以得到如下的抽象描述:共同的属性:姓名、学号、成绩等,他们组成了学生数据抽象部分。

用C++语言的数据成员来表示,可以是:char *name; int number; float score;共同的行为:数据录入、数据修改和数据输出等,这构成了学生的行为抽象部分,用C++语言的成员函数表示,可以是:input();modify();print(); 如果我们开发一个学生健康档案程序,所关心的特征就有所不同了。

可见,即使对同一个研究对象,由于所研究问题的侧重点不同,就可能产生不同的抽象结果。

[1_7]什么是继承?请举例说明。

继承所表达的是对象类之间的相关关系,这种关系使得某类对象可以继承另一类对象的特征和能力。

现实生活中,继承是很普遍和容易理解的。

例如我们继承了父母的一些特征,如种族、血型、眼睛的颜色等,父母是我们所具有的属性的基础。

继承所表达的是对象之间相关的关系。

这种关系使得某一类可以继承另一个类的特征和能力。

[1_8]若类之间具有继承关系,则它们之间具有什么特征?(1)类间具有共享特征(包括数据和操作代码的共享)(2)类间具有差别或新增部分(包括非共享的数据和代码操作)(3)类间具有层次结构假设有两个类A和B,若类B继承类A,则类B包含了类A的特征(包括数据和操作),同时也可以加入自己所特有的新特性。

这时,我们称被继承类A为基类或父类或超类;而称继承类B为A类的派生类或子类。

同时,我们还可以说,类B是从类A中派生出来的。

[1_9]什么是单继承、多继承?请举例说明。

从继承源上分,继承分为单继承和多继承。

单继承是指每个派生类只直接继承了一个基类的特征。

多继承是指多个基类派生出一个派生类的继承关系。

多继承的派生类直接继承了不止一个基类的特征。

例如:小孩的玩具车继承了车的一些特性,还继承了玩具的一些特征。

[1_10]什么是多态性?举例说明。

多态性也是面向对象程序设计的重要特性。

它是指不同的对象收到相同的消息时产生不同的行为方式。

例如我们同样双击windows系统桌面上的图标时,有的是打开多媒体播放器,有的是打开资源管理器。

利用多态性,用户只需发送一般形式的消息,而将所有的实现留给接收消息的对象。

对象根据所收到的消息做出相应的动作。

[1_11]什么是函数重载和运算符重载?为什么要使用重载?重载一般包括函数重载和运算符重载。

函数重载是指一个表示符可同时用于为多个函数命名,而运算符重载是指一个运算符可同时用于多种运算。

也就是说,相同名字的函数或运算符在不同的场合可以表现出不同的行为。

使用重载的目的是为了更好地表达行为共享,这种行为共享就象将相似的操作划分在一起。

使用重载可以使程序员在只知道操作的一般含义,而不知道操作的具体细节的情况下能正确地对某个对象使用一个操作。

另外,使用重载的直接益处是减少了程序员记忆操作的名字的负担。

第二章::C++基础[2_1]简述C++的主要特点(1)C++保持与C的兼容,用C编写的软件可以用到C++中。

(2)用C++编写的程序可读性好,代码结构更合理,可直接地在程序中映射问题空间的结构。

(3)生成代码的质量高。

(4)软件的可重用性、可扩充性、可维护性和可靠性有了明显的提高,从而节省了开发费用和时间。

(5)支持面向对象的机制,可方便地构造出模拟现实问题的实体和操作。

[2_2]下面是一个C程序,改写它,使它采用C++风格的i/o语句改写如下:#include <iostream.h>main(){ int a,b,d,min;cout<<”enter two numbers: “;cin>>a;cin>>b;min=a>b?b:a;for(d=2;d<min;d++)if((a%b)==0)&&((b%d)==0)) break;if(d==min){ cout<<”no common denominators\n”;return 0;}cout<<”the lowest common denominator is “<<endl<<d;return 0;}[2_3]测试下面的注释是否有效?此注释有效,单行注释中可以嵌套/*……….*/方式的注释。

[2_4]以下简单的C++程序不可能编译通过,为什么?原因是:在程序中,当一个函数的定义在后,而对它的调用在前时,必须将该函数的原型写在调用语句之前,而在本程序中缺少函数原型语句。

在语句:#incl ude <iostream.h>后加上语句sum(int a,int b);就可以通过了。

[2_5](1)答:这两个函数原形是等价的,因为函数原型中的参数名可以缺省。

(2)答:这两个函数的第一行是不等价的,函数的第一行中必须包含参数名。

(3)答:这两个函数原型是等价的,因为在函数原型中未注明参数,C++认为该函数的参数表为空(void)[2_6]答:输出结果为:10 20 因为f函数的参数是引用,所以修改k的值有效。

函数调用后,主函数中k的值变为10。

由于m是对函数的引用,当m被赋值为20时,k的值也变为20。

[2_7] 举例说明可以使用const替代#define以消除#define的不安全性答:例如:#include <iostream.h>#define A 2+4#define B A*3void main(){ cout<<B<<endl; }上面程序的运行结果是14而不是18,但很容易被认为是18。

用const替代#de fine就能得到正确结果,从而消除了#define的不安全性。

#include <iostream.h>const A=2+4;const B=A*3;void main(){ cout<<B<<endl; }运行结果为18。

合集下载

C面向对象程序设计课后习题答案1~14章

C面向对象程序设计课后习题答案1~14章
c1=getchar(); //将输入的第一个字符赋给c1
c2=getchar(); //将输入的第二个字符赋给c2
cout<<"用putchar函数输出结果为:";
putchar(c1);
putchar(44);
putchar(c2);
cout<<endl;
cout<<"用cout语句输出结果为:";
putchar(c2);
cout<<endl;
cout<<"用cout语句输出结果为:";
cout<<c1<<c2<<endl;
return 0;
}
3-4-1、#include <iostream>
using namespace std;
int main ( )
{char c1,c2;
cout<<"请输入两个字符c1,c2:";
cout<<"please enter score of student:";
cin>>score;
while (score>100||score<0)
{cout<<"data error,enter data again.";
cin>>score;
}
switch(int(score/10))
{case 10:
else m=y;
if (z<m) m=z;
return(m);
}
1-8、#include <iostream>

C面向对象程序设计课后习题答案第版谭浩强

C面向对象程序设计课后习题答案第版谭浩强

C面向对象程序设计课后习题答案第版谭浩强文件排版存档编号:[UYTR-OUPT28-KBNTL98-UYNN208]第六章课后习题答案(第二版谭浩强)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 {return 0.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;}。

C++面向对象程序设计课后答案

C++面向对象程序设计课后答案

Char k:4; Unsigned short i:8; Unsigned long m; }; Main(0 { struct A a; a.t=’b’; printf(“%x”,a.t); } 答:输出是 2
习题3
3-1 New delete与malloc free的联系与区别是什么? 答;在程序开发中的内存的动态分配与管理是一个让开发者头痛的问 题,在C中,一般通过malloc和free来进行内存分配和回收,而在 C++中,new和delete已经完全包含malloc和free的功能,并且更为强大。 它们的区别是malloc和free是库函数而不是运算符,不在编译器控制权 限之内,而new和delete是运算符。New出来的指针式直接带类型信息, 而malloc返回是void指针。 3-2 描述内存分配以及它们的区别。 答:内存分配的“堆”和“栈”。主要区别是:(1)管理方式和碎片问题: 对于栈来说,由编译器自动管理;对于堆来说,释放工作由程序员控 制,容易产生内存碎片。(2)分配效率:栈的效率要比堆的高得多; (3):增长方式不同:栈内存是从高地址到低地址增长,而堆是相反 的;(4)空间大小的不同:一般来讲,在32位系统下,内存可以达到 4GB空间,但对于栈来讲,一般都是有一定的空间大小的。 3-3 请定义一个变量,初始化为34759,并以八进制与十六进制输出。 如果将该整数定义成无符号短整数,当以有有符号数输出时,结果是什 么? 答;程序如下: #include<iostream> using namespace std; void main() { int i=34759;
习题1
-1 面向对象程序设计与面向过程程序设计相比有什么优点? 答:优点有:(1)写程序不再是从计算机的角度考虑问题,而是站在 人类思维的角度;(2)程序的可拓展性比其他不是面向对象的好; (3)能最大限度波爱护已有程序的代码。 -2 简述C++语言程序的特点。 答:C++语言是在C语言的基础上发展而来,但它比C语言更容易学习和 掌握,它与C的最大区别是把函数放进了结构中,并引入了新的关键词 类。C++完美的体现了面向对象的各种特性。 1-3 C++与C语言的关系如何?它们的本质区别是什么? 答:C++语言是基于C语言的基础上发展起来的。它们的关系十分密切, 很多语句之间是通用的。它们的本质区别是C语言是面向过程的编程语 言,而C++是面向对象的编程语言。 -3 组件编程有什么优点? 答:组件编程的优点是它把对象技术应用于系统设计,对面向对象的程 序设计的实现过程做了进一步抽象。它强调真正的软件可重用性和高度 的互操作性。

C#面向对象程序设计课后答案

C#面向对象程序设计课后答案

习题参考答案Chapter 1 1_1 对象 客观世界中的事物都是对象,包括有形的物理对象,可感知的逻辑实体,以及概念化的抽象实体。

它有自己的属性,能够执行特定的操作。

概念化的抽象实体。

它有自己的属性,能够执行特定的操作。

类 具有相同属性和操作的一组对象的集合;它描述的不是单个对象,而是“一类”对象的共同特征。

其重要性在于它是面向对象技术中最重要的结构,它支持信息隐藏和封装,进而支持对抽象数据类型(ADT )的实现。

)的实现。

1_2 略 1_3 参考图如下参考图如下Scene+Name +Type +Price+IntroductionLine +Name +Days+Nights +ScenesPackage+Line +Type +Grade +Number+Price +AgentTour+Package +StartTime +EndTime +Guide+State+Customers0..*10..*1Customer+Name +IdCard +Gender +Age +Enroll()+Cancel()Staff +Name +IdCard +Gender +Age +Degree +Apply()Guide +CerId +CerDue Agent +Accept()+Reject()Manager +Hire()+Fire()Member +Username +Password +CreditProvinceCityTravelAgency +Name+Manager+Representative +Address +PhoneDirector +Approve()0..*10..*1图1 1_4 UML 与面向对象 UML 是一种定义良好、易于表达、功能强大且普遍适用的建模语言。

它溶入了软件工程领域的新思想、新方法和新技术。

它的作用域不限于支持面向对象的分析与设计,还支持从需求分析开始的软件开发的全过程。

c面向对象程序设计课后习题答案

c面向对象程序设计课后习题答案

c面向对象程序设计课后习题答案C面向对象程序设计课后习题答案在学习C面向对象程序设计课程的过程中,课后习题是巩固知识、提升能力的重要途径。

通过认真完成习题,我们可以更好地理解课程内容,掌握编程技巧,提高解决问题的能力。

下面我们将针对一些常见的C面向对象程序设计课后习题进行答案解析。

1. 请编写一个C++程序,实现一个简单的学生信息管理系统,包括学生姓名、学号、成绩等信息的录入、查询和修改功能。

答案解析:首先,我们需要定义一个学生类,包括姓名、学号、成绩等属性,并实现相应的方法来实现信息的录入、查询和修改功能。

然后在主函数中创建学生对象,调用相应的方法即可实现学生信息管理系统。

2. 编写一个C++程序,实现一个简单的图书管理系统,包括图书名称、作者、出版社等信息的录入、查询和删除功能。

答案解析:同样地,我们需要定义一个图书类,包括图书名称、作者、出版社等属性,并实现相应的方法来实现信息的录入、查询和删除功能。

在主函数中创建图书对象,调用相应的方法即可实现图书管理系统。

3. 设计一个C++程序,模拟实现一个简单的银行账户管理系统,包括账户余额、存款、取款等功能。

答案解析:在这个问题中,我们需要定义一个银行账户类,包括账户余额、存款、取款等属性,并实现相应的方法来实现账户管理系统。

在主函数中创建账户对象,调用相应的方法即可实现银行账户管理系统。

通过以上习题的解答,我们可以看到C面向对象程序设计的重要性和实际应用。

通过不断地练习和实践,我们可以更好地掌握面向对象程序设计的知识和技能,提高编程能力,为今后的工作和学习打下坚实的基础。

希望同学们能够认真对待课后习题,不断提升自己的编程水平。

面向对象程序设计C课后题答案

面向对象程序设计C课后题答案

第一章:面向对象程序设计概述[1_1]什么是面向对象程序设计?面向对象程序设计是一种新型的程序设计范型。

这种范型的主要特征是:程序=对象+消息。

面向对象程序的基本元素是对象,面向对象程序的主要结构特点是:第一:程序一般由类的定义和类的使用两部分组成,在主程序中定义各对象并规定它们之间传递消息的规律。

第二:程序中的一切操作都是通过向对象发送消息来实现的,对象接受到消息后,启动有关方法完成相应的操作。

面向对象程序设计方法模拟人类习惯的解题方法,代表了计算机程序设计新颖的思维方式。

这种方法的提出是软件开发方法的一场革命,是目前解决软件开发面临困难的最有希望、最有前途的方法之一。

[1_2]什么是类?什么是对象?对象与类的关系是什么?在面向对象程序设计中,对象是描述其属性的数据以及对这些数据施加的一组操作封装在一起构成的统一体。

对象可以认为是:数据+操作在面向对象程序设计中,类就是具有相同的数据和相同的操作的一组对象的集合,也就是说,类是对具有相同数据结构和相同操作的一类对象的描述。

类和对象之间的关系是抽象和具体的关系。

类是多个对象进行综合抽象的结果,一个对象是类的一个实例。

在面向对象程序设计中,总是先声明类,再由类生成对象。

类是建立对象的“摸板”,按照这个摸板所建立的一个个具体的对象,就是类的实际例子,通常称为实例。

[1_3]现实世界中的对象有哪些特征?请举例说明。

对象是现实世界中的一个实体,其具有以下一些特征:(1)每一个对象必须有一个名字以区别于其他对象。

(2)需要用属性来描述它的某些特性。

(3)有一组操作,每一个操作决定了对象的一种行为。

(4)对象的操作可以分为两类:一类是自身所承受的操作,一类是施加于其他对象的操作。

例如:雇员刘名是一个对象对象名:刘名对象的属性:年龄:36 生日:1966.10.1 工资:2000 部门:人事部对象的操作:吃饭开车[1_4]什么是消息?消息具有什么性质?在面向对象程序设计中,一个对象向另一个对象发出的请求被称为“消息”。

C++面向对象程序设计教程课后习题答案


a
15
2.在面向对象方法中,实现信息隐蔽是依靠 。
A)对象的继承
B)对象的多态
C)对象的封装
D)对象的分类
解析:在面向对象方法中,封装性是指将数据和算法捆绑成一个 整体,这个整体就是对象,描述对象的数据被封装在内部,只可 以通过对象提供的算法来进行操作,从而实现信息隐蔽。 答案:C
a
16
3.下列关于类和对象的叙述中,错误的是 。 A)一个类只能有一个对象 B)对象是类的具体实例 C)类是某一类对象的抽象 D)类和对象的关系就像数据类型和变量的关系
}
}
……
a
11
*5.编一个程序,用同一个函数名对n个数据进行从小到大排序,数据类型可
以是整型、单精度实型、双精度实型,用重载函数实现。
参考程序:
……
int main()
// 主函数main()
{
int a[] = {1, 3, 2, 5, 6, 9, 0, 6};
// 定义a
float b[] = {1.6, 3.3, 2.8, 5.6, 6.8, 9.6, 0.6, 6.8}; // 定义b
所以先输出i的值1,再使i加1。
答案:A
a
4
二、编程题
1.编写一个C++程序,要求输出“欢迎学习C++语言!”。
参考程序: #include <iostream> using namespace std;
// 编译预处理命令 // 使用命名空间std
int main()
// 主函数main()
{
cout << "欢迎学习C++语言!"<< endl; // 用C++的方法输出一行

c面向对象程序设计课后习题答案陈维兴

c面向对象程序设计课后习题答案陈维兴C面向对象程序设计是计算机科学中的一门重要课程,通过学习该课程,学生可以掌握面向对象的编程思想和技巧,提高自己的编程能力。

而课后习题则是巩固和应用所学知识的重要环节。

本文将为大家提供一些C面向对象程序设计课后习题的答案,希望能够帮助大家更好地理解和掌握相关知识。

1. 题目:请编写一个C程序,实现一个简单的计算器,可以进行加、减、乘、除四则运算。

答案:以下是一个简单的计算器程序的实现代码。

```c#include <stdio.h>int main() {char operator;double num1, num2, result;printf("请输入要进行的运算符:");scanf("%c", &operator);printf("请输入两个操作数:");scanf("%lf %lf", &num1, &num2);switch (operator) {case '+':result = num1 + num2;printf("运算结果为:%.2lf\n", result);break;case '-':result = num1 - num2;printf("运算结果为:%.2lf\n", result); break;case '*':result = num1 * num2;printf("运算结果为:%.2lf\n", result); break;case '/':if (num2 != 0) {result = num1 / num2;printf("运算结果为:%.2lf\n", result); } else {printf("除数不能为0!\n");}break;default:printf("输入的运算符有误!\n");break;}return 0;}```2. 题目:请编写一个C程序,实现一个简单的学生信息管理系统,可以添加、删除、查询和修改学生信息。

C 面向对象程序设计习题解答(全)


4答案 n=2,sum=2 n=3,sum=5 n=5,sum=10
5答案 x=3 6答案 x=1,y=2 x=30,y=40 7答案 1 2 3 4 exit main 3 2 1 8答案 n=100 9答案 the student is:Li Hu the teacher is:Wang Ping 10答案 2 11答案 1035,789.504 12答案 13答案
一、选择题 1 2 3 4 D B B C
类和对象的进一步解析
5 D 6 D 7 D B 8 C B 9 10 11 12 13 14 15 16 B D B A A C B A
17 18 19 20 21 22 23 24 C C D B A D 二、填空题 1 this 2所有成员 3友元类、友元函数 4 friend 5 程序编译、程序结束 三、程序阅读题
第六章 派生与继承
一、选择题 1(1) 1(2) 2 A B 3 4 5 6 7 8 9 10 11 D C C C D D B C A D
二、填空题 1 继承 2 具体化、抽象 3 公有继承、保护继承、私有继承 4 子对象 5 public(共有的)、protected(保护的)、不可访问 6 protected(保护的)、protected(保护的)、不可访问的 7 private(私有的)、private(私有的)、不可访问的 8 二义性 三、判断下列描述的正确性 1 2 3 4 5 6 7 8 9 10 11 12 13 √ × × × × × √ √ × × √ √ ×
1、 选择题 1 2 3 4 5 6 7 C 8 9 10 D D D C A D C 2、 程序阅读题 1答案 a=639,b=78,c=12 2答案 a=5,b=8 a=8,b=5 3答案 10 4答案 x=20.6 y=5 z=A x=216.34 y=5 z=A x=216.34 y=2 z=A x=216.34 y=2 z=E 5答案 ic=11 fc=7.82 ic=5 fc=2.15 3、 判断下列描述的正确性 1 2 √ × D A

c面向对象程序设计课后习题答案谭浩强版

c++面向对象程序设计课后习题答案(谭浩强版)第一章5:#include <iostream> using namespace std;int main(){cout<<"This"<<"is";cout<<"a"<<"C++";cout<<"program."<<endl; return 0;}6:#include <iostream> using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}7:#include <iostream>using namespace std;int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return 0;}int f(int x,int y,int z) {int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m); }8: #include <iostream> using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl; return 0;}9:#include <iostream>using namespace std;int main(){int add(int x,int y);int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return 0;}int add(int x,int y){int c;c=x+y;return(c);}10:#include <iostream>using namespace std;int main(){void sort(int x,int y,int z); int x,y,z;cin>>x>>y>>z;sort(x,y,z);return 0;}void sort(int x, int y, int z){int temp;if (x>y) {temp=x;x=y;y=temp;} //{ }内3个语句的作用是将x和y的值互换)if (z<x) cout<<z<<','<<x<<','<<y<<endl; else if (z<y) cout<<x<<','<<z<<','<<y<<endl;elsecout<<x<<','<<y<<','<<z<<endl;}11:#include <iostream>using namespace std;int main(){int max(int a,int b,int c=0);int a,b,c;cin>>a>>b>>c;cout<<"max(a,b,c)="<<max(a,b,c)<<endl;cout<<"max(a,b)="<<max(a,b)<<endl; return 0;}int max(int a,int b,int c){if(b>a) a=b;if(c>a) a=c;return a;}12:#include <iostream>using namespace std;int main(){void change(int ,int );int a,b;cin>>a>>b;if(a<b) change(a,b);cout<<"max="<<a<<" min="<<b<<endl; return 0;}void change(int ,int ){int r1,r2,temp;temp=r1;r1=r2;r2=temp;}13:#include <iostream>using namespace std;int main(){void sort(int &,int &,int &);int a,b,c,a1,b1,c1;cout<<"Please enter 3 integers:";cin>>a>>b>>c;a1=a;b1=b;c1=c;sort(a1,b1,c1);cout<<a<<" "<<b<<" "<<c<<" in sorted order is ";cout<<a1<<" "<<b1<<" "<<c1<<endl;return 0;}void sort(int &i,int &j,int &k) { void change(int &,int &);if (i>j) change(i,j);if (i>k) change(i,k);if (j>k) change(j,k);}void change(int &x,int &y){ int temp;temp=x;x=y;y=temp;}14:#include <iostream>#include <string>using namespace std;int main(){ string s1="week",s2="end";cout<<"s1="<<s1<<endl;cout<<"s2="<<s2<<endl;s1=s1+s2;cout<<"The new string is:"<<s1<<endl; return 0;}15:#include <iostream>#include <string>using namespace std;int main(){ string str;int i,n;char temp;cout<<"please input a string:";cin>>str;n=str.size();for(i=0;i<n/2;i++){temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;}cout<<str<<endl;return 0;}16:#include <iostream>#include <string>using namespace std;int main(){ int i;stringstr[5]={"BASIC","C","FORTRAN","C++","PASC AL"};void sort(string []);sort(str);cout<<"the sorted strings :"<<endl;for(i=0;i<5;i++)cout<<str[i]<<" ";cout<<endl;return 0;}void sort(string s[]){int i,j;string t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (s[i]>s[i+1]){t=s[i];s[i]=s[i+1];s[i+1]=t;}}17: #include <iostream>#include <string>using namespace std;int main(){long c[5]={10100,-123567, 1198783,-165654, 3456};int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 }; void sort(int []);void sort(float []);void sort(long []);sort(a);sort(b);sort(c);return 0;}void sort(int a[]){int i,j,t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(long a[]){int i,j;long t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(float a[]){int i,j;float t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}18: #include <iostream>#include <string>using namespace std;template <typename T>void sort(T a[]){int i,j,min;T t;for(i=0;i<5;i++){min=i;for (j=i+1;j<5;j++)if(a[min]>a[j]) min=j;t=a[i]; a[i]=a[min]; a[min]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}int main(){ int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 }; long c[5]={10100,-123567, 1198783,-165654, 3456};sort(a);sort(b);sort(c);return 0;}第二章1#include <iostream>using namespace std;class Time{public:void set_time();void show_time();private: //成员改为公用的int hour;int minute;int sec;};void Time::set_time() //在main函数之前定义{cin>>hour;cin>>minute;cin>>sec;}void Time::show_time() //在main函数之前定义{cout<<hour<<":"<<minute<<":"<<sec<<endl; }int main(){Time t1;t1.set_time();t1.show_time();return 0;}2:#include <iostream>using namespace std;class Time{public:void set_time(void){cin>>hour;cin>>minute;cin>>sec;}void show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl; }private: int hour;int minute;int sec;};Time t;int main(){t.set_time();t.show_time();return 0;}3:#include <iostream>using namespace std; class Time{public:void set_time(void); void show_time(void); private:int hour;int minute;int sec;};void Time::set_time(void) {cin>>hour;cin>>minute;cin>>sec;}void Time::show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl; }Time t;int main(){ t.set_time();t.show_time();return 0;}4://xt2-4-1.cpp(main.cpp)#include <iostream>using namespace std;#include "xt2-4.h"int main(){Student stud;stud.set_value();stud.display();return 0;}//xt2-4-2.cpp(即student.cpp)#include "xt2-4.h" //在此文件中进行函数的定义#include <iostream>using namespace std; //不要漏写此行void Student::display( ){ cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}void Student::set_value(){ cin>>num;cin>>name;cin>>sex;}5://xt2-5-1.cpp(file1.cpp)#include <iostream>#include "xt2-5.h"int main(){Array_max arrmax;arrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;}//xt2-5-2.cpp(arraymax.cpp) #include <iostream>using namespace std;#include "xt2-5.h"void Array_max::set_value() { int i;for (i=0;i<10;i++)cin>>array[i];}void Array_max::max_value(){int i;max=array[0];for (i=1;i<10;i++)if(array[i]>max) max=array[i]; }void Array_max::show_value(){cout<<"max="<<max<<endl;}6:解法一#include <iostream>using namespace std;class Box{public:void get_value();float volume();void display();public:float lengh;float width;float height;};void Box::get_value(){ cout<<"please input lengh, width,height:";cin>>lengh;cin>>width;cin>>height;}float Box::volume(){ return(lengh*width*height);}void Box::display(){ cout<<volume()<<endl;}int main(){Box box1,box2,box3;box1.get_value();cout<<"volmue of bax1 is ";box1.display();box2.get_value();cout<<"volmue of bax2 is "; box2.display();box3.get_value();cout<<"volmue of bax3 is "; box3.display();return 0;}解法二:#include <iostream>using namespace std;class Box{public:void get_value();void volume();void display();public:float lengh;float width;float height;float vol;};void Box::get_value(){ cout<<"please input lengh, width,height:";cin>>lengh;cin>>width;cin>>height;}void Box::volume(){ vol=lengh*width*height;}void Box::display(){ cout<<vol<<endl;}int main(){Box box1,box2,box3;box1.get_value();box1.volume();cout<<"volmue of bax1 is ";box1.display();box2.get_value();box2.volume();cout<<"volmue of bax2 is "; box2.display();box3.get_value();box3.volume();cout<<"volmue of bax3 is "; box3.display();return 0;}第三章2:#include <iostream>using namespace std;class Date{public:Date(int,int,int);Date(int,int);Date(int);Date();void display();private:int month;int day;int year;};Date::Date(int m,int d,int y):month(m),day(d),year(y){ }Date::Date(int m,int d):month(m),day(d) {year=2005;}Date::Date(int m):month(m){day=1;year=2005;}Date::Date(){month=1;day=1;year=2005;}void Date::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;}int main(){Date d1(10,13,2005);Date d2(12,30);Date d3(10);Date d4;d1.display();d2.display();d3.display();d4.display();return 0;}3:#include <iostream>using namespace std;class Date{public:Date(int=1,int=1,int=2005);void display();private:int month;int day;int year;};Date::Date(int m,int d,int y):month(m),day(d),year(y){ }void Date::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;}int main(){Date d1(10,13,2005);Date d2(12,30);Date d3(10);Date d4;d1.display();d2.display();d3.display();d4.display();return 0;}4:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void display();private:int num;float score;};void Student::display(){cout<<num<<" "<<score<<endl;}int main(){Student stud[5]={Student(101,78.5),Student(102,85.5),Stude nt(103,98.5),Student(104,100.0),Student(105,95.5)}; Student *p=stud;for(int i=0;i<=2;p=p+2,i++)p->display();return 0;}5:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}int num;float score;};void main(){Student stud[5]={Student(101,78.5),Student(102,85.5),Stude nt(103,98.5),Student(104,100.0),Student(105,95.5)}; void max(Student* );Student *p=&stud[0];max(p);}void max(Student *arr){float max_score=arr[0].score;int k=0;for(int i=1;i<5;i++)if(arr[i].score>max_score){max_score=arr[i].score;k=i;}cout<<arr[k].num<<" "<<max_score<<endl; }6:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) {num=n;score=s;}void display(){cout<<num<<" "<<score<<endl;}private:int num;float score;};int main(){Student stud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return 0;}7: 解法一#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) {num=n;score=s;}void display() {cout<<num<<" "<<score<<endl;}//可改为:void display() const {cout<<num<<" "<<score<<endl;}private:int num;float score;};int main(){const Studentstud(101,78.5);stud.display();//stud.change(101,80.5);stud.display();return 0;}解法二:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) const {num=n;score=s;}void display() const {cout<<num<<" "<<score<<endl;}private:mutable int num;mutable float score;};int main(){const Student stud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return 0;}解法三:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) {num=n;score=s;}void display() {cout<<num<<""<<score<<endl;}private:int num;float score;};int main(){Student stud(101,78.5);Student *p=&stud;p->display();p->change(101,80.5);p->display();return 0;}8:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) {num=n;score=s;}void display() {cout<<num<<" "<<score<<endl;}private:int num;float score;};int main(){Student stud(101,78.5);void fun(Student&);fun(stud);return 0;}void fun(Student &stu){stu.display();stu.change(101,80.5);stu.display();}9:#include <iostream>using namespace std;class Product{public:Product(int n,int q,float p):num(n),quantity(q),price(p){};void total();static float average();static void display();private:int num;int quantity;float price;static float discount;static float sum;static int n;};void Product::total(){float rate=1.0;if(quantity>10) rate=0.98*rate;sum=sum+quantity*price*rate*(1-discount); n=n+quantity;}void Product::display(){cout<<sum<<endl;cout<<average()<<endl;}float Product::average(){return(sum/n);}float Product::discount=0.05;float Product::sum=0;int Product::n=0;int main(){Product Prod[3]={Product(101,5,23.5),Product(102,12,24.56) ,Product(103,100,21.5)};for(int i=0;i<3;i++)Prod[i].total();Product::display();return 0;}10:#include <iostream>using namespace std;class Date;class Time{public:Time(int,int,int);friend void display(const Date &,const Time &);private:int hour;int minute;int sec;};Time::Time(int h,int m,int s){hour=h;minute=m;sec=s;}class Date{public:Date(int,int,int);friend void display(const Date &,const Time &);private:int month;int day;int year;};Date::Date(int m,int d,int y){month=m;day=d;year=y;}void display(const Date &d,const Time &t) {cout<<d.month<<"/"<<d.day<<"/"<<d.year<<e ndl;cout<<t.hour<<":"<<t.minute<<":"<<t.sec<< endl;}int main(){Time t1(10,13,56);Date d1(12,25,2004);display(d1,t1);return 0;}11:#include <iostream>using namespace std;class Time;class Date{public:Date(int,int,int);friend Time;private:int month;int day;int year;};Date::Date(int m,int d,int y):month(m),day(d),year(y){ }class Time{public:Time(int,int,int);void display(const Date &);private:int hour;int minute;int sec;};Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){ }void Time::display(const Date &d){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<e ndl;cout<<hour<<":"<<minute<<":"<<sec<<endl; }int main(){Time t1(10,13,56);Date d1(12,25,2004);t1.display(d1);return 0;}12:#include <iostream>using namespace std;template<class numtype>class Compare{public:Compare(numtype a,numtype b); numtype max();numtype min();private:numtype x,y;};template <class numtype> Compare<numtype>::Compare(numtype a,numtype b){x=a;y=b;}template <class numtype>numtype Compare<numtype>::max(){return (x>y)?x:y;}template <class numtype>numtype Compare<numtype>::min(){return (x<y)?x:y;}int main(){Compare<int> cmp1(3,7);cout<<cmp1.max()<<" is the Maximum of two integer numbers."<<endl;cout<<cmp1.min()<<" is the Minimum of two integer numbers."<<endl<<endl;Compare<float> cmp2(45.78,93.6);cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl;Compare<char> cmp3('a','A');cout<<cmp3.max()<<" is the Maximum of two characters."<<endl;cout<<cmp3.min()<<" is the Minimum of two characters."<<endl;return 0;}第四章1:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}double get_real();double get_imag();void display();private:double real;double imag;};double Complex::get_real(){return real;}double Complex::get_imag(){return imag;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator + (Complex &c1,Complex &c2){returnComplex(c1.get_real()+c2.get_real(),c1.ge t_imag()+c2.get_imag());}int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c3=";c3.display();return 0;。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档