c++实例化对象

今天看到c++实例化对象,有点懵了。Activity_Log the_log (theLogPtr, Tree->GetBranch());这是那一段小代码,开始没看懂。java看习惯了总喜欢new一个对象。c++直接类名 + 对象名(如果有构造函数定义就变为 类名 + 对象名())。c++动态分配内存的时候才需要new一个对象,哎~我还是那么水

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

 1 #include <iostream>
 2 #include <string>
 3 #include "header1.h"
 4 
 5 using namespace std;
 6 
 7 fruit::fruit(double price, string name){
 8     this->name = name;
 9     this->price = price;
10 }
11 void fruit::show(){
12     cout<<"the fruit's name is:"<<this->name<<" ,the price is:"<<this->price<<endl;
13 }

test.cpp

 1 #include <iostream>
 2 #include "header1.h"
 3 
 4 using namespace std;
 5 
 6 
 7 int main(){
 8     fruit apple(5.6, "apple");//这里实例化一个对象不需要new,直接类名+对象名就好了
 9     apple.show();
10 
11     return 0;
12 }

原文地址:https://www.cnblogs.com/luckygxf/p/4026464.html