小弟刚学习C++,有些题求救!

sevenmaster 2006-12-30 09:45:09
《面向对象方法与C++》作业
一、填空题
1.若char *string=”test”;则如果要输出指针值,正确的语句是 。
2.在重载“[ ]”运算符时,必须使用 函数重载。
3.当用public继承时,基类的public成员成为派生类的 成员,基类的protected成员成为派生类的 成员。
4.可以赋给指针的唯一整数是 。
5.在重载“=”运算符时,必须使用 函数重载。
6.以下程序: int c=10;
cout.flags(ios::hex|ios::showbase);
cout<<c;
的输出结果是 。
7.当用private继承时,基类的public成员和protected成员都将成为派生类的 成员。
8.在main 函数中出现的以下语句:max= Max(a, Max(b, c));是一个 函数调用的语句。
9. 头文件声明和定义了重要的文件处理操作服务。
10.关键字 表示类的静态成员。
11.当一个成员函数定义在类的声明外部时,函数名之前要加上 名称和 运算符。
12.在C++中,虚基类的引入是为了实现 继承。

二、判断题
1.在以下语句中: cin >> XXXX;
XXXX必须是一个变量名,而不能是一个任意表达式。( )
2.以下声明将不会导致编译错误。( )
enum GradeType {'A', 'B', 'C', 'D', 'E', 'F'};
3.在C++里,派生类的构造函数先于其基类的构造函数执行。( )
4.C++中的所有函数都是传值调用。( )
5.在类time中声明如下的函数原型:void ~time( ); ( )
6. 以下程序:
class CA
{ public:
virtual void dis( )=0; };
class CB:public CA
{ public:
void set(int m){ k=m;}
protected:
int k;
};
void main( )
{ CB b; }
是正确的。( )
7.函数模板能够定义一个在不同数据类型基础上完成同一任务的函数。( )
8.以下语句:char *string=”test”;
delete [ ]string; 是正确的 。( )
9.一个有指针数据成员的类必须要包含的成员函数有:初始化构造函数,析构函数,拷贝构造函数和赋值运算符函数 。( )
10.抽象基类中的所有虚函数都要声明为纯虚函数。 ( )
11.以下语句: char *string=new char[10];
strcpy(string,”test”);//…
delete [ ]string; 是正确的 。 ( )
12.枚举类型变量的值可以直接进行输入、输出。 ( )

...全文
167 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
endline 2006-12-30
  • 打赏
  • 举报
回复

sevenmaster 2006-12-30
  • 打赏
  • 举报
回复
3.有一家医院的门诊记录如下所示,已知该记录存放在一个”patient.dat”文件中,请编写一个程序,帮助医生计算一下每一位病人平均血压。
病人的编号 病人血压的测量次数 病人血压各次测量的结果
1001 5 100 120 90 110 100
1002 2 100 120
1003 3 90 120 130
1004 4 80 70 90 100
1005 3 120 135 110
┆ ┆ ┆
4.请完成以下集合类的设计,集合类对象的创建及使用。
#include <iostream.h>
const int maxcard=20;
class set{
int elems[maxcard],card;
public:
set( ) {card=____1____;}
friend bool operator&(int,set);
friend bool operator==(set,set);
friend ___2___ operator!=(set,set);
friend set operator+(set,set);
friend set operator+(set,int);
friend set operator-(set,int);
___3___ set operator*(set,set);
friend bool operator<(set,set);
friend bool operator<=(set,set);
friend ostream& operator<<(ostream&out,const set&s)
{ out<<endl;
for(int i=0;i<s.card;i++)
out<<s.elems[i]<<" ";
return ____4____; } };
bool operator&(int e ,set s){
for(int i=0;i<s.card;i++)
if(s.elems[i]==e)
return true;
return ____5_____;}
bool operator==(set s1,set s2){
if(s1.card!=s2.card) return false;
for(int i=0;i<s1.card;____6_____)
if(!(s1.elems[i]&s2)) return false;
return true;}
bool operator!=(set s1,set s2)
{ return !(s1==s2)?true:false;}
set operator+(set s,int e)
{ set res=s;
if(s.card<maxcard)
if(!(e&s))res.elems[res.card++]=e;
return res;}
set operator+(set s1,set s2)
{ set res=s1;
for(int i=0;i<s2.card;i++)
res=res+s2.elems[i];
return res;}
set operator-(set s,int e)
{ set res=s;
if(!(e&s))return res;
for(int i=0;i<s.card;i++)
if(s.elems[i]==e)
for(;i<s.card-1;i++) res.elems[i]=res.elems[i+1];
_____7______;
return res;}
set operator*(set s1,set s2){
_____8______;
for(int i=0;i<s1.card;i++)
for(int j=0;j<s2.card;j++)
if(s1.elems[i]==s2.elems[j])
{ res.elems[res.card++]=____9_____; break;}
return res;}
bool operator<=(set s1,set s2){
if(s1.card>s2.card)return false;
for(int i=0;i<s1.card;i++)
if(!(s1.elems[i]&s2))return false;
return true;}
bool operator<(set s1,set s2){
return s1.card<s2.card&&s1<=s2?true:false;}
void main( ){ set ______10_______;
for(int i=0;i<100;i++) s1=s1+i;
cout<<s1;
if(s1!=s2) cout<<"\nTrue\n"; else cout<<"\nFalse\n";
s2=s1; cout<<s2;
if(s1==s2) cout<<"\nTrue\n"; else cout<<"\nFalse\n";
for(i=0;i<20;i++) s1=s1-i;
cout<<s1;
s3=s1*s2; cout<<s3;
if(s1<s2) cout<<"\nTrue\n"; else cout<<"\nFalse\n";
if(s1+s2<=s3)cout<<"\nTrue\n"; else cout<<"\nFalse\n"; }
5.完成以下程序。
#include <iostream.h>
#include <iomanip.h>
double function(double x){
return 4/(1+x*x);}
class inte_algo {
protected:
double a,b,n,h,sum;
public:
inte_algo(double left,double right,double steps){
a=_____1_____,b=right,n=steps,h=(b-a)/n,sum=0;}
virtual void integrate( )= ___2___; };
class rectangle: ______3______{
public:
rectangle(double left,double right,double steps):
inte_algo(left,right,steps){ }
____4_____; };
void rectangle::integrate( ) {
double a1=a;
for(int i=0;i<n;i++)
sum+=function(a1),a1+=h;
sum*=h;
cout<<"Sum="<<sum<<endl; }
class ladder: public inte_algo {
public:
ladder(double left,double right,double steps):
inte_algo(left,right,steps){ }
void integrate( ); };
void ladder::integrate( ) {
double a1=a;
sum=(function(a)+function(b))/2;
for(int i=1;i<n;i++)
a1+=h,sum+=function(a1);
sum*=h;
cout<<"Sum="<<sum<<endl; }
class ______5_____: public inte_algo {
public:
simpson(double left,double right,double steps):
______6______ { }
void integrate( ); };
void simpson::integrate( ) {
sum=function(a)+function(b);
double s=1,a1=a;
for(int i=1;i<n;i++)
a1+=h, sum+=(3+s)*function(a1),s=-s;
sum*=h/=3;
____7____; }
void main( ) {
rectangle r(0,1,10); ladder l(0,1,10);
simpson ____8____(0,1,10); inte_algo ____9____ =&r;
cout<<setprecision(15);
p->integrate( ); p=&l;
p->integrate( ); p=&s;
_____10_____; }

sevenmaster 2006-12-30
  • 打赏
  • 举报
回复
四、编程题
1.请完成以下程序:设计一个学生类,其属性有学号、姓名、性别、年龄和C++成绩,并定义相应操作,重载析取符( >> )和插入符( << )以支持I/O操作。在main函数中定义学生对象数组,对于学生对象数组从磁盘文件“student.txt” 进行输入,最后以学号n为参数在数组中查找学号为n的学生,并显示该生的全部信息。
#include <_____1_____>
#include <string.h>
#include <iomanip.h>
class student{
_____2_____;
char name[20],sex;
int age;
float score;
public:
student( ){ }
student(int nu,char * na,char se,int ag, _____3_____ sc){
num=nu; strcpy(name,na);
sex=se; age=ag; score=sc;}
int getn( ){
return num;}
friend istream&operator>>(istream& s,student& st){
s>>st.num>>st.name>>st.sex>>st.age>>st.score;
return _____4_____; }
friend ostream&operator<<( _____5_____ s,const student& st){
s<<st.num<<" "<<_____6_____ <<" "<<st.sex<<" "<<st.age<<
" "<<st.score<<endl;
return s;} };
const int m=300;
_____7_____ main( ){
student s[m];
ifstream f("____8_____");
int i=0;
while(!f.eof( ))
f>>s[__9__];
int n,flag=1;
cout<<"Enter n:";
_____10_____;
for(int j=0;j<i && flag;j++)
if(s[j].getn( )==n){ flag=0;cout<<"找到了:"<<s[j];}
if(flag==1) cout<<"没找到。\n";
}
2.请完成以下复数类的设计,复数类对象的创建及使用。
#include <____1____>
#include <math.h>
#include <stdlib.h>
class complex{
double rpart,ipart;
double abs( )const;
double norm( )const;
public:
complex(double r=0,double i=0){
_____2_____; ipart=i;}
complex operator-( );
friend complex operator+(const complex&,const complex&);
friend complex operator-(const complex&,const complex&);
friend complex operator*(const complex&,const complex&);
friend complex operator/(const complex&,const complex&);
friend ___3___ operator>(const complex&,const complex&);
friend int operator>=(const complex&,const complex&);
friend int operator< (const complex&,const complex&);
friend int operator<=(const complex&,const complex&);
friend int operator==(const complex&,const complex&);
friend int operator!=(const complex&,const complex&);
friend istream& operator>>(istream& si,complex& c){
si>>c.rpart>>c.ipart;
return si;}
friend ostream& operator<<( ostream&so,const complex&c){
so<<'('<<c.rpart<<','<<c.ipart<<")\n";
_____4_____;} };
double complex::abs( )const{
return sqrt(rpart*rpart +_____5_____);}
double complex::norm( )const{
return (rpart*rpart+ipart*ipart);}
complex complex:: operator-( ){
return complex(-rpart,-ipart);}
complex operator+(const complex&c1,const complex&c2){
return complex(c1.rpart+c2.rpart),c1.ipart+c2.ipart);}
complex operator-(const complex&c1,const complex&c2){
return complex(_____6_____ ,c1.ipart-c2.ipart);}
complex operator*(const complex&c1,const complex&c2){
return complex(c1.rpart*c2.rpart-c1.ipart*c2.ipart,
c1.rpart*c2.ipart+c1.ipart*c2.rpart); }
complex operator/(const complex&c1,const complex&c2){
complex res;
double d=c2.norm( );
if(d!=0){
res.rpart=(c1.rpart*c2.rpart+c1.ipart*c2.ipart)/d;
res.ipart=(c1.ipart*c2.rpart-c1.rpart*c2.ipart)/d; }
else { cerr<<"dive by 0.\n";exit(1);}
___7___; }
int operator>(const complex&c1,const complex&c2){
return c1.abs( )>c2.abs( );}
int operator>=(const complex&c1,const complex&c2){
return _____8_____; }
int operator<(const complex&c1,const complex&c2){
return c1.abs( )<c2.abs( ); }
int operator<=(const complex&c1,const complex&c2){
return c1.abs( )<=c2.abs( );}
int operator==(const complex&c1,const complex&c2){
return c1.rpart==c2.rpart&&c1.ipart==c2.ipart;}
int operator!=(const complex&c1,const complex&c2){
return _____ 9______|| c1.ipart!=c2.ipart;}
void main( ){
___10___ c1(1,2),c2(3,4),c3;
cin>>c3; cout<<c3;
c3=-c1*2+c2/c3*c1-6;
cout<<c3;
if(c1>c2) cout<<c1;
else cout<<c2;
cin>>c1>>c2>>c3;
cout<<c1<<c2<<c3; }
sevenmaster 2006-12-30
  • 打赏
  • 举报
回复
三、读程题
1.下面程序在执行过程中,若顺序输入十个整数: 1 2 3 4 5 6 7 8 9 10,其结果是什么。
#include <iostream.h>
class stack;
class node{
int data;
node *prev;
public:
node(int d,node *n){ data=d; prev=n;}
friend class stack;
};
class stack{
node *top;
public:
stack( ){ top=0;}
void push(int i){
node *n=new node(i,top); top=n; }
int pop( ){
node *t=top;
if(top){
top=top->prev; int c=t->data;
delete t; return c;}
return 0; }
};
void main( ){
int c; stack s;
for(int i=0;i<10;i++)
{ cin>>c; s.push(c); }
for(i=0;i<10;i++)
cout<< s.pop( )<<" ";
cout<<endl; }



2.
#include <iostream.h>
#include <iomanip.h>
class Increment {
public:
Increment(int=0,int=1);
void addIncrement( )
{ count+=increment;}
void print( ) const;
private:
int count;
const int increment; };
Increment::Increment(int c,int i):increment(i)
{ count=c; }
void Increment::print( ) const{
cout<<"count="<<setw(8)<<count<<endl;
cout<<"increment="<<increment<<endl; }
void main( ){
Increment object(65,7);
cout<<"before:"<<endl;;
object.print( );
for(int j=0;j<3;j++){
object.addIncrement( );
cout<<"after:"<<j+1<<endl;
object.print( ); } }
3.
#include <iostream.h>
class point{
static int count ;
float xcoord , ycoord ;
public:
point(float x=0 ,float y=0 )
{ xcoord=x ;ycoord=y ;count++ ;}
static int getcount( )
{ return count ;}
~point( )
{ count-- ; } };
int point::count=0;
void main( ){
cout<<point::getcount( )<<" ";
point *p ,a(32.98,-4.71) ,b ,c;
cout<<point::getcount( )<<" ";
cout<<b.getcount( )<<" ";
p=new point[100];
cout<<point::getcount( )<<" ";
delete [ ]p;
cout<<point::getcount( )<<endl; }
4.
#include <iostream.h>
class Test{
static int count;
public:
Test( ){ ++count; }
~Test( ){ --count; }
static int getCount( ){ return count; } };
int Test::count=0;
void main( ){
cout<<Test::getCount( );
Test t,tab[5],*p;
cout<<'\t'<<Test::getCount( );
p=new Test[10];
cout<<'\t'<<Test::getCount( );
delete [ ]p;
cout<<'\t'<<Test::getCount( )<<endl; }
5.
#include <iostream.h>
#include <stdlib.h>
template<class T> class vector{
T *v; int sz;
public:
vector(int s=100){
v=new T[sz=s]; }
vector(const vector&vv){
sz=vv.sz; v=new T[sz];}
vector&operator=(const vector&vv){
if(&vv!=this){
delete[ ]v;
sz=vv.sz;
v=new T[sz];
for(int i=0;i<sz;i++)
v[i]=vv.v[i]; }
return *this;}
~vector( ){ delete[ ]v;}
T & operator[ ](int i){
if(i<0||i>=sz){ cerr<<"Error\n";exit(1);}
return v[i];} };
void main( ){
vector<int> v(10),u(v);
for(int i=0;i<10;i++)
v[i]=3*i+1;
u=v;
for( i=0;i<=10;i++)
cout<<u[i]<<" "; }

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧