C++使用对象指针
2015-10-03 13:33
393 查看
//定义结构
Box.h:
//定义volume方法
t1.cpp:
test.cpp:
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/C++头文件的作用和用法
- C/C++头文件的作用和用法
- RGB转jpg使用libjpeg库实现
- 双链表的实现C++封装
- C/C++语言中的函数参数传参三种对比
- c++ primer 练习5.25
- C++学习(一)-纯虚函数和抽象类(4)
- 单链表的实现C++封装
- c++的引用
- C2第一次作业
- C++学习(一)- 虚函数和重载函数的区别(3)
- 【C++】c++复数类Complex
- 指针做函数参数——高效C语言
- 【Cpp】删除字符串空格
- Bjarne Stroustrup announces C++ Core Guidelines
- 顺序表的实现C++封装
- c++ primer 练习5.22
- c语言的正则表达式
- c++ primer 练习5.21
- C语言中函数参数为什么是由右往左入栈的?