4、调试和运行参考程序5,并观察输出结果,体会和理解内存动态分配的概念与new、delete运算符的基本使用方法。
参考程序5
#include
main()
{
int*p;
//在画线处填上语句,用new关键字动态分配一个int型存储区,并将首地址赋给p
_____________________p=newint;
if(!
p)
{
cout<<"allocationfailure\n";
return1;
}
//在下画线处填上语句,将p所指向的存储区赋值20
________________*p=20
cout<<*p;
//在下画线处填上语句,用于撤销指针p,释放p指向的存储空间
________________delete[]p;
return0;
}
(二)程序设计题
1、输出1到100的偶数,一行5个。
#include
main()
{
inti,j;
for(i=1;i<=100;i++)
{
if(i%2==0)
{
cout<
j++;
if(j%5==0)
{
cout<}
}
}
}
2、打印一个三角形的1~9的乘法表。
#include
voidmain()
{
inti,j;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
cout<cout<}
}
3、编程求矩阵的加法:
。
#include
intmain()
{
inti,j;
inta[3][3]={5,7,8,2,-2,4,1,1,1},
b[3][3]={4,-2,3,3,9,4,8,-1,2},
c[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<}
cout<}
return0;
}
4、编写一个程序,用来分别求2个整数、3个整数、2个双精度数和3个双精度数的最大值。
要求使用重载函数来完成。
#include
#include
intmax(inta,intb)
{
if(a>=b)
returna;
else
returnb;
}
intmax(inta,intb,intc)
{
if(a>=b)
{
if(a>c)
returna;
else
returnc;
}
else
{
if(b>c)
returnb;
else
returnc;
}
}
doublemax(doubled,doublee)
{
if(d>=e)
returnd;
else
returne;
}
doublemax(doubled,doublee,doublef)
{
if(d>=e)
{
if(d>f)
returnd;
else
returnf;
}
else
{
if(e>f)
returne;
else
returnf;
}
}
main()
{
inta,b,c,d,e;
floatf,g,h,i,j;
cout<<"请输入2个整数:
"<cin>>a>>b;
cout<<"2个整数中最大的为:
"<cout<<"请输入3个整数:
"<cin>>c>>d>>e;
cout<<"这3个整数中最大的为:
"<cout<<"请输入2个实数:
"<cin>>f>>g;
cout<<"这2个实数中最大的为:
"<cout<<"请输入3个实数:
"<cin>>h>>i>>j;
cout<<"这3个实数中最大的为:
"<return0;
}
实验二类与对象
一、实验目的
1、学习类与对象的定义,掌握类与对象的使用方法。
2、学习数据成员与成员函数的访问方式,理解构造函数和析构函数的定义与执行过程,学会构造函数的重载方法。
3、掌握数组与指针的定义与使用方法,理解数组与指针的存储分配与表示。
4、掌握用指针和引用向函数传递参数。
5、掌握静态数据成员和静态成员函数的使用。
6、理解友元与友元函数的作用与使用方法。
二、实验内容
1、下面是一个计算器类的定义,请完成该类成员函数的实现。
classCounter
{
public:
Counter(intnumber);
voidincrement();//给原值加1
voiddecrement();//给原值减1
intgetValue();//取得计数器值
intprint();//显示计数
private:
intvalue;
};
程序:
#include
using namespace std;
class Counter
{
int value;
public:
void getvalue();
int increment();
int decrement();
void print();
};
void Counter:
:
getvalue() //取得计数器值
{
cin>>value;
}
int Counter:
:
increment() //给原值加1
{
++value;
return value;
}
int Counter:
:
decrement( ) //给原值减1
{
value=value-2;
return value;
}
inline void Counter:
:
print() //显示计数
{
cout<}
int main()
{
Counter c;
cout<<"原数为:
";
c.getvalue();
c.increment();
cout<<"加一后为:
";
c.print();
c.decrement();
cout<<"减一后为:
";
c.print();
return 0;
}
4、声明一个Student,在该类中包括一个数据成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全班成绩的平均值。
在main()函数中,输入某班学生的成绩,并调用上述函数求出全班学生的成绩之和和平均分。
源程序:
#include
classStudent
{
private:
doublescore;
staticdoubletotal;
staticintcount;
public:
Student(doubles)
{
score=s;
total+=s;
count++;
}
staticdoublesum()
{
returntotal;
}
staticdoubleaverage()
{
returntotal/count;
}
};
doubleStudent:
:
total=0.0;
intStudent:
:
count=0;
voidmain()
{inti,n;
doublesco;
cout<<"Inputthenumberofstudent";
cin>>n;
for(i=0;i{
cout<<"Inputthescore:
";
cin>>sco;
Studentobj(sco);
}
cout<<"Thetotalscoreis:
"<:
sum()<cout<<"Theaveragescoreis:
"<:
average()<}
运行结果:
2、构建一个购书清单类booklist,其中含有私有数据成员bookname(书名)、price(单价)、sum(总价),建立一个有5个元素的对象数组,接受5个以内图书信息,并输出图书信息和总价格。
3、参考课本例子,建立一个源程序文件,在此文件中建立一个新的类,将新建的类命名为Rect。
classRect
{
public:
intArea_int();
doubleArea_double();
Rect(doublelength,doublewidth);
Rect(intlength,intwidth);
virtual~Rect();
private:
intnLength;
intnWidth;
doubledLength;
doubledWidth;
};
源程序:
# include
using namespace std;
class Rect
{
public:
int Area_int();
double Area_double();
Rect(double length, double width);
Rect(int length,int width);
~Rect(){
cout<< "I am theconstructor!
"< }
Rect(){
cout<< "I am the constructor!
"< }
private:
int nLength;
int nWidth;
double dLength;
double dWidth;
};
Rect:
:
Rect(int length,int width)
{
nLength=length;
nWidth=width;
}
Rect:
:
Rect(double length,double width)
{
dLength=length;
dWidth=width;
}
int Rect:
:
Area_int()
{
return nLength*nWidth;
}
double Rect:
:
Area_double()
{
return dLength*dWidth;
}
void main()
{
Rect I(30,50);
Rect *pt;
pt=new Rect(10,300);
cout<<"Area_int()函数计算长方形面积是:
"< cout<<"Area_double函数计算长方形面积并用指针分配空间的是:
"<Area_int()<}
实验结果
5、设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果:
A(x1,y1),B(x2,y2)
Distance=d
其中:
x1、y1、x2、y2为指定的坐标值,d为两个坐标点之间的距离。
源程序:
#include"iostream.h"
#include"math.h"
classLocation{
private:
intx,y;
public:
Location(inti,intj):
x(i),y(j){}
intGetx(){returnx;}
intGety(){returny;}
doubledistance(Locationb);
frienddoubledistance(Location&a,Location&b);
};
doubledistance(Location&a,Location&b)//友元函数
{intdx=a.x-b.x;
intdy=a.y-b.y;
returnsqrt(dx*dx+dy*dy);}
doubleLocation:
:
distance(Locationb)//成员函数
{intdx=x-b.x;
intdy=y-b.y;
returnsqrt(dx*dx+dy*dy);}
voidmain()
{
LocationA(-11,-18),B(-22,50);cout<<"A("<doubled=A.distance(B);//调用成员函数
cout<<"Distance1="<cout<<"Distance2="<运行结果:
实验三派生类与继承
一、实验目的
1、学习类的继承,能够定义和使用类的继承关系。
2、学习派生类的声明与定义方法。
3、掌握类的定义和对象的声明。
4、熟悉公有派生和私有派生的访问特性。
5、掌握派生类构造函数和析构函数的执行顺序。
6、掌握利用访问声明调整基类成员在派生类中的访问属性。
二、试验内容
1、已有类Time和Date,要求设计一个派生类Birthtime,它继承类Time和Date,并且增加一个数据成员Childname用于表示小孩的名字,同事设计主程序显示一个小孩的出生时间和名字。
#include
#include
usingnamespacestd;
classTime
{
public:
Time(inth,intm,ints)
{
hours=h;
minutes=m;
seconds=s;
}
voiddisplay()
{
cout<<"出生时间:
"<}
protected:
inthours,minutes,seconds;
};
classDate
{
public:
Date(inty,intmon,intd)
{
years=y;
months=mon;
days=d;
}
voiddisplay()
{
cout<<"出生年月:
"<}
protected:
intyears,months,days;
};
classBirthtime:
publicDate,publicTime
{
public:
Birthtime(stringcn,intyy,intmm,intdd,inthh,intmint,intss):
Date(yy,mm,dd),Time(hh,mint,ss){
//strcpy(childname,cn);
childname=cn;
}
voiddisplay()
{
cout<<"姓名:
"<Date:
:
display();
Time:
:
display();
}
private:
stringchildname;
};
intmain()
{
Birthtimeys("王丽",2009,12,1,22,12,14);
ys.display();
return0;
}
2、编写一个学生和教师的数据输入和显示程序。
学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。
要求将编号、姓名、性别、年龄的输入和显示设计成一个类Person,并作为学生类Student和教师类Teacher的基类。
#include
usingnamespacestd;
classperson
{
public:
voidinput()
{
cout<<"编号:
";
cin>>number;
cout<cout<<"姓名:
";
cin>>childname;
cout<}
voidprint()
{
cout<<"编号:
"<cout<<"姓名:
"<}
private:
intnumber;
charchildname[10];
};
classstudent:
publicperson
{
public:
voidinput()
{
person:
:
input();
cout<<"班号:
";
cin>>depart;
cout<cout<<"成绩:
";
cin>>grade;
cout<}
voidprint()
{
person:
:
print();
cout<<"班号:
"<cout<<"成绩:
"<}
private:
intgrade;
chardepart[10];
};
classteacher:
publicperson
{
public:
voidinput()
{
person:
:
input();
cout<<"职称:
";
cin>>prof;
cout<cout<<"部门:
";
cin>>depart;
cout<}
voidprint()
{
person:
:
print();
cout