第三四章习题答案(1)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第三章习题答案
一、填空题
1.数据成员,成员函数
2. public〔公有的〕, protected〔保护的〕,private〔私有的〕
3.public,完全公开的
4.Private,完全保密的
5.protected,类,子类,友元
6.函数原型,函数体,作用域符“::〞,直接进行定义,比拟少的
7. main,只能有一个,开始
8. 类名,参数,返回值类型,void,自动
9. ~,类名,参数,返回值,重载
10.多,一
11.构造
12.析构
13.Student(){}
14.~Student(){}
15. 3种,值传递,指针传递,引用传递
16.单向的,实参传递到形参,形参,实参
17.地址,实参对象指针,形参对象地址,双向传递
18.形参对象,实参对象,形参,实参
19. 对象,数据成员,成员函数
20.Cls(int x1){x=x1;},int GetNum(){return x;}
二、判断正误
1. ╳
2. ╳
3. ╳
4. √
5. √
6.╳
7.╳
8.╳
9.√
10.√
11.√
12.√
13.╳
14.√
15.╳
16.╳
17.╳
18.╳
19.√
20.╳
22.╳
23.╳
24.√
25.√
26.√
27.╳
28.╳
29.╳
30.╳三、选择题
1.D
2.D
3.A
4.D
5.B
6.C
7.A
8.B
9.B
10.A
11.B
12.B
13.A
14.A
15. A
16. C
17.C
18.B
19.D
20.A
21.B
22.B
23.B
24.B
25.B
26.B
27.C
28.C
29.B
30.D
31. B
32. A
33.C
34. C
35. B
四、改错题,请指出下面程序中的错误代码,并说出错误原因和改错方法。

1.int x=20,y;语句出错,不能这样初始化类的数据成员
2. 没有对class Time进行前向引用声明,应在第一行增加 class Time;
3. int x,p;语句出错,应改为:int x,y;

My(){x=0;y=0}; Print(){cout<<x<<〞〞<<y<<endl;}语句出错,应改为:My(){x=0;p=0}; Print(){cout<<x<<〞〞<<p<<endl;}
4. X=A.x; Y=A.y; 语句出错,类Loca的数据成员x,y是私有访问权限,不能这样访问。

5. Base obj1(3);语句出错,不能对未定义的类建立对象。

7. { x=aa,y=bb; }语句出错,应改为:{ x=aa;y=bb; };
8. MyClass(int aa=0,int bb,int cc=1)语句出错,应改为:MyClass(int aa=0,int bb,int cc)
9. cout<<a.x<<endl; 语句出错,因为类的对象不能访问类的私有成员。

10.clock.hour=20;语句出错,因为类的对象不能访问类的私有成员。

11. Cube(int=10,int,int=10);语句出错,应改为:Cube(int h=10,int w,int len);
12. point类的数据成员x的访问权限private错误,应改为public。

五、写出下面程序的运行结果
1.
7
拷贝构造函数被调用
7
拷贝构造函数被调用
7
拷贝构造函数被调用
3
2.
<1,1>
<2,2>
<3,3>
3.
Constructing
12
Copy constructing
Destructing
144
Destructing
4.
1
3+4I
3-5I
5.
Initalizing default
Initalizing default
00
Destructor is active
Destructor is active
6.
构造函数被调用
拷贝构造函数被调用
构造函数被调用
拷贝构造函数被调用
7.
p1:
x=3 y=5
p2:
x=-858993460 y=-858993460
p2=p1:
x=3 y=5
8.
today is 2022-3-1
9.
inside main
constructor.......
inside fun
destructor........
outside main
destructor........
10.
constructor.....
2 3
1 3
destructor......
11.
construct..........
construct..........
construct..........
3.14
28.26
78.5
destruct...........
destruct...........
destruct...........
12.
<1,2>
<3,4>
<5,6>
13.
s1 area is 9
s2 area is 25
14.
before add: x:1 ,y:2
after add: x:1 ,y:2
15.
缺省构造函数被调用。

析构函数被调用。

构造函数被调用。

析构函数被调用。

六、编程题
1. 设计一个名为Rectangle的矩形类,其属性为矩形的左上角和右下角两个点的坐标,能计算和输出矩形的周长和面积。

#include "stdafx.h"
#include "iostream"
using namespace std;
struct Point
{
int a;
int b;
};
class Rectangle
{
Point topLeft;
Point bottomRight;
public:
Rectangle(Point a,Point b);
int Area();
int SideLength();
};
int main()
{
Point m,n;
m.a=3;
m.b=4;
n.a=12;
n.b =10;
Rectangle rect(m,n);
cout<<"矩形的面积为:"<<rect.Area()<<endl;
cout<<"矩形的周长为:"<<rect.SideLength()<<endl;
return 0;
}
Rectangle::Rectangle(Point a,Point b)
{
topLeft=a;
bottomRight=b;
}
int Rectangle::Area()
{
int x=bottomRight.a-topLeft.a;
int y=bottomRight.b-topLeft.b;
return x*y ;
}
int Rectangle::SideLength()
{
int x=bottomRight.a-topLeft.a;
int y=bottomRight.b-topLeft.b;
return 2*(x+y) ;
}
2. 声明一个datatype类,能处理包含字符型、整型和浮点型三种类型的数据,给出其构造函数。

#include "stdafx.h"
class datatype
{
char x;
int y;
double z;
public:
datatype(char x1);
datatype(int y1);
datatype(double z1);
};
d atatype::datatyp
e (char x1)
{
x=x1;
}
datatype::datatype(int y1)
{
y=y1;
}
datatype::datatype (double z1):z(z1)
{
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
3. 一矩形体育场如下列图所示,现在需在其周围建一矩形过道,并在四周围安上栅栏。

栅栏价格为50/米,过道造价为240元/平方米。

过道宽为3米,体育场的长宽由键盘输入。

请编写程序计算并输出过道和栅栏的造价。

#include "stdafx.h"
#include "iostream"
using namespace std;
class Rectangle
{
double length;
double width;
public:
Rectangle(double Length=10.,double Width=5.);
double Area();
double SideLength();
};
int main()
{
int a=50,b=240;
double x,y;
cout<<"请输入矩形的长和宽:";
cin>>x>>y;
cout<<endl;
Rectangle rect1(x,y),rect2(x+3,y+3);
cout<<"栅栏的长度为:"<<rect2.SideLength()
<<",造价为:"<<rect2.SideLength()*a<<endl;
double area12;
area12=rect2.Area ()-rect1.Area ();
cout<<"过道的面积为:"<<area12 <<",造价为:"<<area12*b<<endl;
return 0;
}
Rectangle::Rectangle (double Length,double Width)
{
length=Length;
width=Width;
}
double Rectangle::Area ()
{
return length*width;
}
double Rectangle::SideLength ()
{
return 2*(length+width);
}
#include "stdafx.h"
#include "iomanip"
#include "iostream"
using namespace std;
#define MAXSIZE 5000
struct Employees
{
char name[10]; //姓名
char sex[3]; //性别
int age; //年龄
char positions[20]; //职务
char professionalTitles[20]; //职称
char jobs[20]; //岗位
float remuneration; //薪酬
};
class Employees_c
{
private:
Employees Employees_struct[MAXSIZE];
int total;
public:
Employees_c();
int Insert_seq(int i,Employees x); //插入第i员工的信息
int Delete_seq(int i); //删除第i个员工信息void Print_seq(); //打印所有员工信息};
void menu();
int main()
{
Employees_c Employees_Object;
int n;
bool m=true;
while(m)
{
menu();
cin>>n;
switch(n)
{
case 1:
{
int i;
Employees x;
cout<<"请输入插入位置:";
cin>>i;
<<"职务、职称、岗位和薪酬:"<<endl;
cin>>x.no >> >>x.sex >>x.age >>x.positions >>
x.professionalTitles >>x.jobs >>x.remuneration ;
Employees_Object.Insert_seq(i,x);
cout<<"插入后的情况:"<<endl;
Employees_Object.Print_seq();
break;
}
case 2:
{
int i;
cout<<"请输入删除位置:;
cin>>i;
Employees_Object.Delete_seq(i);
cout<<"删除后的情况:"<<endl;
Employees_Object.Print_seq();
break;
}
case 0:m=false;
}
}
return 0;
}
void menu()
{
cout<<endl;
cout<<"1.插入"<<endl;
cout<<"2.删除"<<endl;
cout<<"0.退出"<<endl;
cout<<endl;
cout<<"请选择:";
}
Employees_c::Employees_c ()
{
total=0;
}
int Employees_c::Insert_seq (int i,Employees x)
int j;
if(total==MAXSIZE)
{
cout<<"table is full"<<endl;
return -1;
}
if(i<1||i>(total+1))
{
cout<<"place is wrong!"<<endl;
return 0;
}
for(j=total-1;j>=i-1;j--)
{
Employees_struct[j+1]=Employees_struct[j];
}
Employees_struct[i-1]=x;
++total;
return 1;
}
int Employees_c::Delete_seq (int i)
{
int j;
if(i<1||i>total)
{
cout<<"this element don't exist!"<<endl;
return -1;
}
for(j=i;j<=total-1;j++)
{
Employees_struct[j-1]=Employees_struct[j];
}
--total;
return 1;
}
void Employees_c::Print_seq ()
{
int i;
for (i=0;i<=total-1;i++)
{
cout<<Employees_struct[i].no<<setw(10)<<Employees_struct[i].name
<<setw(10)<<Employees_struct[i].sex<<setw(10)<<Employees_struct[i].age
<<setw(10)<<Employees_struct[i].positions
<<setw(10)<<Employees_struct[i].professionalTitles
<<setw(10)<<Employees_struct[i].jobs
<<setw(10)<<Employees_struct[i].remuneration <<endl;
}
cout<<endl;
}
5. 设计一个复数类,要求对其构造函数进行重载。

#include "stdafx.h"
#include "iostream"
using namespace std;
{
double real,imag;
public:
};
{
real=0.;
imag=0.;
}
{
real=real1;
imag=0.;
}
{
real=real1;
imag=imag1;
}
6.下面是一个类的测试程序,设计出能使用如下测试程序的类。

int main()
{
A x;
x.initx(400,500);
x.print();
}
输出结果:500-400=100
#include "iostream"
using namespace std;
class A
{
int a,b;
public:
initx(int aa,int bb)
{
a=aa;
b=bb;
}
Print()
{
cout<<b<<〞-〞<<a<<〞=〞<<b-a<<endl;
}
};
7.定义一个日期类,实现设置日期、判断两个日期先后,以及打印日期的功能。

#include "iostream"
using namespace std;
class Date
{
int year,month,day; //年、月、日
public:
Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void Print();
};
{
if(year>d1.year)
return 1;
else
if(if year<d1.year)
return -1;
else
if(month>d1.month)
return 1;
else
if(month<d1.month)
return -1;
else
if(day>d1.day)
return 1;
else
if(day<d1.day)
return -1;
else
return 0;
}
void Date::Print()
{
cout<<year<<〞年〞<<month<<〞月〞<<day<<〞日〞;
}
8.设计一个矩形类Rect,要求有下述成员函数:
〔1〕move():从一个位置移动到另一个位置。

〔2〕size():改变矩形的大小。

〔3〕where():返回矩形右下角的坐标值。

〔4〕area():计算矩形的面积。

#include "iostream"
using namespace std;
class Rect
{
int x,y; //矩形左上角的x,y坐标值
int w,h; //矩形的宽和高
public:
void move(int aa,int bb);
void size(int width,int high);
void where(int &x1,int &y1);
int area();
};
void Rect::move(int aa,int bb)
{
x=aa;
y=bb;
}
void Rect::size(int width,int high)
{
w=width;
h=high;
}
void Rect::where(int &x1,int &y1)
{
x1=x+w;
y1=y+h;
}
int area()
{
Return w*h;
}
9. 设计一个矩形类Rect,要求如下:
〔1〕两点确定一直线,用该直线作为矩形左上角到右下角的对角线来确定一个矩形。

〔2〕初始化矩形时,判断给定的两个点是否能构成一个矩形。

假设不能构成矩形,那么矩形对角线的两点初始化为(0,0)和(1,1)。

〔3〕定义并实现成员函数IsSquare(),判断矩形是否为正方形。

〔4〕定义并实现成员函数PrintRect(),打印矩形四个点的坐标值。

#include "iostream"
#include <math>
using namespace std;
class Rect
{
int x_l,y_l; //直线的起点坐标值
int x_r,y_r; //直线的终点坐标值
public:
Rect(float m=0,float n=0,float p=1,float q=1);
bool IsSquare();
void PrintRect();
};
void Rect:: Rect(float m=0,float n=0,float p=1,float q=1)
{
if((m==p)||(n==q))
{
x_l=y_l=0;
x_r=y_r=1;
}
else
{
x_l=m;
y_l=n;
x_r=p;
y_r=q;
}
bool Rect::IsSquare()
{
if (fabs(x_l-x_r)==fabs(y_l-y_r)
return true;
else
return false;
}
void Rect::PrintRect()
{
cout <<〞第一个点的坐标:<〞<<x_l<<〞,〞<<y_l<<〞>〞<<endl;
cout <<〞第二个点的坐标:<〞<<x_l<<〞,〞<<y_r<<〞>〞<<endl;
cout <<〞第一个点的坐标:<〞<<x_r<<〞,〞<<y_l<<〞>〞<<endl;
cout <<〞第一个点的坐标:<〞<<x_r<<〞,〞<<y_r<<〞>〞<<endl;
}
10.设计一个日期类Date,要求其满足:
〔1〕有一个无参数的构造函数,其初始的日期为:2022/12/25。

〔2〕有一个有参数的构造函数,其参数分别对应年、月、日。

〔3〕实现日期设置的成员函数。

〔4〕实现日期获取的成员函数。

#include "iostream"
using namespace std;
class Date
{
int year,month,day; //年、月、日
public:
Date();
Date(int y,int m,int d);
void SetDate(int y,int m,int d);
void GetDate(int &y,int &m,int &d);
};
Date::Date()
{
year=2022;
month=12;
day=25;
}
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void Date::SetDate(int y,int m,int d)
{
year=y;
month =m;
day=d;
}
void Date::GetDate(int &y,int &m,int &d)
{
y=year;
m =month;
d=day;
}
11.定义有理数类Rational,该类存放分数形式的有理数。

要求为:
〔1〕该类具有默认参数的构造函数,默认值为1。

〔2〕定义变量x和y,分别存放分子和分母,同时分数要以最简形式存放。

例如:分数2/4应简化为1/2。

〔3〕定义成员函数Add、Sub、Mul和Div。

计算结果仍以最简形式存放。

〔4〕以x/y的形式打印分数。

〔5〕以浮点数形式打印分数。

#include "iostream"
using namespace std;
class Rational
{
int x,y;
public:
Rational(int a=1,int b=1);
Rational Add(Rational &r);
Rational Sub(Rational &r);
Rational Mul(Rational &r);
Rational Div(Rational &r);
void Print();
void PrintFloat();
int Divisor(int a,int b); //求最大公约数
};
Rational::Rational(int a,int b)
{
int c;
c=Divisor(a,b);
x=a/c;
y=b/c;
}
Rational::Add(Rational &r)
{
int a,b,c;
a=x*r.y+y*r.x;
b=y*r.y;
Divisor(a,b);
return Rational(a/c,b/c);
}
Rational::Sub(Rational &r)
{
int a,b,c;
a=x*r.y-y*r.x;
b=y*r.y;
Divisor(a,b);
return Rational(a/c,b/c);
}
Rational::Mul(Rational &r)
{
int a,b,c;
a=x*r.x;
b=y*r.y;
Divisor(a,b);
return Rational(a/c,b/c);
}
Rational::Div(Rational &r)
{
int a,b,c;
a=x*r.y;
b=y*r.x;
Divisor(a,b);
return Rational(a/c,b/c);
}
Rational::Divisor(int a,int b)
{
int t,c;
if (a<b)
{
t=a;
a=b;
b=t;
}
do {
c=a%b;
a=b;
b=c;
} while (c!=0);
return a;
}
void Rational::Print()
{
cout<<〞x/y=〞<<x<<〞/〞<<y<<endl;
}
void Rational::PrintFloat()
{
cout<<〞x/y=〞<<double(x)/y<<endl;
}
〔1〕该类有三个构造函数:第一个无参数,第二个只有一个参数,第三个有两个参数。

〔2〕定义成员函数Add、Sub,分别进行两个复数的加、减运算。

〔3〕定义成员函数Print输出复数。

#include "stdafx.h"
#include "iostream"
using namespace std;
{
private:
double real;
double imag;
public:
void print ();
};
{
real=r;
imag=i;
}
{
real=r;
imag=0;
}
{
real=0;
imag=0;
}
{
temp.real =real +a.real ;
temp.imag =imag +a.imag ;
return temp;
}
{
temp.real =real -a.real ;
temp.imag =imag -a.imag ;
return temp;
}
{
cout<<real;
if(imag>0)
cout<<"+";
if(imag!=0)
cout<<imag<<"i"<<endl;
}
13.定义点类Point,〔1〕用构造函数初始化Point的对象;〔2〕定义函数Dist,计算平面上两点之间的距离。

#include "stdafx.h"
#include "iostream"
*include "math"
using namespace std;
class Point
{
private:
double x,y;
public:
Point(double x1,double y1)
{
x=x1;
y=y1;
}
void Dist(double a,double b);
};
void Point::dist(double a,double b)
{
return sqrt((x-a)*(x-a)+(y-b)*(y-b));
}
第四章习题
一、填空题
1. 特殊的,static
2. 构造函数,体内,类外,对象生成。

3. 类,对象,类名,域运算符〞::〞
4.所有对象,类对象,产生类对象
5.作用域运算符,类,对象
6.类内,类外。

static
7. 该类,friend
8. 所有〔或私有〕,友元函数,友元类
9.成员函数,private,public,protected
10.执行效率
11.隐蔽性
12.函数,块,类,文件
13. 局部对象,静态对象,全局对象,动态对象
14.程序块,函数体内,函数体或程序块,生存期结束15.static,定义该对象时,整个程序结束时
16.函数体外,整个程序结束时
17.new,delete
18. 值,修改
19.指针,指向常量的指针,常量指针
20.const,函数定义中
21.数据成员,没有用const修饰的,常量
22.非常量成员,常量成员。

23.初始化列表进行
24.friend void A::f()
25.const int类型,不能被改变,地址
26.Friend,Cls,Cls
二、判断正误
1. ╳
2.╳
3.╳
4. √
5. ╳
6.╳
7.╳
8.√
9.╳
10.╳
11.╳
12.√
13.╳
14.╳
15.╳
16.√
17.√
18.╳
19.√
20.╳
21.√
22.√
23.╳
24.╳
25.√
三、选择题
1.D
2.A
3.A
4.A
5.B
6.B
7.B
8.D
9.D
10.D
11.C
12.D
13.A
14.B
15.A
16.A
17.D
18.B
19.B
20.B
四、改错题,请指出下面程序中的错误代码,并说出错误原因和改错方法。

1.cout<<a<<endl;语句出错,类的静态成员函数不能访问类的非静态成员。

2. int p::x=1; 语句出错,类的非静态数据成员不能在类外初始化。

3. cout<<a.getValue1 ()<<endl; 语句出错,常量对象不能访问类非常量成员函数。

4.语句 int Student::GetNo() 和语句const char* Student::GetName()错误,静态成员函数中的const关键字是语句的一局部,在定义时也必须包含,这两条语句应改为:int Student::GetNo() const和语句const char* Student::GetName() const
5. 语句Point B(A);和语句fun1(A);错误,原因:Point类的拷贝构造函数的访问属性为private,而上述两个语句在对象生成和以对象作为参数进行值传递时,都要调用拷贝构造函数。


语句Point(const Point& p);不应该是private访问权限,而应是public访问权限。

五、写出下面程序的运行结果
1.
1004
2.
1
()
(1,2,3,4,5,6,7,8)
1
(11,12,13,14,15,16,17,18,19)
(19,18,17,16,15,14,13,12,11)
3.
4.4+6.6i
4.
2004-10-18 14:20:25
5.
1.2+3.4i
(1.2,3.4)
6.
Student:number=1
after call f():number=1
~Student:number=0
~Student:number=-1
7.
normal construction:1
copy construction:2
copy construction:3
inside fun1():3
~Point:2
~Point:1
~Point:0
六、编程题
1.〔1〕建立一个类,该类具有const和非const成员函数。

〔2〕创立这个类的const和非const 对象,并用不同类型的对象调用不同类型的成员函数。

#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
void point();
void output() const;
};
int _tmain(intargc, _TCHAR* argv[])
{
A const a1;
A a2;
a1.output();
a2.output ();
a2.point ();
return 0;
}
void A::point ()
{
cout<<"point"<<endl;
}
void A::output () const
{
cout<<"output"<<endl;
}
2.编写一个类,统计目前存在多少个该类的对象。

#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
static int total;
public:
A()
{
total++;
cout<<"当前类的对象总数为:"<<total<<endl;
}
~A()
{
total--;
cout<<"当前类的对象总数为"<<total<<endl;
}
};
void f()
{
A aa,bb,cc;
}
int A::total=0;
int _tmain(intargc, _TCHAR* argv[])
A a1,a2,a3;
f();
A b1,b2,b3;
return 0;
}
3.编写一个学生类,学生信息包括姓名、学号、年龄、性别和成绩;统计学生的总人数及总成绩,并输出。

#include "stdafx.h"
#include <iostream>
using namespace std;
class Student
{
int no;
char name[10];
char sex[3];
int age;
double score;
static inttotalNumber;
static double totalScore;
public:
Student(intno_,char *name_,char *sex_,intage_,double score_);
static void Output();
void StudentInformation();
};
int Student::totalNumber=0;
double Student::totalScore =0;
int _tmain(intargc, _TCHAR* argv[])
{
Student stu1(1001,"张三","男",18,97.5);
stu1.StudentInformation ();
Student stu2(1002,"李四","女",19,83.);
stu2.StudentInformation ();
Student stu3(1003,"王五","男",17,93.);
stu3.StudentInformation ();
Student stu4(1004,"郭六","女",20,62.5);
stu4.StudentInformation ();
Student stu5(1005,"任七","男",18,77.);
stu5.StudentInformation ();
Student::Output ();
return 0;
}
Student::Student (intno_,char *name_,char *sex_,intage_,double score_)
{
no=no_;
strcpy(name,name_);
strcpy(sex,sex_);
age=age_;
score=score_;
totalNumber++;
totalScore+=score;
}
void Student::StudentInformation ()
cout<<"学号:"<<no<<" "<<"姓名:"<<name<<" "<<"性别:"
<<sex<<" "<<"年龄:"<<age<<" "<<"成绩:"<<score<<endl;
}
void Student::Output ()
{
cout<<"学生总数:"<<totalNumber<<" "<<"总成绩:"
<<totalScore<<" "<<"平均成绩:"<<totalScore/totalNumber<<endl;
}
4.编写一个学生类,〔1〕输出每个学生的姓名、学号、成绩;〔2〕统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。

#include "stdafx.h"
#include <iostream>
using namespace std;
class Student
{
int no;
char name[10];
double score;
static inttotalNumber;
static double totalScore;
static double lowestScore;
static double highestScore;
public:
Student(intno_,char *name_,double score_);
static void Output();
void StudentInformation();
};
int Student::totalNumber=0;
double Student::totalScore =0;
double Student::highestScore =0.;
double Student::lowestScore =100.;
int _tmain(intargc, _TCHAR* argv[])
{
Student stu1(1001,"张三",97.5);
stu1.StudentInformation ();
Student stu2(1002,"李四",83.);
stu2.StudentInformation ();
Student stu3(1003,"王五",93.);
stu3.StudentInformation ();
Student stu4(1004,"郭六",62.5);
stu4.StudentInformation ();
Student stu5(1005,"任七",77.);
stu5.StudentInformation ();
Student::Output ();
return 0;
}
Student::Student (intno_,char *name_,double score_)
{
no=no_;
strcpy(name,name_);
score=score_;
totalNumber++;
totalScore+=score;
if (score>highestScore)
highestScore=score;
if (score<lowestScore)
lowestScore=score;
}
void Student::StudentInformation ()
{
cout<<"学号:"<<no<<" "<<"姓名:"<<name<<" "
<<"成绩:"<<score<<endl;
}
void Student::Output ()
{
cout<<"学生总数:"<<totalNumber<<" "<<"总成绩:"
<<totalScore<<" "<<endl;
cout<<"平均成绩:"<<totalScore/totalNumber<<" "
<<"最高成绩"<<highestScore<<" "
<<"最低成绩"<<lowestScore<<" "<<endl;
}
5.定义复数类和二维向量类,复数具有实部和虚部,二维向量有两个向量。

利用友元类将复数转换为二维向量。

#include "stdafx.h"
#include <iostream>
using namespace std;
{
double real;
double imag;
public:
friend class Vector;
};
class Vector{
double x,y;
public:
};
int main()
{
Vector v;
v.Change (c);
v.Print (c);
return 0;
}
{
real=r;
imag=i;
}
{
x=c.real;
y=c.imag;
}
{
//输出复数
cout<<"复数:";
cout<<c.real;
if(c.imag>0)
cout<<"+";
cout<<c.imag<<"i"<<endl;
//输出二维向量
cout<<"二维向量:";
cout<<"("<<x<<","<<y<<")"<<endl;
}
6. 有Distance类和Point类,将Distance类定义为Point类的友元类来实现计算两点之间距离。

#include "stdafx.h"
#include"iostream"
#include"math.h"
using namespace std;
class Distance;
class Point
{
int X,Y;
public:
Point(int xx=0,int yy=0)
{
X=xx;
Y=yy;
}
friend class Distance;
};
class Distance
{
public:
float Dist(Point &a,Point &b);
};
int main()
{
Point myp1(1,1),myp2(4,5);
Distance d;
cout<<"The distance is: ";
cout<<d.Dist(myp1,myp2)<<endl;
return 0;
}
float Distance::Dist(Point &a,Point &b)
{
double x,y;
x=a.X -b.X ;
y=a.Y -b.Y ;
return float(sqrt(x*x+y*y));
}
7. 利用静态数据成员的概念,编写一个类,统计目前存在多少个该类的对象。

#include "stdafx.h"
#include"iostream"
using namespace std;
class MyClass
{
static int n;
public:
MyClass()
{
n++;
print();
}
~MyClass()
{
n--;
print();
}
void Print()
{
cout<<n<<endl;
}
};
void f()
{
MyClass b;
}
int MyClass::n=0;
int main()
{
MyClass a1;
a1.Print();
f();
MyClass a[5],a2;
a2.Print ();
return 0;
}
8. 利用静态的概念,编写一个小狗类,统计并输出每个小狗的重量、小狗的总数量及总重量。

#include "stdafx.h"
#include"iostream"
using namespace std;
class SmallDog
{
double weight;
static double total_weight;
static int total_number;
public:
SmallDog(double w):weight(w)
{
total_weight=total_weight+weight;
total_number++;
}
void Print()
{
cout<<total_weight<<" "<<total_number<<endl;
}
};
double SmallDog::total_number =0;
double SmallDog::total_weight =0;
int main( )
{
SmallDog dog1(2.2),dog2(3.4),dog3(4.5);
dog1.Print ();
dog2.Print ();
dog3.Print ();
return 0;
}
9.建立一个类,该类具有const和非const成员函数。

建立这个类的const和非const 对象,试着为不同类型的对象调用不同类型的成员函数。

#include "stdafx.h"
#include"iostream"
using namespace std;
class Student
{
int No;
char Name[20];
public:
Student(int a,char *b);
int GetNo()const;
char* GetName();
};
Student::Student (int a,char *b)
{
No=a;
strcpy(Name,b);
}
int Student::GetNo()const
{
return No;
}
char* Student::GetName()
{
return Name;
}
int main()
{
const Student s1(101,〞wang〞);
Student s2(102,〞li〞);
s1.GetNo();
s1.GetName(); //错误,常量对象不能调用类的非常量成员函数
s2.GetNo();
s2.GetName();
}。

相关文档
最新文档