搜索
您的当前位置:首页正文

c++实验报告

来源:易榕旅网
实验一

//1) 编写重载函数Max1可分别求取两个整数,三个整数,两个双精度数,三个双精度数的最大值。

#include using namespace std; int Max1(int x,int y) { if(x>=y) return x; if(xint Max1(int x,int y,int z) { while(x>=y) { if(y>=z) return x; if(y=z) return x; else return z; } } while(x=z) return y; if(x=z) return y; else return z; } } }

double Max1(double x,double y) { if(x>=y) return x; if(xreturn y; }

double Max1(double x,double y,double z) { while(x>=y) { if(y>=z) return x; if(y=z) return x; else return z; } } while(x=z) return y; if(x=z) return y; else return z; } } }

void main() { int x,y,z; cout<<\"请输入x,y,z的值\"<>x>>y>>z; cout<<\"请输出最大值:\"<>a>>b>>c;

cout<<\"请输出最大值:\"<运行结果:

定义//2) 写一个函数,具有一个引用作为形参参数,在函数中改变引用变量的值,观察实参变量的变化。

#include using namespace std; void swap1(int &x,int &y) { int temp; temp=x; x=y; y=temp; }

void swap2(int &x,int y) {

int temp; temp=x; x=y; y=temp; }

void swap3(int x,int y) {

int temp; temp=x; x=y; y=temp; }

void main() {

}

int a=5,b=7; swap1(a,b);

cout<<\"a=\"<cout<<\"i=\"<cout<<\"x=\"<运行结果:

3) 一个CPU类,包含等级(Rank)、频率(frequency)、电压(voltage)等属性,有两个公有

成员函数run、stop。其中,rank为枚举类型CPU__Rank,定义为enumCPU_Rank{P1=1,P2,P3,P4,P5,P6,P7},frequency为单位是MHz的整型数,voltage为浮点型的电压值。观察构造函数和析构函数的调用顺序。 #include using namespace std;

enumCPU_Rank {P1=1,P2,P3,P4,P5,P6,P7}; class CPU {

private:

CPU_Rank rank; int frequency; float voltage; public:

CPU(intnewrank,intnewfrequency,floatnewvoltage); void run(); void stop();

~CPU(){cout<<\"成功调用析构函数\"<};

void CPU::run() {

cout<<\"程序开始执行\"<void CPU::stop() {

cout<<\"程序结束\"<CPU::CPU(intnewrank,intnewfrequency,floatnewvoltage) {

rank=(CPU_Rank)newrank; frequency=newfrequency; voltage=newvoltage;

cout<<\"成功调用构造函数\"<int main() {

CPU cpu(2,60,220); cpu.run(); cpu.stop(); return 0; }

/*4) 定义一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。

cpu为CPU类的一个对象,ram为RAM类的一个对象,cdrom为CDROM类的一个对象, 定义并实现这个类,为以上的类编写构造和析构函数,观察组合类和内嵌类的构造函数和析构函数的调用顺序。*/ #include using namespace std; class Computer{ public: Computer(intcpu,intram,intcdrom); Computer(){ cpu=0;ram=0;cdrom=0; } void run(); void stop(); ~Computer(){cout<<\"成功调用析构函数\"<void Computer::run(){ cout<<\"程序执行\"<void Computer::stop()

{

cout<<\"程序结束\"<Computer::Computer(intnewcpu,intnewram,intnewcdrom) {

cpu=newcpu; ram=newram; cdrom=newcdrom;

cout<<\"成功调用构造函数\"<main() {

Computer computer(2,6,2); computer.run(); computer.stop(); return 0; }

5) 为题目2)的类编写复制构造函数,在主函数中利用复制构造的方式创建新的对象,观察对象的状态。 #include using namespace std;

class Point{ public: Point(int xx=0,int yy=0){ x=xx; y=yy; } Point(Point &p); intgetX(){ return x; } intgetY(){ return y; } private: intx,y; };

Point::Point(Point &p){ x=p.x; y=p.y; cout<<\"复制构造函数\"<void fun1(Point p){ cout<Point fun2(){ Point a(1,2); return a; }

int main(){ Point a(4,5); Point b=a; cout<实验二

1) 实现客户机(CLIENT)类。定义字符型静态数据成员ServerName,保存其服务器名称;

整型静态数据成员ClientNum,记录已定义的客户数量;定义静态函数ChangeServerName()改变服务器名称。

#include #include using namespace std;

class CLIENT{ public:

CLIENT(){

ClientNum++; } void showname(){ cout<<\"已定义的客户数量为:\"<>newName; ServerName=newName;

cout<<\"改变后的名字为:\"<ClientNum--;}

private: static char ServerName; static int ClientNum; }; int CLIENT::ClientNum=0;

char CLIENT::ServerName='m'; int main(){ CLIENT a; a.showname(); a.ChangServerName(); CLIENT b(a); b.showname(); b.ChangServerName(); return 0; }

2)利用多文件结构实现题目1),在头文件client.h中定义类,在文件client.cpp中实现该类,在文件test.cpp 中测试这个类,观察相应的成员变量取值的变化情况,要求ClientNum能够实时记录客户机对象的数量。 //Client.h: class CLIENT{ public:

CLIENT(){ClientNum++;} void showname(); static ChangServerName(); ~CLIENT(){ClientNum--;} private: static char ServerName; static int ClientNum; };

//Client.cpp:

#include\"client.h\" #include using namespace std; int CLIENT::ClientNum=0;

char CLIENT::ServerName='m'; void CLIENT::showname(){

cout<<\"已定义的客户数量为:\"<CLIENT:: ChangServerName(){ char newName; cout<<\"请输入新名字:\"<>newName;

ServerName=newName;

cout<<\"改变后的名字为:\"<//test.cpp:

#include\"client.h\" #include using namespace std; int main(){ CLIENT a; a.showname(); a.ChangServerName(); CLIENT b(a); b.showname(); b.ChangServerName();

return 0; }

运行结果相同,只是写成了多文件形式

3)思考并回答以下概念: 类的静态数据成员,类的静态函数成员,多文件结构,文件包含。 类的静态数据成员:如果某个属性为整个类所共有,不属于任何一个具体对象,则采用static关键字来声明为静态数据成员。

类的静态函数成员:就是使用static关键字声明的函数成员,静态成员函数也属于整个类,由同一个类的所有对象共同拥有,为这些类所共享。

多文件结构:在规模较大的项目中,往往需要多个源程序文件,每个源程序文件成为一个编译单元,可以通过不同的文件实现要求。 文件包含:一个文件包含在另一个文件当中。

实验三

1) 编写一个类用于处理3×3矩阵转置,测试转置的效果,输出转置前后的矩阵。 #include using namespace std; class Matrix {

public: Matrix(int a1[3][3]) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { a[i][j]=a1[i][j]; } } } void change(Matrix &m1); void output(); private: int a[3][3]; };

void Matrix::change(Matrix &m1) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { m1.a[j][i]=a[i][j];

} } }

void Matrix::output() { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cout<void main() { }

cout<<\"原来的矩阵是:\"<cout<<\"转置后的矩阵为:\"<//2) 定义一个具有构造函数和析构函数的类,如实验一的CPU类,定义一个CPU的对象数组,观察构造函数的析构函数的调用过程。 #include using namespace std;

enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7}; class CPU {

private:

CPU_Rank rank; int frequency; float voltage; public:

CPU(int newrank,int newfrequency,float newvoltage); void run(); void stop();

~CPU(){cout<<\"成功调用析构函数\"<void CPU::run() {

cout<<\"程序开始执行\"<void CPU::stop() {

cout<<\"程序结束\"<CPU::CPU(int newrank,int newfrequency,float newvoltage) {

rank=(CPU_Rank)newrank; frequency=newfrequency; voltage=newvoltage;

cout<<\"成功调用构造函数\"<int main() {

CPU c1(2,60,220); CPU c[2]; for(int i=0;i<2;i++) { c[i].run(); c[i].stop();

}

cout<<\"成功运用对象数组\"<3)利用动态内存分配的方式重新完成题目2)。

#include using namespace std;

enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7}; class CPU {

public : CPU(int r){cout<cout<<\"第\"<void run(); void stop(); ~CPU() { static int i=1; cout<<\"第\"<i++; } private:

CPU_Rank Rank; int frequency; double voltage; };

void CPU::run() {

cout<<\"run 正在执行\"<void CPU::stop() {

cout<<\"stop 正在执行\"<int main() { CPU *ptr=new CPU[3]; for(int i=0;i<3;i++) { ptr[i].run(); ptr[i].stop(); } delete[]ptr; return 0; }

//4) 使用系统提供的string类定义字符串对象并初始化,实现从原始字符串中提取一个子串。 #include #include using namespace std; int length(string a) { int i=0; while(a[i]!='\\0') {i++;} return i; }

void show(string a,int b1,int b2) {

if(b1+b2>length(a)) cout<<\"长度超出\"<void main() { int i,j; string a=\"abcdefrghjkmnopqrstuv\";

}

cout<<\"字符串:\"<cout<<\"长度为\"<cout<<\"提取的子串从第i个开始,提取j个:\"<>i;

cout<<\"j=?\"<>j; show(a,i,j); cout<7).思考并回答:数组,指针,对象数组,动态内存分配,默认构造函数,标准类库,字符串类 string,线性拟合。

数组:是具有一定顺序关系的若干对象的集合体。 指针:即为内存地址,用于间接访问内存单元。

对象数组:对象数组的元素是对象,不仅具有数据成员,而且还有函数成员。 动态内存分配:保证程序在运行过程中按照实际需要申请适量的内存使用结束后还可以释放。 默认构造函数:调用时无须提供参数的构造函数。

标准类库:标准类库就是类的标准库。编程语言的标准库是该语言的每种实现中都按例提

供的库。在某些情况下,编程语言规格说明中会直接提及该库;另一些情况下,标准库的内容由编程社区中的非正式惯例决定。 字符串类string:对字符串进行处理的类 线性拟合:已知某函数的若干离散函数值{f1,f2,…,fn},通过调整该函数中若干待定函数f(λ1,

λ2,…,λm), 使得该函数与已知点集的差别(最小二乘意义)最小。如果待定函数是线性,就叫线性拟合或者线性回归(主要在统计中)。

实验四

/*1) 定义一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,

看看会有什么问题,把 age改为公有成员变量,还会有问题吗?编程试试看。*/ #include using namespace std; class Animal{ public: int age; Animal(){} ~Animal(){} };

class dog:public Animal{ public: dog(){} ~dog(){} void SetAge(){ cout<<\"please input the age:\"; cin>>age; }

void showage(){ cout<<\"the age of it is:\"<int main(){ dog d; d.SetAge(); d.showage(); return 0; }

/*2) 定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,定义该派生类的对象,

观察构造函数和析构函数的执行情况。*/ #include using namespace std; class BaseClass{ public:

BaseClass(){cout<<\"基类的构造函数正在执行....\"<class DerivedClass:public BaseClass{ public: DerivedClass(){cout<<\"派生类的构造函数正在执行....\"<void main(){ DerivedClass d; return ; }

/*3) 定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,

由此派生出自行车(bicycle)类,汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,

汽车(motorcycle)类有座位数(SeatNum)等属性。从bicycle和motorcycle派生出摩托车(Motorcar)类, 在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle 设置为虚基类,会有什么问?编程实验及分析原因。*/ #include using namespace std; class vehicle{ public:

vehicle(int MaxSpeed0,int Weight0):MaxSpeed(MaxSpeed0),Weight(Weight0){} int MaxSpeed,Weight; void Run(){cout<<\"start running...\"<class bicycle:virtual public vehicle{ public: bicycle(int MaxSpeed0,int Weight0,int Height0):vehicle(MaxSpeed0,Weight0){Height=Height0;} int Height;

void show(){cout<<\"Height=\"<class motorcycle:virtual public vehicle{ public: motorcycle(int MaxSpeed0,int Weight0,int SeatNum0):vehicle(MaxSpeed0,Weight0){SeatNum=SeatNum0;} int SeatNum; void show(){cout<<\"SeatNum=\"<class Motorcar:public bicycle,public motorcycle{ public: Motorcar(int MaxSpeed0,int Weight0,int Height0,int SeatNum0):vehicle(MaxSpeed0,Weight0),

bicycle(MaxSpeed0,Weight0,Height0),motorcycle(MaxSpeed0,Weight0, SeatNum0){} int MaxSpeed0;int Weight0; void show(){vehicle::show();motorcycle::show();bicycle::show();} };

void main(){ Motorcar m(1,2,3,4); m.show(); m.Run(); m.Stop(); return ; }

实验五

/*1) 定义Point类,有坐标x,y两个私有成员变量;对Point类重载\"+\"(相加)、\"-\"(相减)和\"==\"(相等)运算符,实现对坐标的改变,

要求用友元函数和成员函数两种方法实现。对Point类重载<<运算符,以使得代码 Point p; cout< class Point{ public: Point(int x=0,int y=0):x(x),y(y){} Point operator+(const Point &p2) const; Point operator-(const Point &p2) const; Point operator==(const Point &p2) const; void show()const; friend Point operator+(Point &p1,Point &p2); friend Point operator-(Point &p1,Point &p2); friend Point operator==(Point &p1,Point &p2); friend ostream & operator<<(ostream &out,const Point &p);

private: int x,y; };

Point Point::operator +(const Point &p2)const{ return Point(x+p2.x,y+p2.y); }

Point Point::operator -(const Point &p2)const{ return Point(x-p2.x,y-p2.y); }

Point Point::operator ==(const Point &p2)const{ return Point(x==p2.x,y==p2.y); }

Point operator+(Point &p1,Point &p2) { return Point(p1.x+p2.x,p1.y+p2.y); }

Point operator-(Point &p1,Point &p2) { return Point(p1.x-p2.x,p1.y-p2.y); }

Point operator==(Point &p1,Point &p2) { return Point(p1.x==p2.x,p1.y==p2.y); }

ostream & operator<<(ostream &out,const Point &p){ out<<\"(\"<int main(){ Point p1(8,10),p2(6,3),p3,p(1,2);

cout<<\"第一个坐标为:\"; cout</*2) 定义一个车(vehicle)基类,有虚函数Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,

它们都有Run、Stop等成员函数。在主函数中用不同的方法调用Run、Stop成员函数,观察这些函数的执行结果,思考如何实现动态多态性,

如果Run、Stop没有被定义为虚函数,执行结果会怎样,把结果和分析写入实验报告。*/

#include using namespace std; class vehicle{ public:

vehicle(){}

virtual void Run() const {cout<<\"vehicle start running...\"<class bicycle: public vehicle{ public:

void Run() const {cout<<\"bicycle start running...\"<class motorcar:public vehicle{ public:

void Run() const {cout<<\"motorcar start running...\"<void fun(vehicle*ptr){ ptr->Run(); ptr->Stop(); }

void main(){ vehicle v; bicycle b; motorcar m; fun(&v); fun(&b); fun(&m); return ; }

多态:是指同样的消息被不同类型的对象接收时导致不同的行为; 实现多态的方法:静态绑定和动态绑定;

虚函数:是动态绑定的基础,虚函数必须是非静态的成员函数;

运算符重载:是对已有的运算符赋予多重含义,使同一个运算符作用于 不同类型的数据时导致不同的行为; 前++:单目运算符,重载函数没有形参;

后++:单目运算符,重载函数需要有一个整型形参;

实现运算符重载的方式:运算符重载为成员函数和非成员函数。

实验六

//1) 使用函数模板实现一个求3个数最大值的函数,并完成测试。 #include using namespace std; template T max(T x,T y,T z) {

if(x>y) { if(x>z) return x; else return z; } else { if(y>z) return y; else return z; } }

void main() { cout<<\"max(6,9,2)最大值为:\"<//2) 编写程序,用二进制方式打开指定的一个文件,在每一行前加行号 #include #include using namespace std; int main(){ ofstream file; char array[6]={'1','\\n','2','\\n','3','\\n'}; file.open(\"test.dat\ file.write(reinterpret_cast(array),sizeof(array)); file.close(); ifstream is(\"test.dat\"); if(is){ char ch; while((ch=is.get())!=EOF) cout<

因篇幅问题不能全部显示,请点此查看更多更全内容

Top