实验二 类与对象.docx

上传人:b****8 文档编号:28428533 上传时间:2023-07-13 格式:DOCX 页数:24 大小:130.55KB
下载 相关 举报
实验二 类与对象.docx_第1页
第1页 / 共24页
实验二 类与对象.docx_第2页
第2页 / 共24页
实验二 类与对象.docx_第3页
第3页 / 共24页
实验二 类与对象.docx_第4页
第4页 / 共24页
实验二 类与对象.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

实验二 类与对象.docx

《实验二 类与对象.docx》由会员分享,可在线阅读,更多相关《实验二 类与对象.docx(24页珍藏版)》请在冰豆网上搜索。

实验二 类与对象.docx

实验二类与对象

实验二类与对象

一、实验目的

1、学习类与对象的定义,掌握类与对象的使用方法。

2、学习数据成员与成员函数的访问方式,理解构造函数和析构函数的定义与执行过程,学会构造函数的重载方法。

3、掌握数组与指针的定义与使用方法,理解数组与指针的存储分配与表示。

4、掌握用指针和引用向函数传递参数。

5、掌握静态数据成员和静态成员函数的使用。

6、理解友元与友元函数的作用与使用方法。

二、实验内容

1、下面是一个计算器类的定义,请完成该类成员函数的实现。

classCounter

{

public:

Counter(intnumber);

voidincrement();//给原值加1

voiddecrement();//给原值减1

intgetValue();//取得计数器值

intprint();//显示计数

private:

intvalue;

};

#include

classcount

{

public:

counter(intnumber);

voidincrement();

voiddecrement();

intgetvalue(int);

intprint();

private:

intvalue;

};

voidcount:

:

increment()

{

inta=value+1;

}

voidcount:

:

decrement()

{

intb=value-1;

}

intcount:

:

getvalue(ints)

{

value=s;

return0;

}

intcount:

:

print()

{

cout<

cout<

return0;

}

voidmain()

{

counts;

s.getvalue(5);

s.print();

}

//2、根据注释语句的提示,实现类Date的成员函数。

#include

classDate

{

public:

voidprintDate();//显示日期

voidsetDay(intd);//设置日的值

voidsetMonth(intm);//设置月的值

voidsetYear(inty);//设置年的值

private:

intday,month,year;

};

voidDate:

:

printDate()

{

cout<

}

voidDate:

:

setDay(intd)

{

day=d;

}

voidDate:

:

setMonth(intm)

{

month=m;

}

voidDate:

:

setYear(inty)

{

year=y;

}

intmain()

{

DatetestDay;

testDay.setDay(5);

testDay.setMonth(10);

testDay.setYear(2014);

testDay.printDate();

return0;

}

3、建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。

用类cylinder计算圆柱体的体积,并存储在一个double变量中。

在类cylinder中包含一个成员函数vol(),用来显示每个cylinder对象的体积。

#include

classcylinder

{

private:

doubler;

doubleh;

doublev;

public:

~cylinder();

doublevol();

cylinder(double,double);

};

cylinder:

:

cylinder(doublem,doublen):

r(m),h(n)

{}

cylinder:

:

~cylinder()

{

cout<<"Constructorcalled"<

}

doublecylinder:

:

vol()

{

doublev;

v=3.14*r*r*h;

returnv;

}

doublemain()

{

cylindera(1.1,2.2);

cout<<"体积="<

return0;

}

4、构建一个类book,其中含有两个私有数据成员qu和price,建立一个有5个元素的数组对象,将qu初始化为1~5,将price初始化为qu的10倍。

显示每个对象的qu*price值。

#include

classbook

{

private:

intqu;

intprice;

ints;

public:

book(intp,intq):

qu(p),price(q)

{}

voidprint()

{

cout<

}

};

intmain()

{

booka(1,10);

a.print();

bookb(2,20);

b.print();

bookc(3,30);

c.print();

bookd(4,40);

d.print();

booke(5,50);

e.print();

return0;

}

5、修改上题,通过对象指针访问对象数组,使程序以相反的顺序显示对象数组的qu*price值。

#include

classbook

{

private:

intqu;

intprice;

ints;

public:

book(intp)

{

qu=p;

price=qu*10;

}

intprint()

{

return(qu*price);

}

};

intmain()

{

booka[5]={1,2,3,4,5};

book*p;

p=&a[4];

for(inti=4;i>=0;i--)

{

cout<print()<

p--;

}

return0;

}

6、构建一个类Stock,含字符数组stockcode[]及整型数据成员quan、双精度型数据成员price。

构造函数含3个参数:

字符数组na[]及q、p。

当定义Stock的类对象时,将对象的第一个字符串参数赋给数据成员stockcode,第2个和第3个参数分别赋给quan和price。

未设置第2个和第3个参数时,quan的值为1000,price的值为8.98。

成员函数print()使用this指针,显示对象内容。

#include

#include

classStock

{

charstockcode[10];

intquan;

doubleprice;

public:

Stock(charna[10],intq=1000,doublep=8.98);

voidprint();

};

Stock:

:

Stock(charna[10],intq,doublep)

{

strcpy(stockcode,na);

quan=q;

price=p;

}

voidStock:

:

print()

{

cout<stockcode<<","<quan<<","<price<

}

voidmain()

{

Stockm("sdgjgj",798,9.89);

m.print();

Stockn("sljf");

cout<<"默认:

";

n.print();

}

7、参考课本例子,建立一个源程序文件,在此文件中建立一个新的类,将新建的类命名为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

classRect

{

public:

intArea_int();

doubleArea_double();

Rect(doublelength,doublewidth);

Rect(intlength,intwidth);

virtual~Rect();

private:

intnLength;

intnWidth;

doubledLength;

doubledWidth;

};

intRect:

:

Area_int()

{

ints;

s=nLength*nWidth;

cout<<"int的长方形的面积:

"<

return0;

}

doubleRect:

:

Area_double()

{

doublek;

k=dLength*dWidth;

cout<<"double型的长方形的面积:

"<

return0;

}

Rect:

:

Rect(intlength,intwidth)

{

nLength=length;

nWidth=width;

cout<<"Iamtheconstructor!

"<

}

Rect:

:

~Rect()

{

cout<<"Iamthedestructor"<

}

Rect:

:

Rect(doublelength,doublewidth)

{

dLength=length;

dWidth=width;

cout<<"Iamtheconstructor!

"<

}

voidmain()

{

Recta(2,2),b(2.3,3.4);

a.Area_int();

b.Area_double();

Rect*p=newRect(3,4);

p->Area_int();

deletep;

Rect*q=newRect(3.2,3.4);

q->Area_double();

deleteq;

}

8、声明一个Student,在该类中包括一个数据成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全班成绩的平均值。

在main()函数中,输入某班学生的成绩,并调用上述函数求出全班学生的成绩之和和平均分。

#include

classstudent

{

doublescore;

staticdoubletatal_score;

staticintcount;

staticdoubleave;

public:

voidaccount(double);

staticdoublesum();

staticdoubleaverage();

voidprint();

};

voidstudent:

:

account(doublem)

{

score=m;

tatal_score=tatal_score+m;

++count;

}

doublestudent:

:

sum()

{

return(tatal_score);

}

doublestudent:

:

average()

{

ave=tatal_score/count;

returnave;

}

voidstudent:

:

print()

{

cout<<"人数为:

"<

cout<<"总成绩为:

"<

"<

}

intstudent:

:

count=0;

doublestudent:

:

tatal_score=0.0;

doublestudent:

:

ave=0.0;

voidmain()

{

studenta;

a.account(97);

a.sum();

a.average();

studentb;

b.account(87);

b.sum();

b.average();

b.print();

}

9、设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果:

A(x1,y1),B(x2,y2)

Distance=d

其中:

x1、y1、x2、y2为指定的坐标值,d为两个坐标点之间的距离。

#include

#include

classlocation

{

doublex;

doubley;

public:

location(double,double);

voidprint(locationm);

friendvoidprint(location&,location&);

};

location:

:

location(doublem,doublen)

{

x=m;

y=n;

}

voidlocation:

:

print(locationm)

{

doubledx=x-m.x;

doubledy=y-m.y;

doubled=sqrt(dx*dx+dy*dy);

cout<<"AB的distance:

"<

}

voidprint(location&a,location&b)

{

doubledx=a.x-b.x;

doubledy=a.y-b.y;

doubled=sqrt(dx*dx+dy*dy);

cout<<"AB的distance:

"<

}

voidmain()

{

locationA(-3.0,-2.0);

locationB(-4.3,4.3);

A.print(B);

print(A,B);

}

10、使用C++的string类,将5个字符串按逆转后的顺序显示出来。

例如,逆转前的5个字符串是:

Germany、Japan、American、British、France

按逆转后的顺序输出字符串为:

France、British、American、Japan、Germany

#include

#include

usingnamespacestd;

voidmain()

{

strings[5]={"Germany","Japan","American","British","France"};

for(inti=4;i>=0;i--)

{

cout<

}

}

11、设计一个矩阵类Matrix,有分配空间和对矩阵赋值的功能,将这个矩阵类的对象作为参数传送到函数Mul(Matrixa,Matrixb),用Mul(Matrixa,Matrixb)函数实现两个Matrix对象相乘的运算。

#include

usingnamespacestd;

classMatrix

{

int**x;

public:

intm;

intn;

voidcheck(Matrix&g,Matrix&h);

Matrix();

voidMul(Matrixa,Matrixb);

};

Matrix:

:

Matrix()

{

cout<<"输出矩阵的行和列:

"<

cin>>m>>n;

cout<<"输入数据:

";

x=newint*[m];

for(inti=0;i

{

x[i]=newint[n];

}

for(intq=0;q

{

for(intr=0;r

{

cin>>x[q][r];

}

}

}

voidMatrix:

:

check(Matrix&g,Matrix&h)

{

if(g.n!

=h.m)

{

cout<<"Wronginput!

"<

exit

(1);

}

}

voidMatrix:

:

Mul(Matrixa,Matrixb)

{

inttemp=0;

cout<<"两矩阵的乘积是:

"<

for(inti=0;i

{

for(intj=0;j

{

temp+=a.x[i][j]+b.x[j][i];

}

cout<

temp=0;

cout<

}

}

voidmain()

{

Matrixa,b;

a.check(a,b);

a.Mul(a,b);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 历史学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1