/** 将头文件和源代码分开可以提高效率 便于管理
定义类 (数据成员 成员函数 构造函数)
创建对象
*/
//Box.h
class Box{
public:
double length; //数据成员
double width;
double height;
box();//构造函数
Box(double lengthValue,double widthValue,double heightValue);
double volume();//成员函数
};//注意 一定要加上分号 否则编译器无法通过
//Box.cpp
#include <iostream>
#include "Box.h"
using namespace std;
Box::Box(){
length=width=height=1.0;
}
Box::Box(double lengthValue,double widthValue,double heightValue){
length=lengthValue;
width=widthValue;
height=heightValue;
}
double Box::volume(){
return length*width*height;
}
//main.cpp
#include <iostream>
#include "Box.h"
using namespace std;
int main(int argc, char *argv[]){
Box firstBox;
Box smailBox(50.0,60.0,70.0);
cout<<" The Smail Box object is By "
<<smailBox.length<<" by "
<<smailBox.width<<" by "
<<smailBox.height
<<endl;
cout<<"smail Box Volume is "
<<smailBox.volume();
system("PAUSE"); //暂停控制台界面
return EXIT_SUCCESS;//退出程序
}