运算符重载有关习题与答案

发布时间:2012-05-15 16:06:05   来源:文档文库   
字号:

1.   (10)

学生类——静态数据成员和静态成员函数

题目描述

定义一个类Student,要求使用静态数据成员或静态成员函数计算全班学生的《计算机导论》课程的总成绩和平均成绩。
静态数据成员:static int total;
静态成员函数:static void Average(){}

 

输入描述

学生姓名 课程成绩

 

输出描述

总成绩和平均成绩

 

输入样例

Zhang 82

Li 79

Wang 93

Liu 66

Xia 90

 

输出样例

410

82

我的代码:

#include

#include

#include

using namespace std ;

class Student

{

public:

Student( char*p , int s );

void total_score( int s );

static int Total();

static void Average();

private:

static int total ;

static int num ;

static double average ;

int score ;

char name[20] ;

};

int Student::total = 0 ;

double Student::average = 0.0 ;

int Student::num = 0 ;

Student::Student( char *p , int s )

{

strcpy( name , p ) ;

score = s ;

}

void Student::total_score( int s )

{

score = s ;

num ++ ;

total += score ;

}

int Student::Total()

{

return total ;

}

void Student::Average()

{

average = ( double ) total / num ;

cout << average << endl ;

}

int main ()

{

char name1[20] , name2[20] , name3[20] , name4[20] , name5[20] ;

int s1 , s2 , s3 , s4 , s5 ;

cin >> name1 >> s1 >> name2 >> s2 >> name3 >> s3 ;

cin >> name4 >> s4 >> name5 >> s5 ;

Student stu1( name1 , s1 ) , stu2( name2 , s2 ) , stu3( name3 , s3 );

Student stu4( name4 , s4 ) , stu5( name5 , s5 ) ;

stu1.total_score( s1 ) ;

stu2.total_score( s2 ) ;

stu3.total_score( s3 ) ;

stu4.total_score( s4 ) ;

stu5.total_score( s5 ) ;

cout << Student::Total() << endl ;

Student::Average() ;

return 0 ;

}

代码二:

#include

#include

using namespace std ;

class Stase

{

private:

static int total;

static int n;

string name;

double mark;

static double Average();

public:

static void show();

Stase();

Stase(const string &Name,const int Mark):name(Name),mark(Mark){

total+=Mark;

n++;

}

};

int Stase::total=0;

int Stase::n=0;

double Stase::Average()

{

return total/n;

}

void Stase::show()

{

cout<

cout<

}

int main()

{ string name;

int numble;

for(int i=0;i<5;++i)

{

cin>>name>>numble;

Stase stase(name,numble);

}

Stase::show();

return 0;

}

 

窗体顶端

窗体底端

2.   (10)

学生成绩高低——友元函数

题目描述

在第一题的基础上,设计一个友元函数,比较某两个学生成绩的高低。

 

输入描述

学生姓名和分数

 

输出描述

分数高低的结果(><==)

 

输入样例

zhang 92

li 89

 

输出样例

>

 

我的代码:

#include

#include

#include

using namespace std ;

class Student

{

public:

Student( char*p , int s );

void total_score( int s );

static int Total();

static void Average();

friend void Compare( Student &s1 , Student &s2 );

private:

static int total ;

static int num ;

static double average ;

int score ;

char name[20] ;

};

int Student::total = 0 ;

double Student::average = 0.0 ;

int Student::num = 0 ;

Student::Student( char *p , int s )

{

strcpy( name , p ) ;

score = s ;

}

void Student::total_score( int s )

{

score = s ;

num ++ ;

total += score ;

}

int Student::Total()

{

return total ;

}

void Student::Average()

{

average = ( double ) total / num ;

cout << average << endl ;

}

void Compare( Student &s1 , Student &s2 )

{

if( s1.score > s2.score )

{

cout << ">" << endl ;

}

else

if( s1.score == s2.score )

cout << "==" << endl ;

else

cout << "<" << endl ;

}

int main ()

{

char name1[20] , name2[20] ;

int s1 , s2 ;

cin >> name1 >> s1 >> name2 >> s2 ;

Student stu1( name1 , s1 ) , stu2( name2 , s2 ) ;

Compare( stu1 , stu2 ) ;

return 0 ;

}

窗体顶端

窗体底端

3.   (10)

栈类——类模板

题目描述

用类模板方式设计一个栈类stack,其中有两个私有数据成员:s[](存放栈元素)和top(栈顶元素下标),以及3个公有成员函数:push(元素入栈)、pop(元素出栈)和stackempty(判断栈是否为空),并建立一个整数栈和一个字符栈。
template
class stack
{
};

 

输入描述

输入整数栈的数据元素和字符栈的数据元素

 

输出描述

输出整数栈的数据元素出栈序列和字符栈的数据元素出栈序列

 

输入样例

4 8 3 2

a d b c

 

输出样例

2 3 8 4

c b d a

我的代码:

#include

using namespace std ;

template

class Stack

{

public:

Stack(T t) ;

void Push(T value) ;

T Pop() ;

bool Is_empty()const ;

int Size() ;

~Stack() ;

private:

int top ;

T s[100] ;

};

template Stack::Stack( T t )

{

for( int i = 0 ; i < 100 ; i ++ )

s[i] = t ;

top = -1 ;

}

template void Stack::Push(T value)

{

top ++ ;

s[top] = value ;

}//入栈

template T Stack ::Pop()

{

return s[top --] ;

}//出栈

template bool Stack ::Is_empty()const

{

return top == -1 ? 1 : 0 ;

}//判断栈是否为空

template int Stack::Size()

{

return top + 1 ;

}

template Stack::~Stack()//析构函数~Stack()实现体

{

}

int main()

{

int x1 , x2 , x3 , x4 ;

char y1 , y2 , y3 , y4 ;

cin >> x1 >> x2 >> x3 >> x4 ;

cin >> y1 >> y2 >> y3 >> y4 ;

Stacks1(4) ;

s1.Push( x1 ) ;

s1.Push( x2 ) ;

s1.Push( x3 ) ;

s1.Push( x4 ) ;

Stacks2(4) ;

s2.Push( y1 ) ;

s2.Push( y2 ) ;

s2.Push( y3 ) ;

s2.Push( y4 ) ;

while( !s1.Is_empty () )

{

cout << s1.Pop() << " " ;

}

cout << endl ;

while( !s2.Is_empty() )

{

cout << s2.Pop() << " ";

}

return 0 ;

}

 

窗体顶端

窗体底端

4.   (10)

时间类——类和对象

题目描述

仿照教材第13章的Date类设计一个Time类,设计多个重载的构造函数,可以设置时间、进行时间的加减运算、按12小时格式和24小时格式输出时间。
例如:
class Time
{
  int hour,minute,second;
public:
  int SecCalc()  {  return(hour*60+minute)*60+second; }
  Time(int h=0,int m=0, int s=0);
  void SetTime(int h=0,int m=0, int s=0);
  void print_12();
  void print_24();
  Time Add(Time&);
  Time Sub(Time&);
};
其中成员函数Time::print_12()12小时格式显示时间,如09:20:45 AM”、03:15:20 PM”;成员函数Time::print_24()24小时格式显示时间,如09:20:45”、4:30:20”;成员函数Time Time::Add(Time&)用来实现两个时间对象的值进行相加;成员函数Time Time::Sub(Time&)用来实现两个时间对象的值进行相减。
为了检验Time类的功能,主函数按如下方式设计:
int main()
{
  Time t1(2,34),t2;
  t2.SetTime(13,23,34);
  cout<<"t2:";
  t2.print_12();
  cout<
  t2.print_24();
  cout<<"\nt1+t2:";
  t1.Add(t2).print_24();
  cout<<"\nt1-t2:";
  t1.Sub(t2).print_24();
  return 0;
}

 

输入描述

 

输出描述

 

输入样例

 

输出样例

t2:01:23:34 PM

t2:13:23:34

t1+t2:15:57:34

t1-t2:10:49:34

我的代码:

#include

#include

using namespace std ;

class Time

{

int hour , minute , second ;

public:

int SecCalc()

{

return ( hour * 60 + minute ) * 60 + second ;

}

Time( int h = 0 , int m = 0 , int s = 0 ) ;

void SetTime( int h = 0 , int m = 0 , int s = 0 ) ;

void print_12() ;

void print_24() ;

Time Add( Time& ) ;

Time Sub( Time& ) ;

} ;

Time::Time( int h , int m , int s )

{

hour = h ;

minute = m ;

second = s ;

}

void Time::SetTime( int h , int m , int s )

{

hour = h ;

minute = m ;

second = s ;

}

void Time::print_12()

{

if( hour >= 0 && hour <= 11 )

{

cout << setfill('0') ;

cout << setw(2) << hour << ":" << minute << ":" << second << " AM" ;

}

else

{

cout << setfill('0') ;

cout << setw(2) << hour - 12 << ":" << minute << ":" << second << " PM" ;

}

}

void Time::print_24()

{

cout << setfill('0') ;

cout << setw(2) << hour << ":" << minute << ":" << second ;

}

Time Time::Add( Time & t1 )

{

int temp = ( hour*3600 + minute*60 + second ) + ( t1.hour*3600 + t1.minute*60 + t1.second ) ;

hour = temp / 3600 ;

minute = (temp % 3600 ) / 60 ;

second = temp % 3600 % 60 ;

return *this ;

}

Time Time::Sub( Time & t2 )

{

int temp = ( hour*3600 + minute*60 + second ) - ( t2.hour*3600 + t2.minute*60 + t2.second ) ;

if( temp < 0 )

temp = - temp ;

hour = temp / 3600 ;

minute = (temp % 3600 ) / 60 ;

second = temp % 3600 % 60 ;

return *this ;

}

int main()

{

Time t1 ( 2 , 34 ) , t2 ;

t2.SetTime ( 13 , 23 , 34 ) ;

cout << "t2:" ;

t2.print_12 () ;

cout << endl << "t2:" ;

t2.print_24 () ;

cout << "\nt1+t2:" ;

t1.Add( t2 ).print_24() ;

t1.SetTime( 2 , 34 ) ;

cout << "\nt1-t2:" ;

t1.Sub( t2 ).print_24() ;

return 0 ;

}

 

窗体顶端

窗体底端

5.   (10)

时间类——赋值运算符重载

题目描述

对第4题设计的Time类进行修改,通过重载+”、-运算符直接进行时间的加减运算。
提示:
(1)用友元函数来实现运算符的重载
(2)加法运算符可以是两个Time对象进行相加,也可以是一个表示秒数的int型数据加上一个Time对象,可以是Time对象加上int型数据,得到的结果都是Time类型的对象。
(3)减法运算符可以是两个Time对象进行相减,也可以是Time对象减去一个表示秒数的int型数据,得到的结果都是Time类型的对象。
主函数设计如下:
int main()
{
  Time t1(2,34),t2,t3;
  t2.SetTime(13,23,34);
  cout<<"\nt1+t2:";
  t3=t1+t2;  //两个Time类对象相加
  t3.print_24();
  cout<<"\nt1+65:";
  t3=t1+65;  //Time类对象加上65
  t3.print_24();
  cout<<"\n65+t1:";
  t3=65+t1;  //65秒加上Time类对象
  t3.print_24();  
  cout<<"\nt1-t2:";
  t3=t1-t2;  //两个Time类对象相减
  t3.print_24();
  cout<<"\nt1-70:";
  t3=t1-70;  //Time类对象减去70
  t3.print_24();  
  return 0;
}

 

输入描述

 

输出描述

 

输入样例

 

输出样例

t1+t2:15:57:34

t1+65:02:35:05

65+t1:02:35:05

t1-t2:10:49:34

t1-70:02:32:50

我的代码:

#include

#include

using namespace std ;

class Time

{

int hour , minute , second ;

public:

int SecCalc()

{

return ( hour * 60 + minute ) * 60 + second ;

}

Time( int h = 0 , int m = 0 , int s = 0 ) ;

void SetTime( int h = 0 , int m = 0 , int s = 0 ) ;

void print_12() ;

void print_24() ;

Time operator+ ( Time t1 ) ;

friend Time operator+ ( Time t1 , int t2 ) ;

friend Time operator+ ( int t1 , Time t2 ) ;

Time operator- ( Time t1 ) ;

friend Time operator- ( Time t1 , int t2 ) ;

friend Time operator- ( int t1 , Time t2 ) ;

} ;

Time Time::operator+( Time t1 )

{

return Time( hour + t1.hour , minute + t1.minute , second + t1.second ) ;

}

Time operator+ ( Time t1 , int t2 )

{

int t = t1.SecCalc();

t += t2;

return Time( t/60/60 , t/60%60 , t%60 ) ;

}

Time operator+ ( int t1 , Time t2 )

{

int t = t2.SecCalc();

t += t1;

return Time( t/60/60 , t/60%60 , t%60 ) ;

}

Time Time::operator- ( Time t1 )

{

int t = SecCalc() - t1.SecCalc();

if (t < 0)

{

t = -t;

}

return Time( t/60/60 , t/60%60 , t%60 );

}

Time operator- ( Time t1 , int t2 )

{

int t = t1.SecCalc() - t2;

if (t < 0)

{

t = -t;

}

return Time( t/60/60 , t/60%60 , t%60 ) ;

}

Time operator- ( int t1 , Time t2 )

{

int t = t1 - t2.SecCalc();

if ( t < 0 )

{

t = -t;

}

return Time( t/60/60 , t/60%60 , t%60 );

}

Time::Time( int h , int m , int s )

{

hour = h ;

minute = m ;

second = s ;

}

void Time::SetTime( int h , int m , int s )

{

hour = h ;

minute = m ;

second = s ;

}

void Time::print_12()

{

if( hour >= 0 && hour <= 11 )

{

cout << setfill('0') ;

cout << setw(2) << hour << ":" << minute << ":" << second << " AM" ;

}

else

{

cout << setfill('0') ;

cout << setw(2) << hour - 12 << ":" << minute << ":" << second << " PM" ;

}

}

void Time::print_24()

{

cout << setfill('0') ;

cout << setw(2) << hour << ":" << minute << ":" << setw(2) << second ;

}

int main()

{

Time t1( 2 , 34 ) , t2 , t3 ;

t2.SetTime( 13 , 23 , 34 ) ;

cout << "\nt1+t2:" ;

t3 = t1 + t2 ; //两个Time类对象相加

t3.print_24();

cout << "\nt1+65:" ;

t3 = t1 + 65 ; //Time类对象加上65

t3.print_24() ;

cout << "\n65+t1:" ;

t3 = 65 + t1 ; //65秒加上Time类对象

t3.print_24() ;

cout << "\nt1-t2:" ;

t3 = t1 - t2 ; //两个Time类对象相减

t3.print_24() ;

cout << "\nt1-70:" ;

t3 = t1 - 70 ; //Time类对象减去70

t3.print_24() ;

return 0 ;

}

 

窗体顶端

窗体底端

6.   (10)

分数类——运算符重载

题目描述

构造一个分数类rationalNumber,该类中包括分子和分母两个成员数据,并具有下述功能:
1)建立构造函数,它能防止分母为零,当分数不是最简形式时进行约分,并避免分母为负数。
2)重载加法、减法、乘法以及除法运算符。
3)重载关系运算符:><==等。
主函数设计如下:
int main ()
{
rationalNumber p1(3,5),p2(12,16),p3;
cout<<"p1="; p1.disp();
cout<<"p2="; p2.disp();
p2.simple( );
cout<<"after simple ";
cout<<"p2=";p2.disp();
p3=p1+p2; cout<<"p1+p2=";p3.disp();
p3=p1-p2;cout<<"p1-p2=";p3.disp();
p3=p1*p2; cout<<"p1*p2=";p3.disp();
p3=p1/p2; cout<<"p1/p2=";p3.disp();
if(p1==p2) cout<<"p1=p2"<
else if(p1>p2)cout<<"p1"<<">"<<"p2";
else if(p1
cout<
return 0;
}

 

输入描述

 

输出描述

 

输入样例

 

输出样例

p1=3/5

p2=12/16

after simple p2=3/4

27/20

p1+p2=27/20

p1-p2=-3/20

p1*p2=9/20

p1/p2=4/5

p1

我的代码

#include

using namespace std;

class rationalNumber

{

public:

rationalNumber( int a = 0 ,int b = 1 ) ;//构造函数

void Set( int a , int b )

{

m = a ;

n = b ;

}

void simple() ;

void disp() ;// 输出函数

rationalNumber operator+( rationalNumber t1 ) ;

rationalNumber operator-( rationalNumber t1 ) ;

rationalNumber operator*( rationalNumber t1 ) ;

rationalNumber operator/( rationalNumber t1 ) ;

bool operator>( rationalNumber t1 ) ;

bool operator<( rationalNumber t1 ) ;

bool operator==( rationalNumber t1 ) ;

private:

int m , n ;

} ;

rationalNumber::rationalNumber( int a , int b )

{

m = a ;

n = b ;

}

void rationalNumber::simple()

{

int min ;

if( m < n )

min = m ;

min = n ;

for( int i = 2 ; i <= min ; i ++ )

{

while( m % i == 0 && n % i == 0)

{

m = m / i ;

n = n / i ;

min = min / i ;

}

}

}

void rationalNumber::disp()

{

cout << m << "/" << n << endl ;

}

rationalNumber rationalNumber::operator+( rationalNumber t1 )

{

m = m * t1.n + t1.m * n ;

n = n * t1.n ;

simple() ;

return rationalNumber( m , n ) ;

}

rationalNumber rationalNumber::operator-( rationalNumber t1 )

{

m = m * t1.n - t1.m * n ;

n = n * t1.n ;

simple() ;

return rationalNumber( m , n ) ;

}

rationalNumber rationalNumber::operator*( rationalNumber t1 )

{

m = m * t1.m ;

n = n * t1.n ;

simple() ;

return rationalNumber( m , n ) ;

}

rationalNumber rationalNumber::operator/( rationalNumber t1 )

{

m = m * t1.n ;

n = n * t1.m ;

simple() ;

return rationalNumber( m , n ) ;

}

bool rationalNumber::operator>( rationalNumber t1 )

{

if( m * t1.n > n * t1.m )

{

return 1 ;

}

return 0 ;

}

bool rationalNumber::operator<( rationalNumber t1 )

{

if( m * t1.n < n * t1.m )

{

return 1 ;

}

return 0 ;

}

bool rationalNumber::operator==( rationalNumber t1 )

{

if( m * t1.n == n * t1.m )

{

return 1 ;

}

return 0 ;

}

int main ()

{

rationalNumber p1( 3 , 5 ) , p2( 12 , 16 ) , p3 ;

cout<<"p1="; p1.disp();

cout << "p2=" ;

p2.disp() ;

p2.simple( );

cout << "after simple " ;

cout << "p2=" ;

p2.disp() ;

p3 = p1 + p2 ;

cout << "p1+p2=" ;

p3.disp() ;

p1.Set( 3 , 5 );

p3 = p1 - p2 ;

cout << "p1-p2=" ;

p3.disp() ;

p1.Set( 3 , 5 );

p3 = p1*p2 ;

cout << "p1*p2=" ;

p3.disp() ;

p1.Set( 3 , 5 ) ;

p3 = p1/p2 ;

cout << "p1/p2=" ;

p3.disp();

p1.Set( 3 , 5 ) ;

if( p1 == p2 )

cout << "p1=p2" << endl ;

else

{

if( p1 > p2 )

cout << "p1" << ">" << "p2" ;

else

if( p1 < p2 )

cout << "p1" << "<" << "p2" ;

}

cout<

return 0;

}

 

窗体顶端

窗体底端

7.   (10)

水果超市管理系统——3——水果类

题目描述

水果类Fruit的设计:在水果超市管理系统中要处理各种各样的水果(利润,正价水果、特价水果,苹果、橘子、香蕉等),无论是哪类水果都具备水果的基本特征,因此可以将各种水果的共性特征抽取出来,形成一个基本类,这就是水果类,包括水果编号、水果名称、水果进价等数据成员以及修改和输出这些数据成员的成员函数等。水果类定义可保存在Fruit.h文件中。
为了检验Fruit类的功能,在Fruit.cpp文件中设计主函数如下:
#include
#include
#include"fruit.h"
using namespace std;
int main()
{
  Fruit Apple,Orange(1002,"Orange",0.8);
  Apple.SetFruitNumber(10001);
  Apple.SetFruitName("Apple");
  Apple.SetPurchasePrice(1.2);
  Apple.DispFruit();
  cout<
  cout<<"水果编号:"<
  cout<<"名称:"<
  cout<<"进价:"<
  return 0;
}

 

输入描述

请大家参阅最初发下去的文档,统一使用规范的数据成员名称和成员函数名称。

 

输出描述

 

输入样例

 

输出样例

水果编号:10001,名称:Apple

水果编号:1002

名称:Orange

进价:0.8

我的代码:

#include

#include

using namespace std;

class Fruit

{

public:

Fruit( int a = 0 , string b = "0" , double c = 0.0) ;

int GetFruitNumber() ;

string GetFruitName() ;

double GetPrice() ;

void SetFruitNumber( int t1 ) ;

void SetFruitName( string t2 ) ;

void SetPurchasePrice( double t3 ) ;

void DispFruit() ;

private:

int number ;

string name ;

double price ;

} ;

Fruit::Fruit( int a , string b , double c )

{

number = a ;

name = b ;

price = c ;

}

int Fruit::GetFruitNumber()

{

return number ;

}

string Fruit::GetFruitName()

{

return name ;

}

double Fruit::GetPrice()

{

return price ;

}

void Fruit::SetFruitNumber( int t1 )

{

number = t1 ;

}

void Fruit::SetFruitName( string t2 )

{

name = t2 ;

}

void Fruit::SetPurchasePrice( double t3 )

{

price = t3 ;

}

void Fruit::DispFruit()

{

cout << "水果编号:" << number << ",名称:" << name << endl ;

}

int main()

{

Fruit Apple , Orange( 1002 , "Orange" , 0.8 ) ;

Apple.SetFruitNumber(10001) ;

Apple.SetFruitName( "Apple" ) ;

Apple.SetPurchasePrice( 1.2 );

Apple.DispFruit() ;

cout << endl ;

cout << "水果编号:" << Orange.GetFruitNumber() << endl ;

cout << "名称:" << Orange.GetFruitName() << endl ;

cout << "进价:" << Orange.GetPrice() << endl ;

return 0 ;

}

 

完毕窗体顶端

窗体底端

窗体顶端

窗体底端

本文来源:https://www.2haoxitong.net/k/doc/e94362f09e3143323968933d.html

《运算符重载有关习题与答案.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式