2012面向对象程序设计试题集 下载本文

cin>>h; s=3.14159*a*a; v=s*h; cout<<"底面积为:"<<s<<endl; cout<<"体积为:"<<v<<endl; return 0;

}

5、编写一个程序,输入年、月,打印出该年份该月的天数。

#include<stdio.h>

main()

{

int y,m,d;

printf("year month=");scanf("%d%d",&y,&m);

switch(m){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:d=31;break;

case 4:

case 6:

case 9:

case 11:d=30;break;

case 2:if (y%4==0 && y%100!=0 || y%400==0) d=29; else d=28;

}

printf("days=%d\n",d);

}

6、编写函数将化氏温度转换为摄氏温度,公式为C=(F-32)*5/9;并在主函数中调用。

#include<iostream>

using namespace std;

double fun(double a);

int main()

{

double f=37.5,c;

c=fun(f);

cout<<"华氏温度为:"<<f<<"℉"<<endl;

cout<<"摄氏温度为:"<<c<<"℃"<<endl;

return 0;

}

double fun(double a)

{

double b;

b=(a-32)*5/9;

return b;

}

7、声明一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)用以对ages 加上years,showage( )用以显示tree对象的ages值。在主函数中定义Tree类对象,并调用成员函数(学生自行指定实参数

#include<iostream>

using namespace std;

class Tree

{

private:

int ages;

public:

int grow(int years)

{

ages=ages+years;

return ages;

}

void getage()

{

cout<<"输入树的树龄:"<<endl;

cin>>ages;

}

void showage()

{cout<<"该树的年龄是:"<<ages<<endl;}

};

int main()

{

Tree ages,years;

ages.getage();

ages.grow(5);

ages.showage();

return 0;

}

8、定义一个复数类,用友元函数实现对双目运算符“+”的运算符重载,使其适用于复数运算。

#include<iostream.h>

class Complex

{

private:

double real;

double imag;

public:

Complex(){real=0;imag=0;}

Complex(double r,double i):real(r),imag(i){}

friend Complex operator+(Complex &c1,Complex &c2);

void display();

};

void Complex::display()

{

cout<<real<<"+"<<imag<<"i"<<endl;

}

Complex operator+(Complex &c1,Complex &c2)

{

return Complex(c1.real+c2.real,c1.imag+c2.imag);

}

int main()

{

Complex c1(3,4);

Complex c2(4,2.3);

Complex c3;

c3=c1+c2;

c3.display();

return 0;

}

9、有一个函数如下:

输入x的值,计算出相应的y值。

#include<iostream>

using namespace std;

int main()

{

int x,y;

cin>>x;

if(x<5) y=5;

if(x>=5&&x<15) y=x+6;

if(x>=15) y=x-6;

cout<<y<<endl;

return 0;

}

10、14、17、使用函数重载的方法定义两个重名函数,分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。

#include<iostream>

using namespace std;

template<typename T>

T add(T a,T b)

{

T c;

c=a+b;

return c;

}

int main()

{

int a,b,c;

float x,y,z;

cout<<"请输入两个整型数:"<<endl;

cin>>a>>b;

cout<<"请输入两个浮点数:"<<endl;

cin>>x>>y;

c=add(a,b);

z=add(x,y);

cout<<"整型数之和是:"<<c<<endl;

cout<<"浮点数之和是:"<<z<<endl;

return 0;

}

11、定义一个抽象类shape用以计算面积,从中派生出计算长方形、梯形、圆形面积的派生类。程序中通过基类指针来调用派生类中的虚函数,计算不同形状的面积。

#include <iostream>

#define PI 3.1415926

using namespace std;

class Shape

{

public:

void show();

protected:

double s;

};

void Shape::show()

{

cout<<"面积:"<<s<<endl;

}

class Circle :public Shape

{

public:

void GetArea();

Circle(double);

private:

double r;

};

Circle::Circle(double a)

{

r=a;

}

void Circle::GetArea()

{

s=r*r*PI;

}

int main()

{

Circle C(6);

C.GetArea();

C.show();

return 0;

}

12、定义计数器类Counter。要求具有以下成员:计数器值;可进行增值和减值记数;可提供记数值。 #include <iostream>

using namespace std;

class Counter

{

public:

Counter(int );

Counter operator ++();

Counter operator --();

void display();

private:

int i;

};

Counter::Counter(int a)

{

i=a;

}

void Counter::display()

{

cout<<i;

}

Counter Counter:: operator ++()

{

return Counter (++i);

}

Counter Counter:: operator --()

{

return Counter(--i);

}

int main()

{

Counter C1(5);

cout<<"C1=";C1.display();

++C1;

cout<<endl<<"C1=";C1.display();

--C1;

cout<<endl<<"C1=";C1.display();

cout<<endl;

return 0;

}

13、输入一个自然数,输出其各因子的连乘形式,如输入12,则输出12=1*2*2*3。

#include<iostream>

using namespace std;

int main()

{

int i,n;

cin>>n;

cout<<"n="<<"1";

for(i=2;i<=n;)

{

if(n%i==0)

{

n=n/i;