C + + Note 007: Easy-to-make error model-why member functions are required in a class

Source: Internet
Author: User

First look at the source code, in the VS2010 environment can not be compiled through, in the VS2013 environment to compile through, and can run, but the results are not the results we expect.

When the object C1 is initially defined by the Mycircle class, the object is allocated memory space,R is not initialized, its value is garbled,pi is 3.1415926,area garbled.

The statement "CIN>>C1.R" is assigned a value of C1.R, assuming 10, and then performing "Cout<<c1.area<<endl", we see that the execution of cout is from memory The value of C1.area in space , this value is determined when defining the object is a garbled value, here will not execute "double area=pi*r*r" statement, so the output is garbled.

If you want to get the correct result, you need a member function, which is calculated in the member function.

C + + Note 007: Easy-to-make error model-why member functions are required in a class

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.