C + + Instantiation Object

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.