1 / 18
文档名称:

C 面向对象程序设计实验报告.docx

格式:docx   大小:166KB   页数:18页
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

C 面向对象程序设计实验报告.docx

上传人:mh900965 2017/12/31 文件大小:166 KB

下载得到文件列表

C 面向对象程序设计实验报告.docx

文档介绍

文档介绍:——面向对象程序设计
学院:计算机与信息工程学院
班级:电子信息工程1401班
实验一
实验题目
定义盒子Box类,要求具有以下成员变量:长、宽、高分别为length,with,high;具有一个静态成员变量:盒子和个数number;具有以下成员函数:1)构造函数和析构函数;2)计算盒子体积的函数;3)计算盒子的表面积的函数;4)显示长、宽、高、体积和表面积函数;5)显示盒子个数的静态成员函数。编写main函数进行测试。
实验代码

#include<iostream>
using namespace std;
class Box
{
private:
double length,width,high;
static int number;
public:
Box(double a=0,double b=0,double c=0)
{
length=a;
width=b;
high=c;
number++;
}

~Box()
{
number--;
}
double volunm();
double area();
void display();
int getnumber();
};

#include<iostream>
#include""
using namespace std;
double Box::volunm()
{
return length*width*high;
}
double Box::area()
{
return 2*(length*width+length*high+width*high);
}
void Box::display()
{
cout<<"长:"<<length<<"米"<<endl;
cout<<"宽:"<<width<<"米"<<endl;
cout<<"高:"<<high<<"米"<<endl;
cout<<"体积:"<<volunm()<<"立方米"<<endl;
cout<<"表面积:"<<area()<<"平方米"<<endl;
}
int Box::getnumber()
{
return number;
}

#include<iostream>
#include""
using namespace std;
int Box::number=0;
void main()
{
Box a(10,10,10);
();
cout<<"个数:"<<()<<"个"<<endl;
}
实验结果

这个题目主要是考构造函数、析构函数和静态成员变量的使用。在盒子的类中,声明了长、宽、高三个私有成员,和一个number的静态成员变量。这道题的关健是不能直接访问静态成员变量,必须访问包含静态成员变量的函数才可以对其进行操作,且静态成员必须赋初值。
实验二
实验题目
定义一个车基类Vehicle,含私有成员speed、weight。派生出自行车类Bicycle,增加high成员;派生出汽车类Car,增加seatnum(座位数)成员。每个类都有:各自的构造函数、设置(如setSpeed等)和返回每个成员变量(如getSpeed等)的函数、显示名称和参数的函数。编写main函数进行测试。
实验代码

#ifndef VEHICLE_H
#define VEHICLE_H
#include<iostream>
using namespace std;
class Vehicle
{
private:
double speed,weight;
public:
Vehicle(double a=0,double b=0)
{
speed=a;
weight=b;
}
void setspeed(double a);
void setweight(double b);
double getspeed();
double getweight();
};
#endif

#include"”
using namespace std;
void Vehicle::setspeed(double a)
{
speed=a;
}
void Vehicle::setweight(double b)
{
weight=b;
}
double Ve