k=j;
}
if(k!
=i)
{
x=arry[k];
arry[k]=arry[i];
arry[i]=x;
}
}
inta;
for(a=0;a<5;a++)
{
if(arry[a]==student1->average)
{
Console:
:
WriteLine((student1->name));
Console:
:
WriteLine((student1->average).ToString());
}
elseif(arry[a]==student2->average)
{
Console:
:
WriteLine((student2->name));
Console:
:
WriteLine((student2->average).ToString());
}
elseif(arry[a]==student3->average)
{
Console:
:
WriteLine((student3->name));
Console:
:
WriteLine((student3->average).ToString());
}
elseif(arry[a]==student4->average)
{
Console:
:
WriteLine((student4->name));
Console:
:
WriteLine((student4->average).ToString());
}
elseif(arry[a]==student5->average)
{
Console:
:
WriteLine((student5->name));
Console:
:
WriteLine((student5->average).ToString());
}
}
while
(1);
}
实验二类的定义及使用
实验目的:
掌握类及对象的语法规则,练习类成员变量、成员函数、构造函数的使用方法。
实验内容:
1、设计名为CEllipse的椭圆类。
其属性为外接矩形的左上角与右下角两个点的坐标,并能计算出椭圆的面积,并测试该类。
2、设计一个日期类,包含year、month、day数据成员。
包含2个构造函数,一是能够传3个整数给3个数据成员;二是传一个日期格式的字符串(xxxx-xx-xx),函数分解字符串赋值给数据成员。
包含2个获取日期的重载函数GetDate(),一是返回3个整数,二是返回一个日期格式的字符串。
提示:
本题中会用到字符串运算,有关的内容如下:
(1)系统字符串类为String
(2)定义字符串对象String^s;
(3)在字符串对象中查找的函数IndexOf(查找的字符,查找开始位置),例
s->IndexOf("-",4);表示在s中查找字符"-",从s中第4个字符开始查找。
(4)从字符串中截取一部分子串的函数s->Substring(开始位置,子串长度),例如从s中的第6个字符开始,取出2个字符:
s->Substring(5,2);
(5)String和int类型的转换方法Convert:
:
ToInt32(String变量);int变量.ToString();
3、找出下面程序的错误
#include"stdafx.h"
usingnamespaceSystem;
classSample
{
private:
intm_nX;
public:
Sample(inta=0)
{
m_nX=a;
}
};
voidfun(constSample&p)
{
Console:
:
WriteLine(p.m_nX.ToString());
}
int_tmain()
{
Samples(10);
fun(s);
return0;
}
实验2.1
//9.cpp:
主项目文件。
#include"stdafx.h"
#using
#include
usingnamespaceSystem;
classCEllipse
{
public:
doublefun1(doublea,doubleb);
doublefun2(doublec,doubled);
doublearea(doubler1,doubler2);
};
doubleCEllipse:
:
fun1(doublea,doubleb)
{
return((b-a)/2);
}
doubleCEllipse:
:
fun2(doublec,doubled)
{
return((d-c)/2);
}
doubleCEllipse:
:
area(doubler1,doubler2)
{
return(3.14159*r1*r2);
}
intmain(array:
String^>^args)
{
CEllipseA;
String^x;
String^y;
doublex11,y11;
doublex22,y22;
doubler11,r22;
doubles;
Console:
:
WriteLine(L"请输入左上角坐标");
x=Console:
:
ReadLine();
x11=Double:
:
Parse(x);
y=Console:
:
ReadLine();
y11=Double:
:
Parse(y);
Console:
:
WriteLine("您输入的坐标是:
x={0}y={1}",x11.ToString(),y11.ToString());
Console:
:
WriteLine(L"请输入右下角坐标");
x=Console:
:
ReadLine();
x22=Double:
:
Parse(x);
y=Console:
:
ReadLine();
y22=Double:
:
Parse(y);
Console:
:
WriteLine("您输入的坐标是:
x={0}y={1}",x22.ToString(),y22.ToString());
r11=A.fun1(x11,x22);
r22=A.fun2(y22,y11);
s=A.area(r11,r22);
Console:
:
WriteLine(s.ToString());
while
(1);
}
实验2.2
//shiyan2.2.cpp:
主项目文件。
#include"stdafx.h"
#using
usingnamespaceSystem;
refstructDate
{
intd;
inte;
intf;
};
classdate
{
public:
intyear;
intmonth;
intday;
date(inta,intb,intc)
{
year=a;
month=b;
day=c;
}
date(String^s)
{
String^date1;
date1=s;
String^year1;
String^month1;
String^day1;
inti,j;
i=date1->IndexOf("-",1);
j=date1->IndexOf("-",i+1);
year1=date1->Substring(0,4);
month1=date1->Substring(i+1,2);
day1=date1->Substring(j+1,2);
year=Int32:
:
Parse(year1);
month=Int32:
:
Parse(month1);
day=Int32:
:
Parse(day1);
}
Date^GetDate(String^s);
String^GetDate(inth,intk,intl);
};
Date^date:
:
GetDate(String^s)
{
Date^shuju;
shuju=gcnewDate;
String^date1;
date1=s;
String^year1;
String^month1;
String^day1;
inti,j;
i=date1->IndexOf("-",1);
j=date1->IndexOf("-",i+1);
year1=date1->Substring(0,4);
month1=date1->Substring(i+1,2);
day1=date1->Substring(j+1,2);
shuju->d=Int32:
:
Parse(year1);
shuju->e=Int32:
:
Parse(month1);
shuju->f=Int32:
:
Parse(day1);
returnshuju;
}
String^date:
:
GetDate(inth,intk,intl)
{
String^he;
String^ha;
String^hei;
he=h.ToString();
ha=k.ToString();
hei=l.ToString();
String^date11;
date11=he+"-"+ha+"-"+hei;
returndate11;
}
intmain(array:
String^>^args)
{
Date^riqi;
riqi=gcnewDate;
Console:
:
WriteLine(L"输入日期,格式:
xxxx-xx-xx");
String^s;
s=Console:
:
ReadLine();
String^date1;
date1=s;
String^year1;
String^month1;
String^day1;
inti,j;
i=date1->IndexOf("-",1);
j=date1->IndexOf("-",i+1);
year1=date1->Substring(0,4);
month1=date1->Substring(i+1,2);
day1=date1->Substring(j+1,2);
intyear2,month2,day2;
year2=Int32:
:
Parse(year1);
month2=Int32:
:
Parse(month1);
day2=Int32:
:
Parse(day1);
dateA(year2,month2,day2);
//A.date(year2,month2,day2);
//A.date(s);
riqi=A.GetDate(s);
Console:
:
WriteLine("你输入的日期为:
{0}-{1}-{2}",riqi->d.ToString(),riqi->e.ToString(),riqi->f.ToString());
String^hahaha;
hahaha=A.GetDate(year2,month2,day2);
Console:
:
WriteLine("日期为:
{0}",hahaha);
while
(1);
}
实验2.3
//shiyan6.cpp:
主项目文件。
#include"stdafx.h"
usingnamespaceSystem;
classSample
{
//private:
//intm_nX;
public:
intm_nX;
Sample(inta)
{
m_nX=a;
}
};
voidfun(constSample&p)
{
Console:
:
WriteLine(p.m_nX.ToString());
while
(1);
}
intmain()
{
Samples(10);
fun(s);
return0;
}
实验3.1
实验三类的继承实验
实验目的:
掌握继承的概念,理解派生类与基类的关系
实验内容:
1、分析以下程序,理解继承过程中构造函数的执行次序
2、定义一个大学生类CStudent,私有数据成员包括“姓名”、“学号”与“校名”,并为它定义带参数的构造函数,以及名为ToString()的成员函数,功能是将全部信息组合为一个字符串。
令定义一个研究生类,增加私有数据成员“研究方向”、“导师”,同样定义名为ToString()的成员函数。
在主函数中定义派生类对象进行测试。
3、声明一个Shape(形状)基类,它有两个派生类:
Circle(圆)和Square(正方形)。
Shape类的数据成员包括图形基准点的坐标(对圆就是圆心,对正方形就是一个顶点),以及尺寸。
派生类实现周长和面积的计算。
进行测试输出。
//shiyansan1.cpp:
主项目文件。
#include"stdafx.h"
usingnamespaceSystem;
classA
{
public:
A()
{
Console:
:
WriteLine("Constructor1_A");
}
A(intm):
x(m)
{
Console:
:
Write("Constructor2_A");
Console:
:
WriteLine(x.ToString());
}
~A()
{
Console:
:
Write("Destructor_A");
Console:
:
WriteLine(x.ToString());
}
private:
intx;
};
classB:
publicA
{
public:
B()
{
Console:
:
Write("Constructor1_B");
Console:
:
WriteLine(y.ToString());
}
B(intm,intn,intl):
A(m),a(n),y(l)
{
Console:
:
Write("Constructor2_B");
Console:
:
WriteLine(y.ToString());
}
~B()
{