带参数的构造函数
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 class Box 6 { 7 public: 8 Box(int,int,int); 9 int volume(); 10 private: 11 int height; 12 int width; 13 int length; 14 }; 15 16 Box::Box(int h,int w,int len) 17 { 18 height=h; 19 width=w; 20 length=len; 21 } 22 23 int Box::volume() 24 { 25 return(height*width*length); 26 } 27 28 int main(int argc, char** argv) { 29 Box box1(12,25,30); 30 cout<<"The volume of box1 is "<<box1.volume()<<endl; 31 Box box2(15,30,21); 32 cout<<"The volume of box2 is "<<box2.volume()<<endl; 33 return 0; 34 }