Explain the declarations of classes in C + + programming and the references of object Members _c language

Source: Internet
Author: User

Declaration of C + + classes and creation of objects
A class is a template for creating objects, a class can create multiple objects, each object is a variable of a class type, and the process of creating an object is also called a class instantiation. Each object is a concrete instance of the class (Instance) that owns the member variables and member functions of the class.

Like structs, a class is simply a declaration of a complex data type that does not occupy memory space. The object is a variable of this data type of class that occupies memory space.
Declaration of a class

A class is a user-defined type, and if you want to use a class in your program, you must declare it, or use an existing class (a class that someone else writes, a class in a standard library, and so on), and the C + + syntax does not itself provide the name, structure, and content of the

The definition of a simple class:

Class student{
  //member variable
  char *name;
  int age;
  float score;
  member function
  void Say () {
    printf (age of%s is%d, result is%f\n, name, age, score);

This example creates a student class that contains 3 member variables and 1 member functions.

Class is a keyword in C + + that declares a class, and immediately after the Class keyword is our custom class name Student; The class body is surrounded by {}. You cannot initialize a member variable when declaring a class, and you cannot assign a value until the object is created.

Class can be understood as a new data type with a name of Student. Unlike basic data types, such as char, int, and float, Student is a complex data type that can contain basic types and many features that are not in the basic types.
Note that there is a semicolon at the end of the class declaration (;), which is part of the class declaration, which indicates that the class declaration is over and cannot be omitted.
Creating objects

Once you have declared the Student data type, you can use it to define variables, such as:

Student Lilei; Creating objects

The statement declares a variable whose name is Lilei and the data type is Student. This and:

int A; Defining Shaping variables

The statement defines an integer variable that expresses a similar meaning. And Lilei This variable we call the object of the Student class.

Class keywords are not available when you define objects for classes. But out of habit we usually omit the class keyword, for example:

Class Student Lilei; Correct
Student lilei//equally correct

When you create an object for a class, you can define an array or pointer in addition to defining a single variable. For example:

Student all_student[100];
Student *pointer;

The first statement defines a all_student array that has 100 elements, each of which is a student type. The second statement defines a pointer pointer of the Student type, which can point to a variable (object) of the Student type, as in the same way as a normal pointer.
To declare a class while creating an object

Like the structure body (struct), you can declare a class before you create it, or you can create an object while declaring a class. As shown below:

Class student{
  //member variable
  char *name;
  int age;
  float score;
  member function
  void Say () {
    printf (the age of%s is%d, the result is%f\n ", name, age, score);

At this point you can also omit the class name and create the object directly. As shown below:

  //member variable
  char *name;
  int age;
  float score;
  member function
  void Say () {
    printf (the age of%s is%d, the result is%f\n ", name, age, score);

The direct definition of the object, which is legal and allowed in C + +, is rarely used and is not advocated.

A complete example:

#include <stdio.h>
//classes can define the class
://class to contain variable
  char *name;
  int age;
  float score;
Public://class contains function
  void Say () {
    printf ("%s is of%d age, score is%f\n", name, ages, score);
int main () {
  //Create object
  Student stu;
  Stu.name = "Xiaoming";
  Stu.age =;
  Stu.score = 92.5f;
  Stu.say ();
  Define pointer
  Student *pt_stu = &stu;
  Pt_stu->name = "Li lei";
  Pt_stu->age =;
  Pt_stu->score =;
  Pt_stu->say ();
  return 0;

Run Result:

Xiaoming's age is 15, the result is 92.500000
Li Lei's age is 16, the result is 80.000000

Public is a keyword in C + + that modifies member variables and member functions to indicate that they are publicly owned. We'll explain the public in more detail in the next section, only to be aware that only the member variables and member functions after public can be accessed by the object being created. Like the example at the beginning of this section, if you don't use public, you cannot use any members after you create the object.
The main function first creates an object Stu, and then defines a pointer variable of the Student type. It can be found that, like the structure body (struct), an object is passed through the member selector "." To access member variables and member functions, while pointer variables access members through the pointer operator "->".

The object pointer is similar to the structure body pointer.

Note: The object pointer is pointing to a specific object, not a class. The following is the wrong wording:

Student *pt;
PT = &Student;

References to C + + object members
You often need to access the members of an object in your program. There are 3 ways to access members of an object:

    1. Access to a member of an object through an object name and a member operator;
    2. Accesses a member of an object by pointing to the object's pointer;
    3. Accesses a member of an object through a reference variable of an object.

Accessing members of an object through object names and member operators

For example, you can write the following statement in your program:

  stud1.num=1001; Suppose num is defined as a public integer data member

Indicates that integer 1001 is assigned to the data member num in the object stud1. where "." is a member operator that is used to qualify a member to indicate which object is being accessed. Note You cannot write only member names but ignore object names.

The general form of access to members in an object is:
The name of the object. Member name
Not only can you reference the public data members of an object outside of the class, but you can also call the object's public member functions, but you must also indicate the object name, such as:

  Stud1.display (); Correct, call the object STUD1 public member function
  display ();//error, no indication of which object's display function

Because the object name is not indicated, the display is treated as a normal function at compile time. You should be aware that the members you are accessing are public or private (private), and you can access only the members of the community, not private members. If NUM is defined as a private data member, the following statement is incorrect:

  stud1.num=10101; Num is a private data member and cannot be referenced by the outside world

Only public member functions can be called outside of a class. You should have at least one public member function in a class as an external interface, otherwise you cannot do anything with the object.
To access a member of an object by pointing to the object's pointer

A pointer to a struct variable (for more information, a pointer to a struct variable) can be referenced by a pointer to a member in a struct body. The method for accessing members in an object with pointers is similar. If you have the following program segments:

Class time
public://data member is a common
  int hour;
  int minute;
Time t, *p; Defines the object t and pointer variable p
p=&t;//causes P to point to the member of the object that the object T cout<<p->hour;//
output P points to hour

On the premise of P pointing T, P->hour, (*p). Hour and T.hour are equivalent.
To access members of an object through a reference variable of an object

If you define a reference variable for an object, they are all in the same storage unit, and they are actually the same object, only in different names. It is therefore entirely possible to access the members of an object by referencing the variable.

If the time class has been declared and has the following definition statement:

  Time T1; Define the object T1 time
  &t2=t1;//define the time class reference variable t2 and initialize it as
  a member in T1 cout<<t2.hour;//Output Object T1

Since T2 and T1 share the same memory unit (that is, T2 is the alias of T1), T2.hour is t1.hour.

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.