Write a C + + program when you are free

Source: Internet
Author: User

The recent work has become a lot easier, with some spare time, ready to pick up C + + in college, and research algorithms:

The first C + + program: Calculate the area of the circle, but also look at other blogs written by the C + + program.

#include <iostream> #define PI 3.14using namespace Std;class Circle {public:circle (double radius) {radius = radius;} Constructor Circle (Circle &c);  Copy constructor: Copies the individual members of the existing object to the corresponding members of the newly defined object. ~circle () {}//destructor double Getarea () const;//const do what? A constant member function cannot change the member variable of an object, nor can it call a class in any non-const member function private:double radius;};/ /class need to have ";" in the back. ; Circle::circle (Circle &c) {C.radius = Radius;} Double Circle::getarea () const {return pi*radius*radius;} int main () {int r;cout<< "The radius of the input circle: r=";cin>>r; Circle Mycircle (R);cout<< "the area of the circle is" <<mycircle.getarea () <<endl;system ("pause"); return 0;}

For a long time not to write C + +, there have been a lot of incomprehensible places:

1.class after completion to have ";";

2.const the use of the method to forget, the details of the examination

http://blog.csdn.net/Eric_Jo/article/details/4138548
3. Copy constructor: This is still not thoroughly understood. The key is not practical ah. 4. The use of system ("pause") is turned off when using visual Stuido to always see the results of execution.
the role of the const
Const is a keyword of C language that is protected from the effects of changes outside of it! Ability to modify variables, parameters, return values, and even function bodies. Const can improve the robustness of your program, and you will only be able to use it wherever you want.
(i) const modifier parameters. Const can only modify input parameters.
1, assuming that the input parameters are pointer-type, with the const modifier to prevent accidental changes in the pointer.
2, assuming that the value of the use of the method of transmission, without a const, because the function itself to generate a temporary variable copy of the argument.
3, non-internal data type of the parameters, it is necessary to temporarily copy the parameters, and the construction of temporary objects, destruction, replication is more time-consuming, it is recommended to use the pre-const reference method to pass non-internal data types. The internal data type does not need to be passed by reference.
(ii) the const modifier function return value.
1, the function returns a const pointer, indicating that the pointer cannot be modified, only the pointer can be assigned to the const modifier of the same type pointer variable.
2, the function return value is the value pass, the function will assign the return value to the external temporary variable, with the const meaningless! Both internal and non-internal data types.
3, the function uses the reference way returns the occasion to be not many, only then in the class's assignment function, the goal is realizes the chain expression.
(c) const+ member functions. Any function that does not alter the data member should be declared as a const type, assuming that the const member function changes the data member or invokes another function to alter the data member, the compiler will give an error!
class Stack
{
Public :
int GetCount (void) const;
Private:
int m_num;
};
int Stack::getcount (void) const
{
m_num++;
}
Compiler output error message: Error c2166:l-value specifies const object.
(d) A const modifier variable that indicates that the variable cannot be altered.
1. const char *P indicates that the pointing content cannot be changed
2, char * const P, is to declare p as a constant pointer, its address can not be changed, is fixed, but its content can be changed.
3, such a const pointer is the first two combinations, so that the point of the content and address can not be changed.
const Double pi = 3.14159;
Const double *const pi_ptr = &pi;

copy constructor:

Write a C + + program when you are free

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.