C++学习之路
来源:互联网 发布:云购cms源码 编辑:程序博客网 时间:2024/05/20 04:13
面向对象程序设计B上机实习报告
(第2次)
学生姓名: 曾而康
学 号: 20151003247
班 序 号: 111151-20
指导教师: 尚建嘎
中国地质大学信息工程学院
2016.3.22
上机题目一(<小二,黑体,居中>)
【需求规格说明】
题目1:
定义一个Point类,记录平面上一个点的坐标(x,y)。以友元方式重载运算符“-”,使其可以计算平面上两个点之间的距离。
程序的测试数据及相应的运行效果如下所示(下划线部分表示输入):
Please input Xand Y coordinates of the first point 0 0
Please input Xand Y coordinates of the second point 5 5
Distance between (0,0) and (5,5) is : 7.07107
问题分析:
该题目考察重载基本运算符以及相关调用的知识,要求以友元的方式进行重载。
【设计思想】
先设计一个Point类,其有两个数据成员X Y,访问类型控制为私有,并含有一个友元函数,这个函数为重载运算符函数,返回值类型为double.用于返回两点间的距离,函数的参数为两个Point对象。并在主函数中调用验证。
【调试报告】
测试数据:
Point1(0,0) Point2(5,5)
运行结果:
【代码清单】
#include<cmath>
#include<iostream>
class Point
{
public:
Point(int xx,int yy){x=xx;y=yy;}//构造函数
int GetX()const{return x;}
int GetY()const{return y;}
private:
int x;
int y;
friend float operator - (PointonePoint,Point otherPoint);//友元重载函数
};
floatoperator-(Point onePoint,Point otherPoint)
{
floatdistance=sqrt(pow(onePoint.GetX()-otherPoint.GetX(),2)+pow(onePoint.GetY()-otherPoint.GetY(),2));//具体计算
return distance;
}
int main()
{
int x1,y1;
int x2,y2;
std::cout<<"Please input Xand Y coordinates of the first point:";
std::cin>>x1>>y1;
std::cout<<"Please input Xand Y coordinates of the second point:";
std::cin>>x2>>y2;
//由输入的数值构造两个点的对象
Point point1(x1,y1);
Point point2(x2,y2);
//调用重载运算符 -
float Distance=point1-point2;
std::cout<<"Distance between("<<point1.GetX()<<","<<point2.GetY()<<")and("<<point2.GetX()<<","<<point2.GetY()<<")is :"<<Distance<<std::endl;
return 0;
}
【UML类图】
上机题目二(<小二,黑体,居中>)
【需求规格说明】
题目2:
重载运算符+、-和=分别实现两个等长数组的加法(对应元素相加)、减法(对应元素相减)和数组之间的赋值。要求定义一维数组类Array,可用于记录一个一维数组的各元素值以及数组元素的个数。假设x、y、z是Array类的对象,x的成员arr[] = {1,3,5,7,9},y的成员arr[] = {2,4,6,8,10},则执行z = x+y后,z的成员arr[] = {3,7,11,15,19}。Array部分实现如下:
Class Array{
public :
Array(floata[],int n);
private :
float arr[20];
int size;
};
问题分析:
题目要求实现两个等长数组相加,要求进行相关类的定义实现,以及使用重载的方式重载相应的运算符,以实现数组的加法,减法,以及赋值操作。
【设计思想】
先把数组类进行完善,添加成员函数GetSize()和成员函数Getar()用与获取访问权限为私有的数据成员。之后进行运算符的重载,对于+ -两个运算符它们的参数为两个数组类对象(同时要求等长),返回值类型为数组类Array对象,对于=运算符,其返回值类型为空,即只要实现右边的数组给左边的数组赋值即可。之后再在主函数中进行验证。
【调试报告】
测试数据:
float arr1[]={1,3,5,7,9};
float arr2[]={2,4,6,8,10};
Array array1(arr1,5);
Array array2(arr2,5);
运行结果:
:
【代码清单】
#include<iostream>
using namespacestd;
//Array类的定义
class Array
{
public:
//缺省的构造函数
Array()
{
float temp[20];
for(int i=0;i<20;i++)
arr[i]=temp[i];
Size=20;
}
//带参数的构造函数
Array(float a[],int n);
int GetSize()
{
return Size;
}
Array operator+(Array otherArray);
Array operator-(Array otherArray);
void operator=(Array otherArray);
float* GetArray() ;//用于返回数组首地址
void print();//用于输出类相关数据
private:
float arr[20];
int Size;
};
//打印相关数据
voidArray::print()
{
for(int i=0;i<Array::GetSize();i++)
cout<<""<<Array::GetArray()[i];
cout<<endl;
}
float*Array::GetArray()
{
return arr;
}
Array::Array(floata[],int n)
{
for(int i=0;i<n;i++)
{
arr[i]=a[i];
}
Size=n;
}//构造函数的实现,给相应的数据成员传值;
ArrayArray::operator +(Array otherArray)
{
float* temp=new float[Array::GetSize()];//使用动态数组,增加灵活性
for(int i=0;i<Array::GetSize();i++)
{
temp[i]=Array::GetArray()[i]+otherArray.GetArray()[i];//实现数组相加
}
ArraytempArray(temp,otherArray.GetSize());//建立一个临时的Array对象,并把它返回;
delete[] temp;//释放相关内存
return tempArray;
}
ArrayArray::operator -(Array otherArray)
{
float* temp=newfloat[Array::GetSize()];
for(int i=0;i<Array::GetSize();i++)
{
temp[i]=Array::GetArray()[i]-otherArray.GetArray()[i];//实现数组相减
}
ArraytempArray(temp,otherArray.GetSize());//建立一个临时的Array对象,并把它返回;
delete[] temp;
return tempArray;
}
voidArray::operator =(Array otherArray)
{
for(inti=0;i<Array::GetSize();i++)//实现拷贝
{
arr[i]=otherArray.GetArray()[i];
}
Size=otherArray.GetSize();
}
int main()
{
float arr1[]={1,3,5,7,9};
float arr2[]={2,4,6,8,10};
Array array1(arr1,5);
Array array2(arr2,5);
array1.print();
array2.print();
Array array3;//建立一个空对象
array3=array1+array2;
array3.print();//验证加法
return 0;
}
【UML类图】
上机题目三(<小二,黑体,居中>)
【需求规格说明】
题目三:
3、编写一个学生和教师信息输入和显示的程序,学生信息有编号、姓名、班级和成绩,教师的信息有编号、姓名、职称和所属学院、系。请按照要求设计基类Person,并由此派生出学生类Student和教师类Teacher。具体要求如下:
(1)定义基类Person,protected成员:
Ø int pid : 编号
Ø char pname[10] : 姓名
public成员:
Ø Person(int id,char name[]) : 构造函数
Ø void show() : 请设计成虚函数,功能是输出pid和name的值。
(2)Student类 :公有继承Person类,根据题目描述设计适当的成员。
(3)Teacher类 :公有继承Person类,根据题目描述设计适当的成员。
(4)编写函数 void func(Person * p),其功能是根据参数p所指的对象调用恰当的show()成员函数来输出指定的学生对象或者教师对象的信息。
(5)编写main函数对程序功能进行测试。
程序的参考执行效果如下所示:
This is astudent:
Id = 1036 name = Peter major = pharmacy classid =16 grade = 3
This is ateacher:
Id = 2018 name = lucy jobtitle = lecturer department =school of science
题目分析:
该题目要求利用继承与派生的方法,进行相关基类与派生类的设计与实现,可利用公有继承的方式,将相关类联系在一起。并实现相关成员函数。
【设计思想】
先实现基类Person,将最基本的成员实现,并将输出信息的函数show设置为虚函数。Student类和Teacher类继承至Person类,并添加新的数据成员,同时重新实现show函数,对于func函数,它的参数为基类指针,利用这一点。在调用时可以先创建基类指针数组,利用对象的地址赋值。之后利用循环调用,可大大提高编程效率。
【调试报告】
测试数据:
char Apersonname[]="Tom";
char Astudentname[]="Peter";
char Ateachername[]="Lucy";
stringstudentmajor("pharmacy");
stringteacherJobtitle("Lecturer");
string teacherdepatment("school ofscience");
Person Aperson(20,Apersonname);
StudentAstudent(1036,Astudentname,studentmajor,16,3);
TeacherAteacher(2018,Astudentname,teacherJobtitle,teacherdepatment);
运行结果:
【代码清单】
#include<iostream>
#include<string>
using namespacestd;
class Person
{
protected:
int pid;
char pname[10];
public:
Person(int id,char name[]);
virtual void show();
};
Person::Person(intid,char name[])
{
pid=id;
for(int i=0;i<10;i++)
pname[i]=name[i];
}
voidPerson::show()
{
cout<<endl;
cout<<" ID="<<pid<<" Name="<<pname;
}
//Student类的实现
classStudent:public Person
{
public:
//构造函数
Student(int id,char name[],stringmajor,int classid,int grade):Person(id,name)
{
Major=major;
Classid=classid;
Grade=grade;
}
void show();
private:
string Major;
int Classid;
int Grade;
};
voidStudent::show()
{
Person::show();//调用基类的show函数。
cout<<" Major="<<Major<<" Classid="<<Classid<<" Grade="<<Grade;
}
//Teacher类的实现
classTeacher:public Person
{
public:
//构造函数
Teacher(int id,char name[],stringjobtitle, string depatment):Person(id,name)
{
Jobtitle=jobtitle;
Depatment=depatment;
}
void show();
private:
string Jobtitle;
string Depatment;
};
voidTeacher::show()
{
Person::show();
cout<<" Jobtitle="<<Jobtitle<<" Depatment="<<Depatment;
}
void func(Person*p)
{
p->show();
}
int main()
{
char Apersonname[]="Tom";
char Astudentname[]="Peter";
char Ateachername[]="Lucy";
stringstudentmajor("pharmacy");
stringteacherJobtitle("Lecturer");
string teacherdepatment("school ofscience");
Person Aperson(20,Apersonname);//构造一个Person对象
//构造一个Student对象
StudentAstudent(1036,Astudentname,studentmajor,16,3);
//构造一个Teacher对象
TeacherAteacher(2018,Astudentname,teacherJobtitle,teacherdepatment);
//创建一个基类指针数组
Person* s[]={&Aperson,&Astudent,&Ateacher};
for(int i=0;i<3;i++)
{
func(s[i]);
}
return 0;
}
【UML类图】
总 结
(<五号,宋体>,要求:1)字数不限。2)具体内容:通过本次上机实习,谈谈你对本次上机实习及本章(或多章)内容的体会和感想)
本次上机做的这三个题目,主要考察我们对类以及运算符重载的知识,通过这些编程,我渐渐了解到运算符的函数本至,即通过调用运算符,实现对操作数相关数据的修改,其与函数的本质是一样的,其中运算符重载的返回值个人认为比较重要,需要根据实际需求进行设置,希望以后能多多上机,天天向上!
面向对象程序设计B上机实习报告
(第2次)
学生姓名: 曾而康
学 号: 20151003247
班 序 号: 111151-20
指导教师: 尚建嘎
中国地质大学信息工程学院
2016.3.22
上机题目一(<小二,黑体,居中>)
【需求规格说明】
题目1:
定义一个Point类,记录平面上一个点的坐标(x,y)。以友元方式重载运算符“-”,使其可以计算平面上两个点之间的距离。
程序的测试数据及相应的运行效果如下所示(下划线部分表示输入):
Please input Xand Y coordinates of the first point 0 0
Please input Xand Y coordinates of the second point 5 5
Distance between (0,0) and (5,5) is : 7.07107
问题分析:
该题目考察重载基本运算符以及相关调用的知识,要求以友元的方式进行重载。
【设计思想】
先设计一个Point类,其有两个数据成员X Y,访问类型控制为私有,并含有一个友元函数,这个函数为重载运算符函数,返回值类型为double.用于返回两点间的距离,函数的参数为两个Point对象。并在主函数中调用验证。
【调试报告】
测试数据:
Point1(0,0) Point2(5,5)
运行结果:
【代码清单】
#include<cmath>
#include<iostream>
class Point
{
public:
Point(int xx,int yy){x=xx;y=yy;}//构造函数
int GetX()const{return x;}
int GetY()const{return y;}
private:
int x;
int y;
friend float operator - (PointonePoint,Point otherPoint);//友元重载函数
};
floatoperator-(Point onePoint,Point otherPoint)
{
floatdistance=sqrt(pow(onePoint.GetX()-otherPoint.GetX(),2)+pow(onePoint.GetY()-otherPoint.GetY(),2));//具体计算
return distance;
}
int main()
{
int x1,y1;
int x2,y2;
std::cout<<"Please input Xand Y coordinates of the first point:";
std::cin>>x1>>y1;
std::cout<<"Please input Xand Y coordinates of the second point:";
std::cin>>x2>>y2;
//由输入的数值构造两个点的对象
Point point1(x1,y1);
Point point2(x2,y2);
//调用重载运算符 -
float Distance=point1-point2;
std::cout<<"Distance between("<<point1.GetX()<<","<<point2.GetY()<<")and("<<point2.GetX()<<","<<point2.GetY()<<")is :"<<Distance<<std::endl;
return 0;
}
【UML类图】
上机题目二(<小二,黑体,居中>)
【需求规格说明】
题目2:
重载运算符+、-和=分别实现两个等长数组的加法(对应元素相加)、减法(对应元素相减)和数组之间的赋值。要求定义一维数组类Array,可用于记录一个一维数组的各元素值以及数组元素的个数。假设x、y、z是Array类的对象,x的成员arr[] = {1,3,5,7,9},y的成员arr[] = {2,4,6,8,10},则执行z = x+y后,z的成员arr[] = {3,7,11,15,19}。Array部分实现如下:
Class Array{
public :
Array(floata[],int n);
private :
float arr[20];
int size;
};
问题分析:
题目要求实现两个等长数组相加,要求进行相关类的定义实现,以及使用重载的方式重载相应的运算符,以实现数组的加法,减法,以及赋值操作。
【设计思想】
先把数组类进行完善,添加成员函数GetSize()和成员函数Getar()用与获取访问权限为私有的数据成员。之后进行运算符的重载,对于+ -两个运算符它们的参数为两个数组类对象(同时要求等长),返回值类型为数组类Array对象,对于=运算符,其返回值类型为空,即只要实现右边的数组给左边的数组赋值即可。之后再在主函数中进行验证。
【调试报告】
测试数据:
float arr1[]={1,3,5,7,9};
float arr2[]={2,4,6,8,10};
Array array1(arr1,5);
Array array2(arr2,5);
运行结果:
:
【代码清单】
#include<iostream>
using namespacestd;
//Array类的定义
class Array
{
public:
//缺省的构造函数
Array()
{
float temp[20];
for(int i=0;i<20;i++)
arr[i]=temp[i];
Size=20;
}
//带参数的构造函数
Array(float a[],int n);
int GetSize()
{
return Size;
}
Array operator+(Array otherArray);
Array operator-(Array otherArray);
void operator=(Array otherArray);
float* GetArray() ;//用于返回数组首地址
void print();//用于输出类相关数据
private:
float arr[20];
int Size;
};
//打印相关数据
voidArray::print()
{
for(int i=0;i<Array::GetSize();i++)
cout<<""<<Array::GetArray()[i];
cout<<endl;
}
float*Array::GetArray()
{
return arr;
}
Array::Array(floata[],int n)
{
for(int i=0;i<n;i++)
{
arr[i]=a[i];
}
Size=n;
}//构造函数的实现,给相应的数据成员传值;
ArrayArray::operator +(Array otherArray)
{
float* temp=new float[Array::GetSize()];//使用动态数组,增加灵活性
for(int i=0;i<Array::GetSize();i++)
{
temp[i]=Array::GetArray()[i]+otherArray.GetArray()[i];//实现数组相加
}
ArraytempArray(temp,otherArray.GetSize());//建立一个临时的Array对象,并把它返回;
delete[] temp;//释放相关内存
return tempArray;
}
ArrayArray::operator -(Array otherArray)
{
float* temp=newfloat[Array::GetSize()];
for(int i=0;i<Array::GetSize();i++)
{
temp[i]=Array::GetArray()[i]-otherArray.GetArray()[i];//实现数组相减
}
ArraytempArray(temp,otherArray.GetSize());//建立一个临时的Array对象,并把它返回;
delete[] temp;
return tempArray;
}
voidArray::operator =(Array otherArray)
{
for(inti=0;i<Array::GetSize();i++)//实现拷贝
{
arr[i]=otherArray.GetArray()[i];
}
Size=otherArray.GetSize();
}
int main()
{
float arr1[]={1,3,5,7,9};
float arr2[]={2,4,6,8,10};
Array array1(arr1,5);
Array array2(arr2,5);
array1.print();
array2.print();
Array array3;//建立一个空对象
array3=array1+array2;
array3.print();//验证加法
return 0;
}
【UML类图】
上机题目三(<小二,黑体,居中>)
【需求规格说明】
题目三:
3、编写一个学生和教师信息输入和显示的程序,学生信息有编号、姓名、班级和成绩,教师的信息有编号、姓名、职称和所属学院、系。请按照要求设计基类Person,并由此派生出学生类Student和教师类Teacher。具体要求如下:
(1)定义基类Person,protected成员:
Ø int pid : 编号
Ø char pname[10] : 姓名
public成员:
Ø Person(int id,char name[]) : 构造函数
Ø void show() : 请设计成虚函数,功能是输出pid和name的值。
(2)Student类 :公有继承Person类,根据题目描述设计适当的成员。
(3)Teacher类 :公有继承Person类,根据题目描述设计适当的成员。
(4)编写函数 void func(Person * p),其功能是根据参数p所指的对象调用恰当的show()成员函数来输出指定的学生对象或者教师对象的信息。
(5)编写main函数对程序功能进行测试。
程序的参考执行效果如下所示:
This is astudent:
Id = 1036 name = Peter major = pharmacy classid =16 grade = 3
This is ateacher:
Id = 2018 name = lucy jobtitle = lecturer department =school of science
题目分析:
该题目要求利用继承与派生的方法,进行相关基类与派生类的设计与实现,可利用公有继承的方式,将相关类联系在一起。并实现相关成员函数。
【设计思想】
先实现基类Person,将最基本的成员实现,并将输出信息的函数show设置为虚函数。Student类和Teacher类继承至Person类,并添加新的数据成员,同时重新实现show函数,对于func函数,它的参数为基类指针,利用这一点。在调用时可以先创建基类指针数组,利用对象的地址赋值。之后利用循环调用,可大大提高编程效率。
【调试报告】
测试数据:
char Apersonname[]="Tom";
char Astudentname[]="Peter";
char Ateachername[]="Lucy";
stringstudentmajor("pharmacy");
stringteacherJobtitle("Lecturer");
string teacherdepatment("school ofscience");
Person Aperson(20,Apersonname);
StudentAstudent(1036,Astudentname,studentmajor,16,3);
TeacherAteacher(2018,Astudentname,teacherJobtitle,teacherdepatment);
运行结果:
【代码清单】
#include<iostream>
#include<string>
using namespacestd;
class Person
{
protected:
int pid;
char pname[10];
public:
Person(int id,char name[]);
virtual void show();
};
Person::Person(intid,char name[])
{
pid=id;
for(int i=0;i<10;i++)
pname[i]=name[i];
}
voidPerson::show()
{
cout<<endl;
cout<<" ID="<<pid<<" Name="<<pname;
}
//Student类的实现
classStudent:public Person
{
public:
//构造函数
Student(int id,char name[],stringmajor,int classid,int grade):Person(id,name)
{
Major=major;
Classid=classid;
Grade=grade;
}
void show();
private:
string Major;
int Classid;
int Grade;
};
voidStudent::show()
{
Person::show();//调用基类的show函数。
cout<<" Major="<<Major<<" Classid="<<Classid<<" Grade="<<Grade;
}
//Teacher类的实现
classTeacher:public Person
{
public:
//构造函数
Teacher(int id,char name[],stringjobtitle, string depatment):Person(id,name)
{
Jobtitle=jobtitle;
Depatment=depatment;
}
void show();
private:
string Jobtitle;
string Depatment;
};
voidTeacher::show()
{
Person::show();
cout<<" Jobtitle="<<Jobtitle<<" Depatment="<<Depatment;
}
void func(Person*p)
{
p->show();
}
int main()
{
char Apersonname[]="Tom";
char Astudentname[]="Peter";
char Ateachername[]="Lucy";
stringstudentmajor("pharmacy");
stringteacherJobtitle("Lecturer");
string teacherdepatment("school ofscience");
Person Aperson(20,Apersonname);//构造一个Person对象
//构造一个Student对象
StudentAstudent(1036,Astudentname,studentmajor,16,3);
//构造一个Teacher对象
TeacherAteacher(2018,Astudentname,teacherJobtitle,teacherdepatment);
//创建一个基类指针数组
Person* s[]={&Aperson,&Astudent,&Ateacher};
for(int i=0;i<3;i++)
{
func(s[i]);
}
return 0;
}
【UML类图】
总 结
(<五号,宋体>,要求:1)字数不限。2)具体内容:通过本次上机实习,谈谈你对本次上机实习及本章(或多章)内容的体会和感想)
本次上机做的这三个题目,主要考察我们对类以及运算符重载的知识,通过这些编程,我渐渐了解到运算符的函数本至,即通过调用运算符,实现对操作数相关数据的修改,其与函数的本质是一样的,其中运算符重载的返回值个人认为比较重要,需要根据实际需求进行设置,希望以后能多多上机,天天向上!
- C学习之路
- C语言学习之路
- C#.NET学习之路
- c++DLL 学习之路
- C/C++ 学习之路
- C语言学习之路。
- c++DLL 学习之路
- c++DLL 学习之路
- Objective-c学习之路
- C学习之路_7
- C/C++学习之路。
- c语言学习之路
- C/C++学习之路
- C语言学习之路
- C#WebApi学习之路
- C语言学习之路
- C语言学习之路
- C/C++学习之路之数据类型
- java的static关键字——static变量
- Redis 存储字符串和对象
- Greepplum Database增加Segment Mirror全过程
- 人脸识别方法简记
- 将博客搬至CSDN
- C++学习之路
- Java对象、对象引用关系及参数传递讨论
- Android开源项目合集
- 书签 网址地址
- 网易webapp 通过js计算html的font-size
- 单词接龙
- Android应用程序管理服务启动过程浅析(PackageManagerService)
- 瀑布流
- eclipse-tomcat发布java web项目 解决404