First knowledge of C + + class

Source: Internet
Author: User

Recognize the class, first know the object.

1. Object: An object that identifies a unique entity in the real world. For example, a student, a table, a circle ... An object has a unique identity, state, and behavior. the state of an object is represented by data fileds, which is also a property of the object, which is represented by variables and arrays in the program implementation. The behavior of an object is represented by a set of functions , and invoking a function on an object is the request object to perform a task.

2. Class: A class is a generic template for objects of the same type. In a C + + class, define a data field with a variable and define the behavior with a function. At the same time, a class also provides special functions-constructors (constructor), which are called when new objects are created.

1 classCircle2 {3  Public:4     //The radius of this circle-----data filed5     Doubleradius;6 7     //Constructor function on argument8Circle ()//---default. Name is same to the class name9    {TenRadius =1.0;  One     } A     -    //Constructor function on argument -Circle (DoubleNewradius)//no return type, include void the     { -Radius =Newraduis;  -       } -  +     //function -    DoubleGetarea () +     { A         returnradius*radius*3.1415926; at      }          -};

3. Constructors

The name of the constructor for a class is the same as the class name. Constructors can be overloaded, as long as their signatures (prototypes) are not the same.

A class usually has a constructor that has no arguments, such as circle (), and if no constructor is declared in a class, C + + implicitly declares an empty constructor that does not have an argument.

The constructor does not have a return type, that is, there is no type keyword before the function name.

Its role is to create objects and initialize them.

4. Objects

Syntax for creating objects:

ClassName VariableName;  ClassName variableName (arguments); Constructors with parameters Create objects for example: Circle circle1; Circle Circle2 (10.2);

Access objects: When an object is created, you can use the object member access operator (., dot operator) to access the object's data and the functions that call the object.

ObjectName.dataFiledobjectName.function (argument)

5. Keyword Public

Indicates that all data fields, constructors, and ordinary member functions can be accessed through class objects.

6. Anonymous objects (anonymous object): can be created as an anonymous object when an object needs to be created and used only once.

  

Parameterless constructor creates an anonymous object classname ()//with a parameter constructor creates an anonymous object classname (arguments)//Creates an object and copies its contents to Circle1circle1 = Circle (); circle2 = Circle (5.0);

  

Attention:

1) Similar to normal variables, you can use the assignment operator (=) to replicate between objects.

Circle1 = Circle2; Copying the contents of the object Circle2 to Circle1,circle1 and Circle2 is still two different objects;

2) The same as the array name. Once an object is declared, it is unique, is a specific object, cannot be re-assigned to it, let him represent another object.

First knowledge of C + + 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.