这是一个C++的基础程序,要求用类封装,
#include<iostream>
#include<iomanip>
#include"cube.h"
using namespace std;


void cube::setcube(int a)


{
l=a;
}
int cube::getL()


{
return l;
}

int cube::getV(int vv)


{
v=vv*vv*vv;
return v;
}

int cube::compare(int a, int b)


{
if(a==b)
return 0;
if(a>b)
return
#include<iostream>
#include"cube.h"
using namespace std;

int main()


{ int v1,v2,v3,v4,a1,a2,a3,a4;
cube cube1;
cube cube2;
cube cube3;
cube cube4;
cube1.setcube(5);
cube2.setcube(6);
cube3.setcube(5);
cube4.setcube(4);
a1=cube1.getL();
a2=cube2.getL();
a3=cube3.getL();
a4=cube4.getL();
v1=cube1.getV(a1);
v2=cube2.getV(a2);
v3=cube3.getV(a3);
v4=cube4.getV(a4);
cout<<"V1="<<v1<<","<<"V2="<<v2<<endl;
cout<<"V3="<<v3<<","<<"V4="<<v4<<endl;
if(cube1.compare(v1,v2)==0)
cout<<"The first cube is bigger than the second cube"<<endl;
else if(cube1.compare(v1,v2)==1)
cout<<"The first cube is the same to the second cube"<<endl;
else cout<<"The first cube is samller than the second cube"<<endl;
if(cube1.compare(v1,v3)==0)
cout<<"The first cube is the same to the thrid cube"<<endl;
if(cube1.compare(v1,v4)>0)
cout<<"The first cube is bigger than the fourth cube"<<endl;
return 0;
} 1;
if(a<b)
return -1;
}是做一个水立方,并且能够实现运算符的重载,还能够进行加减乘除的比较
#ifndef CUBE_H
#define CUBE_H


class cube
{
public:
void setcube(int);
int getL();
int getV(int);
int compare(int,int);
private:
int l;
int v;

};
#endif