C++习题与解析-重载

2016-02-19 18:01 57 1 收藏

岁数大了,QQ也不闪了,微信也不响了,电话也不来了,但是图老师依旧坚持为大家推荐最精彩的内容,下面为大家精心准备的C++习题与解析-重载,希望大家看完后能赶快学习起来。

【 tulaoshi.com - 编程语言 】


  01.分析以下程序执行结果
  #includeiostream.h
  int add(int x,int y)
  {
  return x+y;
  }
  double add(double x,double y)
  {
  return x+y;
  }
  void main()
  {
  int a=4,b=6;
  double c=2.6,d=7.4;
  coutadd(a,b)","add(c,d)endl;
  }
  解:
  本题说明函数重载的使用方法, 这里有两个add()函数,一个add()函数的参数与返回值为int型,另一个的参数与返回值为double型,它们是根据参数类型自动区分的。
  所以输出为: 10,10 -----------------------------------------------
  
  02.分析以下程序的执行结果
  #includeiostream.h
  class Sample
  {
  int i;
  double d;
  public:
  void setdata(int n){i=n;}
  void setdata(double x){d=x;}
  void disp()
  {
  cout"i="i",d="dendl;
  }
  };
  void main()
  {
  Sample s;
  s.setdata(10);
  s.setdata(15.6);
  s.disp();
  }
  解:
  本题说明重载成员函数的使用方法。setdata()成员函数有两个,根据其参数类型加以区分。
  所以输出为:i=10, d=15.6
  
  -----------------------------------------------
  
  03.分析以下程序的执行结果
  #includeiostream.h
  class Sample
  {
  int n;
  public:
  Sample(){}
  Sample(int i){n=i;}
  Sample &operator =(Sample);
  void disp(){cout"n="nendl;}
  };
  Sample &Sample::operator=(Sample s)
  {
  Sample::n=s.n;
  return *this;
  }
  void main()
  {
  Sample s1(10),s2;
  s2=s1;
  s2.disp();
  }
  解:
  本题说明重载运算符(=)的使用方法。operator=成员函数实现两个对象的赋值。
  所以输出为: n=10
  
  -------------------------------------------------
  
  04.设计一个点类Point,实现点对象之间的各种运算。
  解:
  Point类提供了6个运算符重载函数(参加程序中的代码和注释),以实现相应的运算。
  本题程序如下:
  #includeiostream.h
  class Point
  {
  int x,y;
  public:
  Point(){x=y=0;}
  Point(int i,int j){x=i;y=j;}
  Point(Point &);
  ~Point(){}
  void offset(int,int); // 提供对点的偏移
  void offset(Point); // 重载,偏移量用Point类对象表示
  bool operator==(Point); // 运算符重载,判定两个对象是否相同
  bool operator!=(Point); // 运算符重载,判定两个对象是否不相同
  void operator+=(Point); // 运算符重载,将两个点对象相加
  void operator-=(Point); // 运算符重载,将两个点对象相减
  Point operator+(Point ); // 运 算符重 载,相加并将结果放在左操作数中
  Point operator-(Point); // 运算符重载,相减并将结果放在左操作数中
  int getx(){return x;}
  int gety(){return y;}
  void disp()
  {
  cout"("x","y")"endl;
  }
  };
  Point::Point(Point &p)
  {
  x=p.x; y=p.y;
  }
  void Point::offset(int i,int j)
  {
  x+=i; y+=j;
  }
  void Point::offset(Point p)
  {
  x+=p.getx(); y+=p.gety();
  }
  bool Point::operator==(Point p)
  {
  if(x==p.getx()&&y==p.gety())
  return 1;
  else
  return 0;
  }
  bool Point::operator!=(Point p)
  {
  if(x!=p.getx()y!=p.gety())
  return 1;
  else
  return 0;
  }
  void Point::operator+=(Point p)
  {
  x+=p.getx(); y+=p.gety();
  }
  void Point::operator-=(Point p)
  {
  x-=p.getx(); y-=p.gety();
  }
  Point Point::operator+(Point p)
  {
  this-x+=p.x; this-y+=p.y;
  return *this;
  }
  Point Point::operator-(Point p)
  {
  this-x-=p.x;this-y-=p.y;
  return *this;
  }
  void main()
  {
  Point p1(2,3),p2(3,4),p3(p2);
  cout"1:"; p3.disp();
  p3.offset(10,10);
  cout"2:"; p3.disp();
  cout"3:"(p2==p3)endl;
  cout"4:"(p2!=p3)endl;
  p3+=p1;
  cout"5:"; p3.disp();
  p3-=p2;
  cout"6:"; p3.disp();
  p3=p1+p3; // 先将p1+p3的结果放在p1中,然后赋给p3,所以p1=p3
  cout"7:"; p3.disp();
  p3=p1-p2;
  cout"8:"; p3.disp();
  }
  
  本程序的执行结果如下:
  1:(3,4)
  2:(13,14)
  3:0
  4:1
  5:(15,17)
  6:(12,13)
  7:(14,16)
  8:(11,12)
  
  ----------------------------------------------------
  
   05.设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如一日期加上天数、一日期减去天数、两日期相差的天数等。
  解:
  在Date类中设计如下重载运算符函数:
  Date operator+(int days); 返回一日期加一天数得到的日期
  Date operator-(int days); 返回一日期减去天数得到的日期
  int operator-(Date &b); 返回两日期相差的天数
  在实现这些重载运算符函数调用以下私有成员函数:
  leap(int); 判定指定的年份是否为闰年
  dton(Date &); 将指定日期转换为从0年0月0日起的天数
  ntod(int); 将指定的0年0月0日起的天数转换为对应的日期
  本题程序如下:
  #includeiostream.h
  int day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
  {31,29,31,30,31,31,30,31,30,31}};
  // day_tab 二维数组存放各月天数,第一行对应非闰年,第二行对应闰年
  class Date
  {
  int year,month,day;
  int leap(int);
  int dton(Date &);
  Date ntod(int);
  public:
  Date(){}
  Date(int y,int m,int d)
  {
  year=y;month=m;day=d;
  }
  void setday(int d){day=d;}
  void setmonth(int m){month=m;}
  void setyear(int y){year=y;}
  int getday(){return day;}
  int getmonth(){return month;}
  int getyear(){return year;}
  Date operator+(int days)
  {
  static Date date;
  int number=dton(*this)+days;
  date=ntod(number);
  return date;
  }
  Date operator-(int days)
  {
  static Date date;
  int number=dton(*this);
  number-=days;
  date=ntod(number);
  return date;
  }
  int operator-(Date &b)
  {
  int days=dton(*this)-dton(b)-1;
  return days;
  }
  void disp()
  {
  coutyear"."month"."dayendl;
  }
  };
  int Date::leap(int year)
  {
  if(year%4==0&&year%100!=0year%400==0) // 是闰年
  return 1;
  else // 不是闰年
  return 0;
  }
  int Date::dton(Date &d)
  {
  int y,m,days=0;
  for(y=1;y=d.year;y++)
  if(leap(y))
  days+=366;
  else
  days+=365;
  for(m=0;md.month-1;m++)
  if(leap(d.year))
  days+=day_tab[1][m];
  else
  days+=day_tab[0][m];
  days+=d.day;
  return days;
  }
  Date Date::ntod(int n)
  {
  int y=1,m=1,d,rest=n,lp;
  while(1)
  {
  if(leap(y))
  {
  if(rest=366)
  break;
  else
  rest-=366;
  }
  else
  {
  if(rest=365)
  break;
  else
  rest-=365;
  }
  y++;
  }
  y--;
  lp=leap(y);
  while(1)
  {
  if(lp)
  {
  if(restday_tab[1][m-1])
  rest-=day_tab[1][m-1];
  else
  break;
  }
  else
  {
  if(restday_tab[0][m-1])
  rest-=day_tab[0][m-1];
  else
  break;
  }
  m++;
  }
  d=rest;
  return Date(y,m,d);
  }
  void main()
  {
  Date now(2002,6,12),then(2003,2,10);
  cout"now:"; now.disp();
  cout"then:"; then.disp();
  cout"相差天数:"(then-now)endl;
  Date d1=now+100,d2=now-100;
  cout"now+100:"; d1.disp();
  cout"now-100:"; d2.disp();
  }
  
  本程序的执行结果如下:
  now:2002.6.12
  then:2003.2.10
  相差天数:242
  now+100:2002.9.20
  now-100:2002.3.4
  
   题1.分析以下程序的执行结果
  #includeiostream.h
  int add(int x,int y)
  {
  return x+y;
  }
  int add(int x,int y,int z)
  {
  return x+y+z;
  }
  void main()
  {
  int a=4,b=6,c=10;
  coutadd(a,b)","add(a,b,c)endl;
  }
  解:
  本题说明重载函数的使用方法。这里有两个add()函数,一个的参数是2个,另一个的参数是3个,它们是根据参数个数自动区分的。
  所以输出为: 10,20
  
  -------------------------------------------------
  
  题2.分析以下程序的执行结果
  #includeiostream.h
  class Sample
  {
  int i;
  double d;
  public:
  void setdata(int n){i=n;d=0;}
  void setdata(int n,double x)
  {
  i=n;d=x;
  }
  void disp()
  {
  cout"i="i",d="dendl;
  }
  };
  void main()
  {
  Sample s;
  s.setdata(10);
  s.disp();
  s.setdata(2,15.6);
  s.disp();
  }
  解:
  本题说明重载函数的使用方法。setdata()成员函数有2个,根据其参数个数自动加以区分。
  所以输出为:
  i=10,d=0
  i=2,d=15.6
  
  ------------------------------------------------
  
  题3.分析以下程序的执行结果
  #includeiostream.h
  class Sample
  {
  int n;
  public:
  Sample(){}
  Sample(int i){n=i;}
  friend Sample operator-(Sample &,Sample &);
  friend Sample operator+(Sample &,Sample &);
  void disp(){cout"n="nendl;}
  };
  Sample operator-(Sample &s1,Sample &s2)
  {
  int m=s1.n-s2.n;
  return Sample(m);
  }
  Sample operator+(Sample &s1,Sample &s2)
  {
  int m=s1.n+s2.n;
  return Sample(m);
  }
  void main()
  {
  Sample s1(10),s2(20),s3;
  s3=s2-s1;
  s3.disp();
  s3=s2+s1;
  s3.disp();
  }
  解:
  本题说明重载运算符-和+的使用。operator-和operator+两个友元函数实现两个对象的减法和加法。所以输出为:
  n=10 // s2-s1
  n=30 // s2+s1
  
  ---------------------------------------------------
  
  题4.分析以下程序的执行结果
  #includeiostream.h
  class Sample
  {
  int A[10][10];
  public:
  int &operator()(int,int);
  };
  int &Sample::operator()(int x,int y)
  {
  return A[x][y];
  }
  void main()
  {
  Sample a;
  int i,j;
  for(i=0;i10;i++)
  for(j=0;j10;j++)
  a(i,j)=i+j;
  for(i=0;i10;i++)
  couta(i,1)" ";
  coutendl;
  }
  解:
  本题说明重载下标运算符的使用方法。通过重载下标运算符,使得对于对象a,有a(i,j)等于a.A[i][j]。
  所以输出为: 1 2 3 4 5 6 7 8 9 10
  
  ------------------------------------------------
  
  题5.分析以下程序的执行结果
  #includeiostream.h
  class Sample
  {
  int n;
  public:
  Sample(int i){n=i;}
  operator++(){n++;} // 前缀重载运算符
  operator++(int){n+=2;} // 后缀重载运算符
  void disp()
  {
  cout"n="nendl;
  }
  };
  void main()
  {
  Sample A(2),B(2);
  A++; // 调用后缀重载运算符
  ++B; // 调用前缀重载运算符
  A.disp();
  B.disp();
  }
  解:
  本题说明重载运算符++的使用方法。operator++()为前缀重载运算符,operator++(int)为后缀重载运算符。A++的语句调用后缀重载运算符,++B语句调用前缀重载运算符。
  所以输出为:
  n=4
  n=3
  
   题6.设计一个三角形类Triangle,包含三角形三条边长的私有数据成员,另有一个重载运算符“+”,以实现求两个三角形对象的面积之和。
  解:
  在Triangle类中设计一个友元函数operator+(Triangle t1,Triangle t2),它重载运算符"+",返回t1和t2两个三角形的面积之和。
  本题程序如下:
  #includeiostream.h
  #includemath.h
  class Triangle
  {
  int x,y,z;
  double area;
  public:
  Triangle(int i,int j,int k)
  {
  double s;
  x=i;y=j;z=k;
  s=(x+y+z)/2.0;
  area=sqrt(s*(s-x)*(s-y)*(s-z));
  }
  void disparea()
  {
  cout"Area="areaendl;
  }
  friend double operator+(Triangle t1,Triangle t2)
  {
  return t1.area+t2.area;
  }
  };
  void main()
  {
  Triangle t1(3,4,5),t2(4,5,6);
  double s;
  cout"t1:"; t1.disparea();
  cout"t2:"; t2.disparea();
  s=t1+t2;
  cout"总面积="sendl;
  }
  本程序执行结果如下:
  t1:Area=6
  t2:Area=9.92157
  总面积=15.9216
  
  -----------------------------------------------------------
  
  题7.习题6的重载运算符“+”友元函数只能返回两个三角形的面积之和,不能计算三个三角形的面积之和,改进一下,使之能计算任意多个三角形的面积之和。
  解:
  习题6的重载运算符为什么不能计算3个三角形的面积之和呢?对于式子:s=t1+t2+t3,先计算t1+t2,返回一个double数然后再进行该double数+t3的计算,显然没有这样的重载运算符“+”友元函数,只需要添加这样重载运算符“+”友元函数即可。
  本题程序如下:
  #includeiostream.h
  #includemath.h
  class Triangle
  {
  int x,y,z;
  double area;
  public:
  Triangle(int i,int j,int k)
  {
  double s;
  x=i;y=j;z=k;
  s=(x+y+z)/2.0;
  area=sqrt(s*(s-x)*(s-y)*(s-z));
  }
  void disparea()
  {
  cout"Area="areaendl;
  }
  friend double operator+(Triangle t1,Triangle t2)
  {
  return t1.area+t2.area;
  }
  friend double operator+(double d,Triangle t)
  {
  return d+t.area;
  }
  };
  void main()
  {
  Triangle t1(3,4,5),t2(4,5,6),t3(5,6,7),t4(6,7,8);
  double s;
  cout"t1:"; t1.disparea();
  cout"t2:"; t2.disparea();
  cout"t3:"; t3.disparea();
  cout"t4:"; t4.disparea();
  s=t1+t2+t3+t4;
  cout"总面积="sendl;
  }
  本程序的执行结果如下:
  t1:Area=6
  t2:Area=9.92157
  t3:Area=14.6969
  t4:Area=20.3332
  总面积=50.9517
  
  -------------------------------------------------------
  
  题8.设计一个学生类student,包括姓名和三门课程成绩,利用重载运算符”+“将所有学生的成绩相加放在一个对象中,再对该对象求各门课程的平均分。
  解:
  #includeiostream.h
  #includeiomanip.h
  #includestring.h
  class student
  {
  char name[10];
  int deg1,deg2,deg3;
  public:
  student(){}
  student(char na[],int d1,int d2,int d3)
  {
  strcpy(name,na);
  deg1=d1;deg2=d2;deg3=d3;
  }
  friend student operator+(student s1,student s2)
  {
  static student st;
  st.deg1=s1.deg1+s2.deg1;
  st.deg2=s1.deg2+s2.deg2;
  st.deg3=s1.deg3+s2.deg3;
  return st;
  }
  void disp()
  {
  coutsetw(10)namesetw(5)deg1setw(5)deg2setw(5)deg3endl;
  }
  friend void avg(student &s,int n)
  {
  coutsetw(10)"平均分"setw(5)s.deg1/nsetw(5)s.deg2/nsetw(5)s.deg3/nendl;
  }
  };
  void main()
  {
  student s1("Li",78,82,86),s2("Zheng",75,62,89);
  student s3("Ma",89,87,95),s4("Xu",54,78,66),s;
  cout"输出结果"endl;
  s1.disp();
  s2.disp();
  s3.disp();
  s4.disp();
  s=s1+s2+s3+s4; // 调用重载运算符
  avg(s,4); // 友元函数求平均分
  }
  本程序的执行结果如下:
  输出结果:
  Li 78 82 86
  Zheng 75 62 89
  Ma 89 87 95
  Xu 54 78 66
  平均分 74 77 84
  
  ------------------------------------------------------------
  
  题9.在Time类中设计如下重载运算符函数:
  Time operator+(Time); 返回一时间加上另一时间得到的新时间
  Time operator-(Time); 返回一时间减去另一时间得到的新时间
  本题程序如下:
  #includeiostream.h
  class Time
  {
  int hour,minute,second;
  public:
  Time(){}
  Time(int h,int m,int s)
  {
  hour=h;minute=m;second=s;
  }
  Time(int h,int m)
  {
  hour=h;minute=m;second=0;
  }
  void sethour(int h){hour=h;}
  void setminute(int m){minute=m;}
  void setsecond(int s){second=s;}
  int gethour(){return hour;}
  int getminute(){return minute;}
  int getsecond(){return second;}
  Time operator+(Time);
  Time operator-(Time);
  void disp()
  {
  couthour":"minute":"secondendl;
  }
  };
  Time Time::operator+(Time t)
  {
  int carry,hh,mm,ss;
  ss=getsecond()+t.getsecond();
  if(ss60)
  {
  ss-=60;
  carry=1; // 进位标记
  }
  else carry=0;
  mm=getminute()+t.getminute()+carry;
  if(mm60)
  {
  mm-=60;
  carry=1;
  }
  else carry=0;
  hh=gethour()+t.gethour()+carry;
  if(hh24)
  hh=24;
  static Time result(hh,mm,ss);
  return result;
  }
  Time Time::operator-(Time t)
  {
  int borrow,hh,mm,ss;
  ss=getsecond()-t.getsecond();
  if(ss0)
  {
  ss+=60;
  borrow=1; // 借位标记
  }
  else borrow=1;
  mm=getminute()-t.getminute()-borrow;
  if(mm0)
  {
  mm+=60;
  borrow=1;
  }
  else borrow=0;
  hh=gethour()-t.gethour()-borrow;
  if(hh0)
  hh+=24;
  static Time result(hh,mm,ss);
  return result;
  }
  void main()
  {
  Time now(2,24,39);
  Time start(17,55);
  Time t1=now-start,t2=now+start;
  cout"输出结果:"endl;
  cout" now: "; now.disp();
  cout" start:"; start.disp();
  cout" 相差: "; t1.disp();
  cout" 相加: "; t2.disp();
  }
  本程序的执行结果如下:
  输出结果:
  now:2:24:39
  start:17:55:0
  相差:8:28:39
  相加:20:19:39
  
  

来源:https://www.tulaoshi.com/n/20160219/1616635.html

延伸阅读
本文中,将要介绍与继续相关的C++/CLI主题,并以现实生活中银行交易的三种形式:存款、取款、转账,来说明类的继续体系,且以一种新的枚举形式来实现。 枚举器 请看例1中声明的类型,它存在于其自身的源文件中,并编译为一个只包含此类型的程序集: 例1: public enum class TransactionType : ...
为什么需要转换运算符? 大家知道对于内置类型的数据我们可以通过强制转换符的使用来转换数据,例如(int)2.1f;自定义类也是类型,那么自定义类的对象在很多情况下也需要支持此操作,C++提供了转换运算符重载函数,它使得自定义类对象的强转换成为可能。 转换运算符的生命方式比较非凡,方法如下: operator 类名(); ...
与C一样,C++使用布尔表达式简化求值法(short-circuit evaluation)。这表示一旦确定了布尔表达式的真假值,即使还有部分表达式没有被测试,布尔表达式也停止运算。例如: char *p; ... if ((p != 0) && (strlen(p) 10)) ... 这里不用担心当p为空时strlen无法正确运行,因为假如p不等于0的测试失败,st...
函数重载: 在C++程序中,可以将语义、功能相似的几个函数用同一个名字表示,即函数重载。 重载的实现: 几个同名的重载函数仍然是不同的函数,它们是如何区分的呢?我们自然想到函数接口的两个要素:参数与返回值。如果同名函数的参数不同(包括类型、顺序不同),那么容易区别出它们是不同的函数。 重载与覆盖成员函数被重载的特征: ...
在完整描述思想之前,我们先看一下如下的例子,这个例子中的加运算符重载是以非成员函数的方式出现的: !-- frame contents -- !-- /frame contents -- //程序作者:管宁  //站点:www.cndev-lab.com  //所有稿件均有版权,如要转载,请务必闻名出处和作者    #include iostream  ...

经验教程

132

收藏

49
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部