VC++程序设计课内实验报告Word文件下载.docx
- 文档编号:19073190
- 上传时间:2023-01-03
- 格式:DOCX
- 页数:27
- 大小:162.83KB
VC++程序设计课内实验报告Word文件下载.docx
《VC++程序设计课内实验报告Word文件下载.docx》由会员分享,可在线阅读,更多相关《VC++程序设计课内实验报告Word文件下载.docx(27页珍藏版)》请在冰豆网上搜索。
验证
同组人数
1
成绩
指导教师签字:
一、实验目的
1.进一步加深对类和对象的理解。
2.掌握类的构造函数和析构函数的概念、意义和使用方法。
3.掌握重载构造函数的含义和使用。
4.编写一个较为复杂的类和对象的应用程序。
二、实验内容
1.设计一个程序,定义一个矩形类,包括数据成员和函数成员。
要求有构造函数、析构函数,还有一个成员函数area()用来计算并显示矩形的面积,并编写main函数进行测试。
程序:
#include<
iostream.h>
math.h>
classRect
{
public:
Rect(intl,intw);
~Rect();
intArea();
private:
intnLength;
intnWidth;
};
Rect:
:
Rect(intl,intw)
{cout<
<
"
executingconstructor..."
endl;
nLength=l;
nWidth=w;
cout<
nLength="
nLength<
endl<
nWidth="
nWidth<
}
~Rect()
intRect:
Area()
{intt;
t=nLength*nWidth;
returnt;
intmain()
{RectA(3,4);
s="
A.Area()<
return0;
运行结果:
2.重载构造函数。
修改上题,一种构造函数用整型变量记录矩形的长和宽,另一种构造函数用double型记录矩形的长和宽,然后完成成员函数及主函数。
{public:
Rect(doubleu,doublev);
doubleArea(doubleu,doublev);
doublemLength;
doublemWidth;
};
Rect(doubleu,doublev)
mLength=u;
mWidth=v;
mLength="
mLength<
mWidth="
mWidth<
doubleRect:
Area(doubleu,doublev)
{doubler;
r=mLength*mWidth;
returnr;
voidmain()
a的面积="
RectB(22,33);
b的面积="
B.Area()<
远行结果:
3.构造一个类countstr,要求用构造函数设置计数器count的初始值为0,成员函数countchar()不返回任何值,它要求用户输入一段文字,按Enter键后结束计算,用count记录输入的字符数,成员函数getchar()返回count的整数值。
#include<
classcountstr
{public:
countstr(){count=0;
voidcountchar(){cout<
请输入字符串:
;
cin>
>
str;
while(str[count]!
='
\0'
)count++;
}
intgetchar(){returncount;
}
intcount;
charstr[200];
voidmain(){countstrs;
s.countchar();
cout<
thenumber="
s.getchar()<
实验六利用友元编程
年月日
1.掌握友元函数和友元类的概念、定义和作用。
2.会使用友元编程。
3.进一步学习内存空间动态分配和释放的方法。
1.有一个向量类Vector,包括一个点的坐标位置x和y,设计两个友元函数,实现两个向量的加法和减法运算。
2.定义一个由y=ax+b确定的直线类Line,该类的构造函数初始化直线,成员函数Print显示该直线方程,友元函数SetPoint()求解两条直线的交点。
3.下列程序是有关友元类的,程序中将A类声明为B类的友元类,A类中所有的成员函数都是B类的友元函数,请给程序填空。
#inlcude<
classB;
classA
{
intx;
public:
A(intxx){x=xx;
intSet(B&
);
intGet(){returnx;
classB
B(intxx){①}
friend②
intA:
Set(③)
{returnx=b.x;
{Aa(10);
Bb(20);
a.Get()<
a.Set(b);
三、编程分析及运行结果
1、#include<
classVector
{public:
Vector(doublem=0,doublen=0){x=m;
y=n;
voiddisplay(){cout<
("
x<
"
y<
)"
friendVectoroperator+(Vector&
a,Vector&
b)
{Vectort;
t.x=a.x+b.x;
t.y=a.y+b.y;
returnt;
friendVectoroperator-(Vector&
t.x=a.x-b.x;
t.y=a.y-b.y;
~Vector(){}
doublex,y;
{Vectora1(8,6),a2(1,2.1),a3,a4;
向量a1="
a1.display();
向量a2="
a2.display();
a3=a1+a2;
a1+a2="
a3.display();
a4=a1-a2;
a1-a2="
a4.display();
2、#include<
classLine
Line(doublem=0,doublen=0){a=m;
b=n;
voidprint(){cout<
直线y="
a<
x+"
b<
voidfriendsetpoint(Line&
l1,Line&
l2)
{doublex,y;
if(l1.a==l2.a)cout<
两直线平行,无交点"
else{x=(l1.b-l2.b)/(l2.a-l1.a);
y=l1.a*x+l1.b;
cout<
两直线交点为("
~Line(){}
doublea,b;
{LineL1(3,5),L2(-2,6);
L1.print();
L2.print();
setpoint(L1,L2);
3、填空后程序:
#include<
intSet(B&
B(intxx){x=xx;
friendclassA;
Set(B&
{Aa(12);
Bb(34);
a.Set(b);
四、解决方法及难点
难点:
函数重载时,注意重载的实现方法:
通过形参类型或个数来区分调用哪个函数。
实验七继承与派生的编程
一、实验目的
1.了解继承在面向对象程序设计中的重要作用。
2.理解继承与派生的概念,掌握单继承和多继承的定义方法。
3.熟悉公有派生和私有派生的访问特性。
4.掌握继承中基类和派生类的构造函数定义和调用过程。
5.了解虚基类的作用和用法,会使用虚基类,学习虚基类在解决二义性问题中的作用
二、实验内容
1.有以下程序,请完成下面的工作:
1)阅读程序,写出运行后的输出结果;
2)然后上机运行,验证结果是否正确;
3)分析程序执行过程,尤其是调用构造函数和析构函数的过程。
classA
A(){cout<
“constructingA”<
~A(){cout<
“destructingA”<
classB:
publicA
B(){cout<
“constructingB”<
~B(){cout<
“destructingB”<
classC:
publicB
C(){cout<
“constructingC”<
~C(){cout<
“destructingC”<
{Cc1;
2.定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。
编写一个完整程序进行测试。
3.定义并描述一个人员类Person,它派生出学生类Student和教师类Teacher,学生类和教师类共同派生出在职读书的教师类Stu_Tech。
人员类有姓名、性别、身份证号、出生年月等信息;
学生类有学号、成绩等信息;
教师类有职务、职称等信息。
三、编程分析及运行结果
1、运行结果:
2、classPoint
voidinpoint(doublex,doubley)
{Px=x;
Py=y;
doublegetPx()
{returnPx;
doublegetPy()
{returnPy;
protected:
doublePx,Py;
classRectangle:
publicPoint
Rectangle(doublex,doubley,doubleh,doublew)
{inpoint(x,y);
High=h;
Wide=w;
doublegetHigh(){returnHigh;
doublegetWide(){returnWide;
doubleArea(){returnHigh*Wide;
doubleHigh,Wide;
classCircle:
Circle(doublex,doubley,doubler)
R=r;
doublegetR()
{returnR;
doubleArea()
{doublepi=3.1415926;
returnpi*R*R;
doubleR;
{doublex,y,h,w,r;
请输入矩形的位置坐标(x,y)、长、宽:
x>
y>
h>
w;
RectangleRect1(x,y,h,w);
矩形的数据:
坐标("
Rect1.getPx()<
<
Rect1.getPy()<
),\theigh="
Rect1.getHigh()<
\twidth="
Rect1.getWide()<
\tArea="
Rect1.Area()<
请输入圆的位置坐标(x,y)及半径:
r;
CircleC1(x,y,r);
圆的数据:
C1.getPx()<
C1.getPy()
),\tR="
C1.getR()<
3、#include<
string.h>
classPerson
Person();
Person(char*name1,charsex1,char*id1,char*birth);
voiddisplay();
~Person();
char*name;
char*id;
charsex;
char*birthday;
Person:
Person()
{name='
sex=0;
id='
birthday='
Person(char*name1,charsex1,char*id1,char*birth)
{name=newchar[strlen(name1)+1];
name=strcpy(name,name1);
sex=sex1;
id=newchar[strlen(id1)+1];
id=strcpy(id,id1);
birthday=newchar[strlen(birth)+1];
birthday=strcpy(birthday,birth);
voidPerson:
display()
{cout<
name:
name<
\nsex:
sex<
\nid:
id<
\nbirthday:
birthday<
~Person()
{delete[]name;
delete[]id;
delete[]birthday;
classStudent:
virtualpublicPerson
Student(char*name,charsex,char*id,char*birthday,longints_num1,floatscore1);
voiddisplay()
{Person:
display();
s_number:
s_num<
\nscore:
score<
~Student(){}
longints_num;
floatscore;
Student:
Student(char*name1,charsex1,char*id1,char*birth,longints_num1,floatscore1):
Person(name1,sex1,id1,birth)
{s_num=s_num1;
score=score1;
classTeacher:
Teacher(char*name1,charsex1,char*id1,char*birth,char*posit,char*title1):
{position=newchar[strlen(posit)+1];
position=strcpy(position,posit);
title=newchar[strlen(title1)+1];
title=strcpy(title,title1);
Position:
position<
\nTitle:
title<
~Teacher()
{delete[]position;
delete[]title;
char*position;
char*title;
classStu_teach:
publicStudent,publicTeacher
Stu_teach(char*name1,charsex1,char*id1,char*birth,longints_num1,floatscore1,char*posit,char*title1):
Person(name1,sex1,id1,birth),
Student(name1,sex1,id1,birth,s_num1,score1),Teacher(name1,sex1,id,birth,posit,title1){}
~Stu_teach(){}
voidStu_teach:
{Student:
s1.display();
t1.display();
s_t1.display();
难点:
用虚基类解决继承和派生时的二义性问题..通过虚基类来实现.
实验八利用虚函数编程
1.了解多态性的概念和虚函数的作用及使用方法。
2.了解静态关联和动态关联的概念和用法。
3.了解纯虚函数和抽象类的概念和用法。
4.学习使用虚函数的继承实现动态关联。
5.学习理解静态数据成员和静态成员函数的使用。
1.事先阅读程序,给出其运行结果,上机验证虚函数的作用。
有如下程序:
#include<
classStudent//定义基类
Student(int,string,float);
//声明输出函数
protected:
//受保护成员,派生类可访问
intnum;
stringname;
floatscore;
Student:
Student(intn,stringnam,floats)
{num=n;
name=nam;
score=s;
voidStudent:
num:
num<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VC 程序设计 实验 报告