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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值