C++中对象指针的使用

时间:2014-07-12 17:25:50   收藏:0   阅读:152

//定义结构

Box.h:

#ifndef BOX_H
#define BOX_H
struct Box{
	double length;
	double width;
	double height;
	double volume();
};
#endif

//定义volume方法
t1.cpp:

#include "box.h"
double Box::volume(){
return length*width*height;
}

test.cpp:

#include <iostream>
using std::cout;
using std::endl;
#include "box.h"
int main(){
	Box box={70.0,60.0,40.0};
	Box* pBox=&box;
	cout<<endl
	<<"Volume is:"<<pBox->volume()<<endl;
	Box* pdBox=new Box;
	pdBox->height=pBox->height+5.0;
	pdBox->length=pBox->length-2.0;
	pdBox->width=pBox->width-2.0;
	cout<<"Volume is:"<<pdBox->volume()<<endl;
	delete pdBox;
	return 0;	
} 




 

C++中对象指针的使用,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!