How are classes and objects defined in C + +?

Source: Internet
Author: User
Tags control characters

in the C + + language , the type of the object is called a class, and the class represents the generality and characteristics of a batch of objects.

A class is an abstraction of an object, and an object is a concrete instance of a class. like the struct in C, we define a struct and then use the struct to define a variable. The same struct can define a number of different structural body variables. So is the class we want to use.

A class is a template for an object, as we do when we build a house, and then we can use the drawing to build many houses. We can use classes that others have already defined. More often, we need to define our own classes as needed.

  Defining classes

  To define a struct body:

  struct Student

  {

  Char name[128];

  int age;

  Char add[128];

  };

The definition format of the class:

  class name

  {

  Private:// member access restriction

member Data ;

member functions;

  Public: The members before the//public setting are private as set above

member Data ;

member functions;

  Protected

member Data ;

member functions;

  };

A class is also a data type, which is a generalized type of data that we define ourselves.

  Private

Private, can only be accessed within the class, outside of the class.

  Protected

Protected, accessible only within the class, outside of the class, accessible in its derived class, and later in the derived class.

  Public

Public, the outside and inside of the class can be accessed.

A class can contain multiple member access limiters, each of which takes effect until the next one is set and, if not set, the default is private. Although the same access limiter can occur multiple times, for the sake of brevity of the code, we should make them appear only once. There are also members of public who are placed in front of the declaration of the class. Because others will only care about your public members when they are watching your code , other hidden members of the outside world are meaningless.

  The addition of the class keyword to C + + still retains the structure, just extending the structure so that it can also define member functions. Unlike classes, the default is public when no access restrictions are declared in the struct.

The following print () function is public in the struct, if no declared access restriction is private in the class.

  Defining objects

  Class Name Object name;

class name object name; // We often use this method to create an object

  member functions of the class

The member function of a class is also called a method of a class, and it is also a function of the same kind as the basic function. It differs from the general function except that it is a member of a class, it is defined inside the class, and it has access control characters.

Private member functions can only be called in this class, the members that need to be called by outsiders are declared as public, and the public member functions become an interface. If we just want a function to be called inside the class by another function, we can declare it as private, because such a function is used to support other functions to implement some functions, which we call a tool function.

we can also not define member functions, but this does not show the function of the class, it is equivalent to C of the structure of the body.

Let's take a look at the structure of the class by an example.

in area fourth, we created a class of object Stu, and through it called the class of two public methods, private and protected methods can not be called in the outside world. In area one, we have completed the declaration and definition of the print () function, but the getage () function simply declares that we have defined it in area third, which is the outside of the class. When we define member functions outside of the class, we need to precede the function name with the class name and scope limiter, which indicates that this member function belongs to this class. Some private member properties are defined in area second. It is a good practice to declare member functions inside a class, and to define member functions outside of the class. This makes the length of the class shorter, the structure level clearer, easy for us to read, and helps to separate the declaration and implementation of the class, from the definition of the class, the user sees only the prototype of the class, which is good for us to achieve data hiding.

when declared and implemented inside a class, the compiler automatically defines these functions as inline functions. such as the print () function. Functions defined outside of a class are not inline functions by default, and we can explicitly add the inline keyword to an inline function when defining a function . such as the getage () function can be defined outside the class by adding the inline keyword declaration to make it an inline function.

original link:http://www.maiziedu.com/wiki/cplus/define/

How are classes and objects defined in C + +?

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.