return0;
}
4、调试和运行参考程序5,并观察输出结果,体会和理解内存动态分配的概念与new、delete运算符的基本使用方法。
参考程序5
#include
main()
{
int*p;
//在画线处填上语句,用new关键字动态分配一个int型存储区,并将首地址赋给p
_____________________
if(!
p)
{
cout<<"allocationfailure\n";
return1;
}
//在下画线处填上语句,将p所指向的存储区赋值20
________________
cout<<*p;
//在下画线处填上语句,用于撤销指针p,释放p指向的存储空间
________________
return0;
}
源程序:
#include
main()
{
int*p;
p=newint;
if(!
p)
{
cout<<"allocationfailure\n";
return1;
}
*p=20;
cout<<*p<deletep;
return0;
}
(二)程序设计题
1、输出1到100的偶数,一行5个。
源程序:
#include
intmain()
{
inti,j=0;
for(i=1;i<=100;i++)
{
if(i%2==0)
{
cout<
j++;
if(j%5==0)
cout<<"\n";
}
}
cout<}
2、打印一个三角形的1~9的乘法表。
源程序:
#include
voidmain()
{
inti,j,s;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{
s=i*j;
cout<
}
cout<<"\n";
}
}
3、编程求矩阵的加法:
。
源程序:
#include
voidmain()
{
inta[3][3]={5,7,8,2,-2,4,1,1,1};
intb[3][3]={4,-2,3,3,9,4,8,-1,2};
intc[3][3];
inti,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<}
cout<}
}
4、编写一个程序,用来分别求2个整数、3个整数、2个双精度数和3个双精度数的最大值。
要求使用重载函数来完成。
源程序:
#include
usingnamespacestd;
voidmax(intx,inty)
{
if(x>y)
cout<<"maxis"<else
cout<<"maxis"<}
voidmax(intx,inty,intz)
{
intmax;
max=x>y?
x:
y;
max=max>z?
max:
z;
cout<<"maxis"<}
voidmax(doublex,doubley)
{
if(x>y)
cout<<"maxis"<else
cout<<"maxis"<}
voidmax(doublex,doubley,doublez)
{
doublemax;
max=x>y?
x:
y;
max=max>z?
max:
z;
cout<<"maxis"<}
intmain()
{
max(3,8);
max(7,8,19);
max(3.4,4.5);
max(9.9,8.56,5.34);
return0;
}
实验二类与对象
一、实验目的
1、学习类与对象的定义,掌握类与对象的使用方法。
2、学习数据成员与成员函数的访问方式,理解构造函数和析构函数的定义与执行过程,学会构造函数的重载方法。
3、掌握数组与指针的定义与使用方法,理解数组与指针的存储分配与表示。
4、掌握用指针和引用向函数传递参数。
5、掌握静态数据成员和静态成员函数的使用。
6、理解友元与友元函数的作用与使用方法。
二、实验内容
1、下面是一个计算器类的定义,请完成该类成员函数的实现。
classCounter
{
public:
Counter(intnumber);
voidincrement();//给原值加1
voiddecrement();//给原值减1
intgetValue();//取得计数器值
intprint();//显示计数
private:
intvalue;
};
源程序:
#include
usingnamespacestd;
classCounter
{
public:
Counter(intnumber);
voidincrement();//给原值加1
voiddecrement();//给原值减1
intgetValue();//取得计数器值
intprint();//显示计数
private:
intvalue;
};
Counter:
:
Counter(intnumber)
{
value=number;
}
voidCounter:
:
increment()
{
value=value+1;
}
voidCounter:
:
decrement()
{
value=value-1;
}
intCounter:
:
getValue()
{
returnvalue;
}
intCounter:
:
print()
{
cout<<"所得数值:
"<return0;
}
intmain()
{
cout<<"请输入所需计算的整数:
"<inta,b;
cin>>a;
CounterA(a);
cout<<"请输入1选择加1,输入2选择减1"<cin>>b;
if(b==1)
{
A.increment();
A.print();
}
if(b==2)
{
A.decrement();
A.print();
}
return0;
}
2、构建一个购书清单类booklist,其中含有私有数据成员bookname(书名)、price(单价)、sum(总价),建立一个有5个元素的对象数组,接受5个以内图书信息,并输出图书信息和总价格。
源程序:
#include
usingnamespacestd;
classbooklist
{
charbookname[20];
doubleprice;
staticdoublesum;
public:
booklist(char*bn=0,doublebp=0)
{
if(bn!
=0)
{
strcpy(bookname,bn);
price=bp;
sum+=price;
}
}
voidinput()
{
cout<<"bookname:
";
cin>>bookname;
cout<<"price:
";
cin>>price;
sum+=price;
}
voidoutput()
{
cout<<"bookname:
"<"<}
staticvoidout_sum()
{
cout<<"sum="<}
};
doublebooklist:
:
sum=0;
intmain()
{
inti;
booklistbs[5];
for(i=0;i<5;i++)
bs[i].input();
for(i=0;i<5;i++)
bs[i].output();
booklist:
:
out_sum();
return0;
}
3、参考课本例子,建立一个源程序文件,在此文件中建立一个新的类,将新建的类命名为Rect。
classRect
{
public:
intArea_int();
doubleArea_double();
Rect(doublelength,doublewidth);
Rect(intlength,intwidth);
virtual~Rect();
private:
intnLength;
intnWidth;
doubledLength;
doubledWidth;
};
【要求】
(1)向Rect类中添加数据成员及成员函数,并完善成员函数的功能。
如设计一个Area_int()函数,计算边长为整型的长方形的面积;设计一个Area_double()函数,计算边长为double型的长方形的面积。
(2)重载构造函数。
一种构造函数用整型变量记录长方形的长和宽,另一种构造函数用double型记录。
(3)体现对象的构造和析构过程。
例如,在构造函数中用cout<<”Iamtheconstructor!
”<(4)在main()函数中定义两个Rect类的对象,一个对象用实例实现(就像定义普通的变量一样),另一个对象用指针实现(利用关键字new,给指针分配内存空间)。
并用不同的参数,以调用不同的构造函数体现构造函数的重载。
源程序:
#include
usingnamespacestd;
classRect
{
public:
intArea_int();
doubleArea_double();
Rect(doublelength,doublewidth);
Rect(intlength,intwidth);
~Rect(){
cout<<"Iamtheconstructor!
"<}
Rect(){
cout<<"Iamtheconstructor!
"<}
private:
intnLength;
intnWidth;
doubledLength;
doubledWidth;
};
Rect:
:
Rect(intlength,intwidth)
{
nLength=length;
nWidth=width;
}
Rect:
:
Rect(doublelength,doublewidth)
{
dLength=length;
dWidth=width;
}
intRect:
:
Area_int()
{
returnnLength*nWidth;
}
doubleRect:
:
Area_double()
{
returndLength*dWidth;
}
voidmain()
{
RectI(30,50);
Rect*pt;
pt=newRect(10,300);
cout<<"Area_int()函数计算长方形面积是:
"<cout<<"Area_double函数计算长方形面积并用指针分配空间的是:
"<Area_int()<}
4、声明一个Student,在该类中包括一个数据成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全班成绩的平均值。
在main()函数中,输入某班学生的成绩,并调用上述函数求出全班学生的成绩之和和平均分。
源程序:
#include
usingnamespacestd;
classStudent{
public:
voidaccount(doubles);
staticdoublesum();
staticdoubleaverage();
private:
doublescore;
staticdoubletotal_score;
staticdoublecount;
};
doubleStudent:
:
total_score=0;
doubleStudent:
:
count=0;
voidStudent:
:
account(doubles)
{
score=s;
total_score=total_score+score;
count=count+1;
}
doubleStudent:
:
sum()
{
cout<<"所有学生的成绩之和是:
";
returntotal_score;
}
doubleStudent:
:
average()
{
cout<<"学生的平均成绩是:
";
returntotal_score/count;
}
intmain()
{
Studentstud[10];
intn;
doubles;
cout<<"请输入学生的人数(1-10):
";
cin>>n;
for(inti=0;i{
cout<<"请输入"<
";
cin>>s;
stud[i].account(s);
}
cout<:
sum()<cout<:
average()<return0;
}
5、设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果:
A(x1,y1),B(x2,y2)
Distance=d
其中:
x1、y1、x2、y2为指定的坐标值,d为两个坐标点之间的距离。
源程序:
#include
#include
classLocation{
public:
Location(double,double);
doubleGetx()
{
returnx;
}
doubleGety()
{
returny;
}
doubledistance(Location&);
frienddoubledistance(Location&,Location&);
private:
doublex,y;
};
Location:
:
Location(doublea,doubleb)
{
x=a,y=b;
}
doubleLocation:
:
distance(Location&p)
{
doubledx=x-p.x;
doubledy=y-p.y;
return(double)sqrt(dx*dx+dy*dy);
}
doubledistance(Location&p1,Location&p2)
{
doubledx=p1.x-p2.x;
doubledy=p1.y-p2.y;
returnsqrt(dx*dx+dy*dy);
}
doublemain()
{
LocationA(-10,-30),B(-50,70);
cout<<"A("<cout<<"Distance1="<cout<<"Distance2="<return0;
}
实验三派生类与继承
一、实验目的
1、学习类的继承,能够定义和使用类的继承关系。
2、学习派生类的声明与定义方法。
3、掌握类的定义和对象的声明。
4、熟悉公有派生和私有派生的访问特性。
5、掌握派生类构造函数和析构函数的执行顺序。
6、掌握利用访问声明调整基类成员在派生类中的访问属性。
二、试验内容
1、已有类Time和Date,要求设计一个派生类Birthtime,它继承类Time和Date,并且增加一个数据成员Childname用于表示小孩的名字,同事设计主程序显示一个小孩的出生时间和名字。
classTime
{
public:
Time(inth,intm,ints)
{
hours=h;
minutes=m;
seconds=s;
}
voiddisplay()
{
cout<<"出生时间:
"<}
protected:
inthours,minutes,seconds;
};
classDate
{