所在的位置: C++ >> C++市场 >> c练习题类与对象基础

c练习题类与对象基础

1.建立一个phone类,包括人名与电话号码两个数据成员。存储通迅信息并输出。

要求用两种方法完成对象中数据成员的赋值:

(1)用成员函数input完成对象中数据成员的输入。

(2)用构造函数完成对象中数据成员的输入。

2.构建一个类,含有三个数据成员,分别表示盒子的三条边长;其中一个成员函数,用来计算盒子的体积。

3.构建一个类book,其中含有2个私有数据成员qu和price,建立一个有5个元素的数组对象,将qu初始化为1~5,将price初始化为qu的10倍。显示每个对象的qu*price.

4.编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。

5.在已有代码的基础上,完成编码,输出3个长方形的体积。

ClassCube{

private:

floatlength,width,height;

floatvolum;

public:

Cube();//构造函数通过输入初始化长、宽、高,并求得体积,请在类外定义

voidPrint();//输出立方体信息,请在类外定义

实验结论、问题与建议(含取得的成果)

1.程序代码:

#includeiostream

usingnamespacestd;

classphone{

  private:

    stringname;

    stringtel;

public:

    voidinput();

    phone(stringn,stringt);

    voidshow();

};

voidphone::input()

{cout"请输入姓名和电话号码"endl;

cinnametel;  

}

phone::phone(stringn,stringt)

{

  name=n;tel=t;

}

voidphone::show()

{

  cout"姓名"nameendl"电话号码"telendl;

}

intmain()

{

phonep1("shushu","");

p1.show();

p1.input();

p1.show();

  return

2.程序代码:

#includeiostream

usingnamespacestd;

classvolume

{

  private:

    intlon;

    intwide;

    intheight;

  public:

  intcal(intx,inty,intz);

};

intvolume::cal(intx,inty,intz)

{

  lon=x;wide=y;height=z;

  returnlon*wide*height;

}

intmain()

{volumev1;

cout"体积;"v1.cal(2,3,4)endl;

  return0;

}

3.程序代码:

#includeiostream

usingnamespacestd;

classbook

{

  private:

    intqu;

    intprice;

  public:

  voidshow();

  book(intn);

};

book::book(intn)

{

  qu=n;

  price=10*n;

}

voidbook::show(){

  coutqu*priceendl;

}

intmain()

{

  bookbook1[5]={book(1),book(2),book(3),book(4),book(5)};

  inti;

  for(i=0;i5;i++)

  {

  book1[i].show();  

  }

  return0;

}

4.程序代码:

#includeiostream

#include"iomanip"

usingnamespacestd;

#include"string.h"

classstudent

{

  private:

    stringe1;

    char*name;

    intstu_no;

    floatscore;

    staticinttotal;

    staticfloatsum;

    staticfloatave;

  public:

    student(conststringe,constchar*na,intno,floatsco);

    voidprint();

};

student::student(conststringe,constchar*na,intno,floatsco)

{

  e1=e;

  name=newchar[strlen(na)+1];

  strcpy(name,na);

  stu_no=no;

  score=sco;

  total++;

  sum=sum+score;

  ave=sum/total;

}

voidstudent::print()

{

  cout"第"total"个学生:"endle1setw(4)namesetw(4)stu_nosetw(4)scoreendl;

  cout"总成绩:"sumsetw(4)"平均成绩:"aveendl;

}

intstudent::total=0;

floatstudent::sum=0;

floatstudent::ave=0;

intmain(){

  students1("","张三",1,90);

  s1.print();

  students2("","李四",2,87);

  s2.print();

  students3("01","王五",3,);

  s3.print();

  return0;

}

5.程序代码:

#includeiostream

usingnamespacestd;

classCube{

private:

floatlength,width,height;

floatvolum;

public:

Cube();//构造函数通过输入初始化长、宽、高,并求得体积,请在类外定义

voidPrint();//输出立方体信息,请在类外定义

};

Cube::Cube()

{

cout"输入长方体的长宽高:"endl;

cinlengthwidthheight;

  volum=length*width*height;

}

voidCube::Print()

{

  cout"体积;"volumendl;

}

intmain()

{

  Cubec1;

  c1.Print();

  Cubec2;

  c2.Print();

  Cubec3;

  c3.Print();

  return0;

}




转载请注明:http://www.aierlanlan.com/rzdk/3554.html

  • 上一篇文章:
  •   
  • 下一篇文章: