c++实例化对象
2014-10-15 15:34
155 查看
今天看到c++实例化对象,有点懵了。Activity_Log the_log (theLogPtr, Tree->GetBranch());这是那一段小代码,开始没看懂。java看习惯了总喜欢new一个对象。c++直接类名 + 对象名(如果有构造函数定义就变为 类名 + 对象名())。c++动态分配内存的时候才需要new一个对象,哎~我还是那么水
header1.h
header1.cpp
test.cpp
header1.h
#include <iostream> #include <string.h> using namespace std;//这里加上namespace是为使用string时,不用std::string #ifndef __HEADER1__H #define __HEADER1__H class fruit{ private: double price; string name; public: fruit(double price, string name); void show(); }; #endif
header1.cpp
#include <iostream> #include <string> #include "header1.h" using namespace std; fruit::fruit(double price, string name){ this->name = name; this->price = price; } void fruit::show(){ cout<<"the fruit's name is:"<<this->name<<" ,the price is:"<<this->price<<endl; }
test.cpp
#include <iostream> #include "header1.h" using namespace std; int main(){ fruit apple(5.6, "apple");//这里实例化一个对象不需要new,直接类名+对象名就好了 apple.show(); return 0; }
相关文章推荐
- C++和JAVA实例化对象的区别
- 对象数组在Java & C++ 中实例化的区别
- C++对象实例化及调用
- C++实例化对象易混淆总结
- More Effective C++ 阅读笔记(十二)--怎样限制实例化对象的数量
- C++实例化对象数组
- c++实现在实例化时为每个对象添加唯一ID
- 对C++对象实例化的测试
- c++在调用类的时候不一定非得实例化对象哦,有时候你不写系统会为你默认生成一个临时实例对象哦~
- c++中实例化对象的大小(转)
- c++对象和实例化的区别(new和不用new的区别)
- C++只在栈或堆上实例化对象
- C++单例类对象的实例化和销毁
- C++和JAVA实例化对象的区别
- C++和JAVA实例化对象的区别
- C++中堆栈对象实例化笔记
- 关于C++中用静态成员统计实例化对象个数问题_CVTE技术面问题
- c++实例化对象
- C++ 对象实例化方法
- C++使用new和不使用new关键字实例化对象的区别