C++习题与解析-引用

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

关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享C++习题与解析-引用教程,希望对大家能有一点小小的帮助。

【 tulaoshi.com - 编程语言 】


  01.分析以下程序的执行结果
  #includeiostream.h
  void main()
  {
  int a;
  int &b=a; // 变量引用
  b=10;
  cout"a="aendl;
  }
  解:
  本题说明变量引用的方法。b是a的引用,它们分配相同的空间,b的值即为a的值。
  所以输出为 a=10。
  
  注重:引用是引入了变量或对明的一个 义词,引用不产生对象的副本。 ----------------------------------------------------------------- 02.分析以下程序的执行结果
  #includeiostream.h
  class Sample
  {
  int x;
  public:
  Sample(){};
  Sample(int a){x=a;}
  Sample(Sample &a){x=a.x+1;}
  void disp(){cout"x="xendl;}
  };
  void main()
  {
  Sample s1(2),s2(s1);
  s2.disp();
  }
  解:
  本题说明类拷贝构造函数的使用方法。Sample类的Sample(Sample &a)构造函数是一个拷贝构造函数,将a对象的x值赋给当前对象的x后加1。
  所以输出为:x=3。
  
  -----------------------------------------------------
  
  03.编写程序,调用传递引用的参数,实现两个字符串变量的交换。
  例如开始:
  char *ap="hello";
  char *bp="how are you?";
  交换的结果使得ap和bp指向的内容分别为:
  ap: "how are you?"
  bp: "hello"
  解:
  本题使用引用调用(实现由于字符串指针本身就是地址,这里可不使用引用参数,效果是一样的)。
  程序如下:
  #includeiostream.h
  #includestring.h
  void swap(char *&x,char *&y) // 引用作为参数
  {
  char *temp;
  temp=x;x=y;y=temp;
  }
  void main()
  {
  char *ap="hello";
  char *bp="how are you?";
  cout"ap:"apendl;
  cout"bp:"bpendl;
  swap(ap,bp);
  cout"swap ap,bp"endl;
  cout"ap:"apendl;
  cout"bp:"bpendl;
  }
  本程序的执行结果如下:
  ap:hello
  bp:hoe are you?
  swap ap,bp
  ap:how are you?
  bp:hello
  
  -----------------------------------------------------
  
  04.设计一个集合类Set,包括将集合置空、添加元素、判定元素是否在集合中、输出集合,以及将集合中元素逆置,另外还有一个拷贝构造函数,并使用一些数据进行测试。
  解:
  Set类包括私有数据成员elems(存放集合元素)、pc(当前元素指针),一个默认构造函数和拷贝构造函数Set(Set &s),另有成员函数empty()(将集合置空)、isempty()(判定集合是否为空)、ismemberof()(判定元素是否在集合中)、add()(添加元素)、print()(输出集合)、reverse(将集合中元素置逆)。
  本题程序如下:
  #includeiostream.h
  #define Max 100
  class Set
  {
  public:
  Set(){pc=0;}
  Set(Set &s); // 对象引用作为参数
  void empty(){pc=0;}
  int isempty(){return pc==0;}
  int ismemberof(int n);
  int add(int n);
  void print();
  void reverse();
  private:
  int elems[Max];
  int pc;
  };
  int Set::ismemberof(int n)
  {
  for(int i=0;ipc;i++)
  if(elems[i]==n)
  return 1;
  return 0;
  }
  int Set::add(int n)
  {
  if(ismemberof(n))
  return 1;
  else if(pcMax)
  return 0;
  else
  {
  elems[pc++]=n;
  return 1;
  }
  }
  Set::Set(Set &p)
  {
  pc=p.pc;
  for(int i=0;ipc;i++)
  elems[i]=p.elems[i];
  }
  void Set::print()
  {
  cout"{";
  for(int i=0;ipc-1;i++)
  coutelems[i]",";
  if(pc0)
  coutelems[pc-1];
  cout"}"endl;
  }
  void Set::reverse()
  {
  int n=pc/2;
  for(int i=0;in;i++)
  {
  int temp;
  temp=elems[i];
  elems[i]=elems[pc-i-1];
  elems[pc-i-1]=temp;
  }
  }
  void main()
  {
  Set A;
  cout"A是否为空:"; coutA.isempty()endl;
  cout"A:"; A.print();
  Set B;
  for(int i=1;i=8;i++)
  B.add(i);
  cout"B:"; B.print();
  cout"5是否在B中:"; coutB.ismemberof(5)endl;
  B.empty();
  for(int j=11;j20;j++)
  B.add(j);
  Set C(B);
  cout"C:"; C.print();
  C.reverse();
  cout"C逆置"endl;
  cout"C:"; C.print();
  }
  本程序执行结果如下:
  A是否为空:1
  A:{}
  B:{1,2,3,4,5,6,7,8}
  5是否在B中:1
  C:{11,12,13,14,15,16,17,18,19}
  C逆置
  C:{19,18,17,16,15,14,13,12,11}
  
   05.设计一个类Sample,实现两个复数的乘法运算。
  解:
  Sample类包括复数的实部和虚部,以及实现复数相乘的成员函数mult()和输出复数的成员函数disp()。
  本题的程序如下:
  #includeiostream.h
  class Sample
  {
  float a; // 实部
  float b; // 虚部
  public:
  Sample(){}
  Sample(float x,float y){a=x;b=y;}
  void mult(Sample &s) // 对象引用作为参数
  {
  if(&s==this) // 不能自己相乘
  cout"自己不能相乘"endl;
  else
  {
  float x=a*s.a-b*s.b;
  float y=a*s.b+b*s.a;
  a=x;b=y;
  }
  }
  void disp()
  {
  if(b0)
  couta"+"b"i"endl;
  else
  couta"-"-b"i"endl;
  }
  };
  void main()
  {
  Sample s1(2,3),s2(3,4);
  cout"复数s1:"; s1.disp();
  cout"复数s2:"; s2.disp();
  s1.mult(s2);
  cout"相乘结果:"; s1.disp();
  coutendl;
  }
  本程序执行结果如下:
  复数s1:2+3i
  复数s2:3+4i
  相乘结果:-6+17i
  
  ------------------------------------------------------
  
  06.有若干教师,每个教师只有姓名,一个教师可以指导多名研究生;每名研究生有姓名、研究方向和班号数据,编写一个程序,要求输出每个教师指导的所有研究生的姓名、研究方向和班号数据。
  解:
  先设计一个学生类student,然后设计一个教师类teacher。teacher类中添加一个student对象数组,存放该教师指导的所有研究生对象,top为当前研究生的指针。这样实现了两个类之间一对多的关系。
  本题程序如下:
  #includeiostream.h
  #includestring.h
  #define Max 10
  class student
  {
  char name[10]; // 姓名
  char search[20]; // 研究方向
  char cname[10]; // 班号
  public:
  student(){}
  student(char n[],char s[],char c[])
  {
  strcpy(name,n);
  strcpy(search,s);
  strcpy(cname,c);
  }
  char *getname(){return name;}
  char *getsearch(){return search;}
  char *getcname(){return cname;}
  };
  class teacher
  {
  int top;
  char name[10];
  student stud[Max]; // 对象数组
  public:
  teacher(char t[]){top=0;strcpy(name,t);}
  void add(student &s) // 对象引用作为参数
  {
  stud[top]=s; top++;
  }
  void disp()
  {
  cout"指导教师:"nameendl" 研究生:"endl;
  for(int i=0;itop;i++)
  {
  cout""stud[i].getname()"(""方向:"stud[i].getsearch()","
  stud[i].getcname()"班)"endl;
  }
  }
  };
  void main()
  {
  teacher t[]={teacher("李明"),teacher("王华")};
  student s1("孙强","数据库","99010");
  student s2("陈文","软件工程","99010");
  student s3("章锐","计算机网络","00010");
  t[0].add(s1);
  t[0].add(s2);
  t[1].add(s3);
  for(int i=0;i2;i++)
  t[i].disp();
  }
  本程序的执行结果如下:
  指导教师:李明
  研究生:
  孙强(方向:数据库,99010班)
  陈文(方向:软件工程,99010班)
  指导教师:王华
  研究生:
  章锐(方向:计算机网络,00010班)
  题1.分析以下程序的执行结果
  #includeiostream.h
  void swap(int &x,int &y)
  {
  int temp;
  temp=x; x=y; y=temp;
  }
  void main()
  {
  int x=10,y=20;
  swap(x,y);
  cout"x="x",y="yendl;
  }
  解:
  这里的函数采用引用调用的方式,所以输出为:x=20,y=10
  注重:在函数调用里,引用调用与传址调用的效果相同,但更加简洁直观。
  
  ----------------------------------------------------------
  
  题2.分析以下程序的执行结果
  #includeiostream.h
  void main()
  {
  int a[]={10,20,30,40},*pa=a;
  int *&pb=pa;
  pb++;
  cout*paendl;
  }
  解:
  pa为数组的指针,首先指向a[0],pb是pa的引用,当执行pb++时,也使pa指向了a[1],所以输出为:20
  
  -------------------------------------------------------------
  
  题3.分析以下程序的执行结果
  #includeiostream.h
  class Sample
  {
  int x;
  public:
  Sample(){};
  Sample(int a){x=a;}
  Sample(Sample &a){x=a.x++ +10;}
  void disp(){cout"x="xendl;}
  };
  void main()
  {
  Sample s1(2),s2(s1);
  s1.disp();
  s2.disp();
  }
  解:
  Sample类的Sample(Sample &a)构造函数是一个拷贝构造函数,将a对象的x增1然后加上10后赋给当前对象的x,由于a是引用对象,所以输出为:
  x=3 // ++运算的结果
  x=12 // 2+10
  
  --------------------------------------------------------------
  
  题4.分析以下程序的执行结果
  #includeiostream.h
  class Sample
  {
  int x,y;
  public:
  Sample(){x=y=0;}
  Sample(int i,int j){x=i;y=j;}
  void copy(Sample &s);
  void setxy(int i,int j){x=i;y=j;}
  void print(){cout"x="x",y="yendl;}
  };
  void Sample::copy(Sample &s)
  {
  x=s.x;y=s.y;
  }
  void func(Sample s1,Sample &s2)
  {
  s1.setxy(10,20);
  s2.setxy(30,40);
  }
  void main()
  {
  Sample p(1,2),q;
  q.copy(p);
  func(p,q);
  p.print();
  q.print();
  }
  解:
  本题说明对象引用作为函数参数的作用。Sample类中的copy()成员函数进行对象拷贝。在main()中先建立对象p和q,p与q对象的x,y值相同,调用func()函数,由于第2个参数为引用类型,故实参发生改变;而第1个参数不是引用类型,实参不发生改变。所以输出为:
  x=1,y=2
  x=30,y=40
  
  -------------------------------------------------------
  
  题5.设计一个Book类,包含图书的书名、作者、月销售量等数据成员,其中书名和作者采用字符型指针,另有两个构造函数、一个析构函数和两个成员函数setbook()和print(),其中setbook()用于设置数据,print()用于输出数据,其说明如下:
  void print(ostream& output)
  即引用输出流。
  解:
  依题意,本题程序如下:
  #includeiostream.h
  #includestring.h
  class Book
  {
  char *title; // 书名
  char *author; // 作者
  int numsold; // 月销售量
  public:
  Book(){}
  Book(const char *str1,const char *str2,const int num)
  {
  int len=strlen(str1);
  title=new char[len+1];
  strcpy(title,str1);
  len=strlen(str2);
  author=new char[len+1];
  strcpy(author,str2);
  numsold=num;
  }
  void setbook(const char *str1,const char *str2,const int num)
  {
  int len=strlen(str1);
  title=new char[len+1];
  strcpy(title,str1);
  len=strlen(str2);
  author=new char[len+1];
  strcpy(author,str2);
  numsold=num;
  }
  ~Book()
  {
  delete title;
  delete author;
  }
  void print(ostream& output) // 输出流引用作为参数
  {
  output"输出数据"endl;
  output" 书名:"titleendl;
  output" 作者:"authorendl;
  output" 月销售量:"numsoldendl;
  }
  };
  void main()
  {
  Book obj1("C语言程序设计","谭浩强",800),obj2;
  obj1.print(cout);
  obj2.setbook("C++语言程序设计","李春葆",300);
  obj2.print(cout);
  }
  本程序的执行结果如下:
  输出数据
  书名:C语言程序设计
  作者:谭浩强
  月销售量:800
  输出数据
  书名:C++语言程序设计
  作者:李春葆
  月销售量:300
  
   题6.阅读下面的程序与输出结果,添加一个拷贝构造函数来完善整个程序
  #includeiostream.h
  class Cat
  {
  public:
  Cat();
  Cat(const Cat &);
  ~Cat();
  int getage()const{return *itsage;}
  void setage(int age){*itsage=age;}
  protected:
  int *itsage;
  };
  Cat::Cat()
  {
  itsage=new int;
  *itsage=5;
  }
  Cat::~Cat()
  {
  delete itsage;
  itsage=0;
  }
  void main()
  {
  Cat frisky;
  cout"frisky's age:"frisky.getage()endl;
  cout"setting frisky to 6...";
  frisky.setage(6);
  cout"creating boots from frisky";
  Cat boots(frisky);
  cout"frisky's age:"frisky.getage()endl;
  cout"boots'age:"boots.getage()endl;
  cout"setting frisky to 7...";
  frisky.setage(7);
  cout"frisky's age:"frisky.getage()endl;
  cout"boots'age:"boots.getage()endl;
  }
  
  当添加了拷贝构造函数后,程序的运行结果为:
  frisky's age:5
  setting frisky to 6...
  creating boots from frisky
  frisky's age:6
  boots'age:6
  setting frisky to 7...
  frisky's age:7
  boots'age:6
  
  解:
  添加的拷贝构造函数如下:
  Cat::Cat(const Cat& c)
  {
  itsage=new int;
  *itsage=*c.itsage;
  }
  
  -----------------------------------------------
  
  题7.设计一个类Sample,有一个私有数据成员,建立该类的四个对象s1(n=10)、s2(n=20)、s3(n=30)、和s4(n=40),建立一个成员函数实现这些对象n值的累加。
  解:
  依题意,建立一个成员函数add(),其参数为Sample对象引用,用于累加对象n值。
  程序如下:
  #includeiostream.h
  class Sample
  {
  int n;
  public:
  Sample(){}
  Sample (int i){n=i;}
  void add(Sample &s) // 对象引用作为参数
  {
  if(&s==this) // 不能自己相加,this是当前对象的指针
  cout"自己不能相加"endl;
  else n+=s.n;
  }
  void disp(){ coutendl" n="nendl;}
  };
  void main()
  {
  Sample s1(10),s2(20),s3(30),s4(40);
  s1.add(s2);
  s1.add(s3);
  s1.add(s4);
  s1.disp();
  coutendl;
  }
  
  本程序的执行结果如下:
  n=100
  
  ---------------------------------------------------
  
  题8.编写一个程序,设计一个点类Point,求两个点之间的距离。
  解:
  设计一个普通函数distance(Point &p1,Point &p2),用于计算p1和p2点之间的距离。
  本题程序如下:
  #includeiostream.h
  #includemath.h
  class Point
  {
  int x,y;
  public:
  Point(int i,int j){x=i;y=j;}
  int getx(){ return x;}
  int gety(){ return y;}
  void disp()
  {
  cout"("x"'"y")";
  }
  };
  float distance(Point &p1,Point &p2) // 对象引用作为参数
  {
  float d;
  d=sqrt((p1.getx()-p2.getx())*(p1.getx()-p2.getx())+
  (p1.gety()-p2.gety())*(p1.gety()-p2.gety()));
  return d;
  }
  void main()
  {
  Point p1(2,2),p2(5,5);
  p1.disp(); cout"与"; p2.disp();
  cout"之间距离="distance(p1,p2)endl;
  }
  
  本程序执行结果如下
  (2,2) 与 (5,5) 之间距离=4.24264
  
  -----------------------------------------------------
  
  题9.编写一个程序,设计一个职工类Person,一个系有若干个职工,按职务分为系主任、室主任和职工,给出他们之间的领导关系。
  解:
  类Person有姓名、职务和指向领导的指针等私有数据,以及两个构造函数和以下成员函数:setleader()(设置当前职工的领导);getname()(获取职工姓名);getleader()(获取领导者对象指针);disp()(输出姓名和职务)。
  本题程序如下:
  #includeiostream.h
  #includestdio.h
  #includestring.h
  class Person
  {
  char name[10];
  char prof[10];
  Person *leader;
  public:
  Person(){strcpy(name,"

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

延伸阅读
在说内部连接与外部连接前,先说明一些概念。 1.声明 一个声明将一个名称引入一个作用域; 在c++中,在一个作用域中重复一个声明是合法的 以下都是声明: int foo(int,int); //函数前置声明 typedef int Int; //typedef 声明 class bar; //类前置声明 exter...
引用类型也称别名,它是个很有趣的东西。在c++ 下你可以把它看作是另外的一种指针,通过引用类型我们同样也可以间接的操作对象,引用类型主要是用在函数的形式参数上,通常我们使用它是把类对象传递给一个函数。 引用对象采用类型名加上&符号和名称的方式进行定义。例如:(int &test;),这里我们就定义了一个int类型的名为test ...
#include iostream    #include string    using namespace std;  !-- frame contents -- !-- /frame contents --   void main(int argc,char* argv[]) {      int a=10;  &nb...
本文中,将要介绍与继续相关的C++/CLI主题,并以现实生活中银行交易的三种形式:存款、取款、转账,来说明类的继续体系,且以一种新的枚举形式来实现。 枚举器 请看例1中声明的类型,它存在于其自身的源文件中,并编译为一个只包含此类型的程序集: 例1: public enum class TransactionType : ...
接口 某些时候,让不相关的类分享一组公有成员,以便产生相同的行为,是非常有用的。一个最基本的方法可能是通过一个公共的基类来定义它们,但这种方法太受局限,因为它要求这些类通过继续而互相关联,另外,它们也许还有着各自的基类,且CLI类型只支持单一类继续。 C++/CLI提供了一种方法,可利用多个类实现一组通用的功能,...

经验教程

839

收藏

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