Seeing C + + instantiation objects today is a bit of a rip. Activity_log The_log (thelogptr, Tree->getbranch ()); This is the small piece of code, the beginning did not read. Java to see the habit of always like new an object. C + + Direct class name + object name (if there is a constructor definition it becomes the class name + object name ()). C + + Dynamic allocation of memory when you need a new object, hey ~ I still water
Header1.h
#include <iostream>#include<string.h>using namespaceStd//This plus namespace is for use with string, without std::string#ifndef __header1__h#define__header1__hclassfruit{Private: DoublePrice ; stringname; Public: Fruit (DoublePricestringname); voidshow ();};#endif
Header1.cpp
1#include <iostream>2#include <string>3#include"Header1.h"4 5 using namespacestd;6 7Fruit::fruit (DoublePricestringname) {8 This->name =name;9 This->price =Price ;Ten } One voidfruit::show () { Acout<<"The fruit ' s name is:"<< This->name<<", the price is:"<< This->price<<Endl; -}
Test.cpp
1#include <iostream>2#include"Header1.h"3 4 using namespacestd;5 6 7 intMain () {8Fruit Apple (5.6,"Apple");//the instantiation of an object here does not require new, the direct class name + object name is OK9 apple.show ();Ten One return 0; A}
C + + Instantiation Object