类和对象

第161题

请在下面的横线处填上适当内容,以使类的定义完整。

class Array
{
Int* ptr;
Int size;
public:
Array(){size=0; ptr=NULL;}
Array(int n){size=n;ptr=new int[size];}
Array(______(1)_______)//复制初始化构造函数
{
size=a.size;
ptr=new int[size];
for(int i=0;i<size;i++)
________(2)________;//将源对象的动态数组内容复制到目标对象
}
};
第162题

程序阅读题

1、

#include<iostream.h>
class Test
{
private:
int num;
public:
Test(int n=0){num=n;num++}
~Test(){cout<<″Destructor is active,number=″<<num<<endl;}
};
void main()
{
Test x[2];
cout<<″Exiting main″<<endl;
}

2、

#include <iostream.h>
class A
{
public:
virtual void fun (int data){cout<<”class A:”<<data<<endl;}
void fun(char *str){ cout<<”class A:”<<str<<endl; }
};
class B: public A
{
public:
void fun() {cout<<”class B”<<endl;}
void fun(int data) { cout<<”class B:”<<data<<endl;}
void fun(char *str){ cout<<”class B:”<<str<<endl;}
};
void main()
{
A *pA;
B b;
pA=&b;
pA->fun(1);
pA->fun(“Hello”);
}

3、

#include <iostream.h>
void main()
{
cout.fill('*');
cout.width(10);
cout<<"123.45"<<endl;
cout.width(8);
cout<<"123.45"<<endl;
cout.width(4);
cout<<"123.45"<<endl;
}

4、从键盘输入10,给出程序的输出结果。

#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
main()
{
vector<int> v;
int i,j,n;
cin>>n;
for(i=1;i<=n;i++)
{
if(i%2!=0)
v.push_back(x);
}
vector<int>::iterator p;
for(p=v.begin();p!=v.end();p++)
cout<<*p<<endl;
}

5、

#include<iostream>
using namespace std;
class Sample
{
private:
int i;
static int count;
public:
Sample();
void display();
};
Sample::Sample()
{
i=0;
count++;
}
void Sample::display()
{
cout<<"i="<<i++<<",count="<<count<<endl;
}
int Sample::count=0;
void main()
{
Sample a,b;
a.display();
b.display();
}
第163题

按下列要求编程,实现类的定义,并在主函数中测试这个类。

定义一个描述日期的类Date,包括的数据成员有年(year)、月(month)和日(day),并实现如下功能函数;

(1)日期对象初始化;

(2)设置日期;

(3)以year/month/day形式输出日期;

(4)判断闰年。


第164题

根据下列Vector类定义,编程完成Vector类的具体实现:

class Vector
{
friend ostream &operator<<(ostream &out, const Vector &v);
private:
int *data;
int size;
public:
Vector();
Vector(int a[],int n);
Vector(const Vector &s);
~Vector();
Vector &operator=(const Vector &v);
int &operator[](int index);
};

第165题

请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:

The square’s area is 4

#include<iostream.h>
class Rectangle
{
    double W,H;
 public:
     Rectangle(double w=0,h=0){W=w;H=h;}
     void show(){cout<<W*H;}
 };
 class Square:public Rectangle
 {
 public:
     Square(double r):__________(1)_________{};
     void show()
     {
         cout<<”The square’s area is “;
         _______________(2)___________;
      }
  };
  void main()
  {
      Square s(2);
      s.show();
   }
第166题

下面程序中A是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

B1 called

B2 called

#include<iostream.h>
class A
{
public:
______________(1)____________;
};
class B1:public A
{
public:
void display()
{cout<”B1 called”<<endl};
};
class B2:public A
{
public:
void display()
{cout<<”B2 called”<<endl};
};
void show(_________(2)________)
{
p->display();
}
void main()
{
B1 b1;
B2 b2;
A* p[2]={&b1,&b2};
for(int i=0;i<2;i++)
show(p[i]);
}
第167题

请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为8。

#include<iostream.h>
template<class T>
void fun(___________(1)___________)
{
t=x[0];
for(int i=0;i<n;i++)
if(a[i]>t)
t=a[i];
}
void main()
{
int a[]={3,5,8,4,6};
int m;
fun(a,5,__(2)____);
cout<<m<<endl;
}
第168题

下面程序通过把类Distance声明为类Point的友元类来实现计算两点之间的距离。请在下面程序的横线处填上适当字句,以使程序完整。

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
double X,Y;
public:
Point(double x,double y)
{X=x;Y=y;}
____________(1)____________;
};
class Distance
{
public:
double Dis(Point& p1,Point& p2);
{
double t;
____________________(2)___________________;
return t;
}
}
void main()
{
Point p(10,10),q(20,20);
Distance d;
cout<<d.Dis(p,q)<<endl;
}
第169题

程序阅读题

1、

#include <iostream>
using namespace std;
int& element(int x[],int index)
{
return x[index+1];
}
void main()
{
int a[]={3,7,2,1,5};
element(a,3)=9;
for(int i=0;i<5;i++)
cout<<a[i]<<’’;
}

2、

#include<iostream>
using namespace std;
class A
{
int num;
public:
A()
{
num=0;cout<<"A default constructor"<<endl;}
A(int n)
{num=n;cout<<”A constructor,num=”<<num<<endl;}
~A()
{cout<<"A destructor,num="<<num<<endl;}
};
void main()
{
A a,*p;
p=new A(2);
a=*p;
delete p;
cout<<”Exiting main”<<endl;
}

3、

#include<iostream>
using namespace std;
class Format
{
public:
virtual void header()
{cout<<"This is a head"<<endl;
}virtual void footer()
{cout<<"This is a footer"<<endl;}
virtual void body()
{cout<<"This is a body"<<endl;}
void display()
{header();body();footer();}
};
class MyFormat:public Format
{
public:
void header()
{cout<<"This is my header"<<endl;}
void footer()
{cout<<"This is my footer"<<endl;}
};
void main()
{
Format * p;
p=new Format;
p->display();
delete p;
p=new MyFormat;
p->display();
delete p;
}

4、从键盘输入8 4 3 1 9 6 1 1 2 5,给出程序的输出结果。

#include<iostream>
#include<vector>
#include<stack>
#include<iterator>
using namespace std;
main()
{
vector<int> v;
stack<int> s;
int i,n,x;
cin>>n;
for(i=0;i<n;i++)
{
cin>>x;
v.push_back(x);
}
for(vector<int>::iterator p=v.begin();p!=v.end();p++)
if(*p%2==0)
s.push(*p);
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
}

5、

#include<iostream>
#include<exception>
using namespace std;
class Fract
{
int den,num;
public:
Fract(int d=0,int n=1)
{
if(n==0)
throw exception("divided by zero");
den = d;num=n;
}
int& operator[](int index)
{
if(index<0||index>1)
throw exception("index out of range");
if(index==0)
return den;
if(index==1)return num;
}
void show()
{ cout<<den<<"/"<<num<<endl;}
};
void main()
{
try
{
Fract f(0,2);
f.show();
cout<<f[1]<<"/"<<f[2]<<endl;
}
catch(exception e)
{
cout<<e.what()<<endl;
}
}
第170题

请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:Hello

hELLO

#include <iostream.h>
#include <string.h>
class Words
{char *str;
public:
Words(char *s)
{str=new char[strlen(s)+1];
strcpy(str,s);
}
void disp(){cout<<str<<endl;}
char operator[](int i)
{if(str[i]>='A'&&str[i]<='Z')
return char(str[i]+32);
else if(str[i]>='a'&&str[i]<='z')
return char(str[i]-32);
else 
return str[i];
}
};
int main()
{int i;char *s="Hello";
Words word(s);
______________(1)____________;
for (i=0;i<strlen(s);i++)
______________(2)____________;
cout<<endl;
}
第171题

利用函数模板,设计求一个数组元素之和的函数sum和两个数组元素之和的函数sum,请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

数组a之和:15

数组b之和:55

两数组之和:70

#include <iostream.h>
template <class T>
T sum (T a[],int n)
{int i;
T s=0;
for(i=0;i<n;i++)
s+=a[i];
return s;
}
template <class T>
T sum(______________(1)____________)
{
return ______________(2)____________;
}
int main()
{int a[5]={1,2,3,4,5};
int b[10]={1,2,3,4,5,6,7,8,9,10};
int s1=sum(a,5);
int s2=sum(b,10);
int s3=sum(a,5,b,10);
cout<<"数组a之和:"<<s1<<endl;
cout<<"数组b之和:"<<s2<<endl;
cout<<"两数组之和:"<<s3<<endl;
}
第172题

请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为

(1,2)

5,6

(6,9)

#include <iostream.h>
class A
{
public:
A(int i,int j){a=i;b=j;}
void move(int x,int y){ ____(1)______;b+=y;}
void show(){cout<<"("<<a<<","<<b<<")"<<endl;}
private:
int a,b;
};
class B:private A
{
public:
B(int i,int j,int k,int l):A(i,j){x=k;y=l;}
void show(){cout<<x<<","<<y<<endl;}
void fun(){move(3,5);}
void fl(){____(2)______;}
private:int x,y;
};
int main()
{A e(1,2);
e.show();
B d(3,4,5,6);
d.fun();
d.show();
d.fl();
}
第173题

请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

10+10i

#include <iostream.h>
class complex
{
int x,y;
public:complex(){}
complex(int i,int j)
{x=i; y=j;}
complex operator+(complex &p)
{________(1)_______;}
void show()
{cout<<x<<"+"<<y<<"i";}
};
int main()
{complex p1(5,8),p2(5,2),p3;
________(2)_______;
p3.show();
第174题

程序阅读题

1、

#include <iostream.h>
class B
{
public:
B(){}
B(int i,int j){a=i;b=j;}
void printb()
{cout<<"a="<<a<<",b="<<b<<endl;}
private:
int a,b;
};
class A
{
B c;
public:
A(){}
A(int i,int j):c(i,j){}
void printa(){c.printb();}
};
int main()
{A a(7,8);
a.printa();
}

2、

#include <iostream.h>
class CArray
{int *m_pArray;
int m_iSize;
public:
CArray(int iArray[],int iSize)
{m_pArray=iArray;
m_iSize=iSize;
}
int GetSize()
{return m_iSize;}
int &operator[](int i)
{return m_pArray[i];}
};
int main()
{int s[]={3,7,2,1,5};
CArray oArray(s,5);
oArray[0]=9;
for(int i=0;i<5;i++)
cout<<oArray[i]<<" ";
cout<<endl;
}

3、

#include <iostream.h>
#include <math.h>
template <class T>
class TAdd
{T x,y;
public:
TAdd(T a,T b){x=a,y=b;}
T add(){return x+y;}
};
int main()
{TAdd<int> A(5,6);
TAdd<double> B(2.4,5.8);
cout<<"s1="<<A.add()<<endl;
cout<<"s2="<<B.add()<<endl;
}

4、

#include <iostream.h>
class base
{
public:
void who(){cout<<"base class"<<endl;}
};
class derive1:public base
{
public:
void who(){cout<<"derive1 class"<<endl;}
};
class derive2:public base
{
public:
void who(){cout<<"derive2 class"<<endl;}
};
int main()
{base obj1,*p;
derive1 obj2;
derive2 obj3;
p=&obj1;
p->who();
p=&obj2;
p->who();
p=&obj3;
p->who();
obj2.who();
obj3.who();
}

5、

#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
double area(double a, double b, double c) {
if (a <= 0 || b <= 0 || c <= 0)
throw invalid_argument("the side length should be positive");
if (a + b <= c || b + c <= a || c + a <= b)
throw invalid_argument("the side length should fit the triangle inequality");
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main() {
double a, b, c;cin >> a >> b >> c; //输入1 2 4
try {
double s = area(a, b, c);
cout << "Area: " << s << endl;
} catch (exception &e) {
cout << "Error: " << e.what() << endl;
}
}