【渝粤题库】陕西师范大学200711 面向对象方法与C++ 作业
《面向對象方法與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.枚舉類型變量的值可以直接進行輸入、輸出。 ( )
三、讀程題
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?; }
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 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 v(10),u(v);
for(int i=0;i<10;i++)
v[i]=3i+1;
u=v;
for( i=0;i<=10;i++)
cout<<u[i]<<” "; }
四、編程題
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(flag1) 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(rpartrpart +5);}
double complex::norm( )const{
return (rpartrpart+ipartipart);}
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.rpartc2.rpart-c1.ipartc2.ipart,
c1.rpartc2.ipart+c1.ipartc2.rpart); }
complex operator/(const complex&c1,const complex&c2){
complex res;
double d=c2.norm( );
if(d!=0){
res.rpart=(c1.rpartc2.rpart+c1.ipartc2.ipart)/d;
res.ipart=(c1.ipartc2.rpart-c1.rpartc2.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.rpartc2.rpart&&c1.ipartc2.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=-c12+c2/c3c1-6;
cout<<c3;
if(c1>c2) cout<<c1;
else cout<<c2;
cin>>c1>>c2>>c3;
cout<<c1<<c2<<c3; }
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(s1s2) cout<<”\nTrue\n"; else cout<<"\nFalse\n";
for(i=0;i<20;i++) s1=s1-i;
cout<<s1;
s3=s1s2; 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+xx);}
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; }
總結
以上是生活随笔為你收集整理的【渝粤题库】陕西师范大学200711 面向对象方法与C++ 作业的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 笔记本内存条在电脑哪个位置
- 下一篇: pat乙级 1007 素数对猜想(C++