C++ class declaration and object creation

Classes are templates for creating objects,A class can create multiple objects,Each object is a variable of class type;The process of creating an object is also called the instantiation of a class.Each object is an instance of a class and has member variables and member functions of the class.

Like structures,A class is just a declaration of a complex data type,Does not take up memory space.The object is a variable of this data type.Takes up memory space.

Class declaration

Classes are user-defined types,If classes are used in the program,Must be declared first,Or use existing classes (classes written by others, classes in the standard library, etc.). The C++ syntax itself does not provide the names, structures, and contents of ready-made classes.

Definition of a simple class:

class student {
  //Member variables
  char * name;
  int age;
  float score;
  //member function
  void say () {
    printf ("The age of%s is%d, the grade is%f \ n", name, age, score);

This example creates a student class, which contains 3 member variables and 1 member function.

class is a keyword in c++,Used to declare a class;Immediately after the class keyword is our custom class name student;surrounded by {} is the class body.You cannot initialize member variables when declaring a class.Assignment is only possible after the object is created.

A class can be understood as a new data type,The name of the data type is student. Unlike basic data types such as char, int, and float,student is a complex data type,Can contain basic types,And there are many more features not found in the basic types.

Note that there is a semicolon (;) at the end of the class declaration, which is part of the class declaration,Indicates that the class declaration is over,It cannot be omitted.

Create object

After the student data type is declared,You can use it to define variables,Such as:

student lilei;//Create object

This statement declares a variable named lilei and a data type of student. with:

int a;//Define integer variables

The statement defines an integer variable and the meaning is similar.The variable lilei is called an object of the student class.

When defining objects of a class,The class keyword is optional.But out of habit we usually omit the class keyword, for example:

class student lilei;//correct
student lilei;//equally correct

When creating an object of a class,In addition to being able to define a single variable,You can also define an array or pointer.E.g:

student all_student [100];
student * pointer;

The first statement defines an all_student array with 100 elements, each of which is of type student. The second statement defines a pointer of type student, which can point to a variable (object) of type student, which is used in the same way as a normal pointer.

Creating objects while declaring classes

Similar to a struct, you can declare the class first,Then create the object,You can also create objects while declaring a class.As follows:

class student {
  //Member variables
  char * name;
  int age;
  float score;
  //member function
  void say () {
    printf ("The age of%s is%d, the grade is%f \ n", name, age, score);
} stu1, stu2;

At this time you can also omit the class name,Create objects directly.As follows:

class {
  //Member variables
  char * name;
  int age;
  float score;
  //member function
  void say () {
    printf ("The age of%s is%d, the grade is%f \ n", name, age, score);
} stu1, stu2;

Define objects directly,It is legal and allowed in C++, but it is rarely used.It is also discouraged.

A complete example:

//Class can be defined outside all functions
class student {
public://Variables included in the class
  char * name;
  int age;
  float score;
public://class contains functions
  void say () {
    printf ("The age of%s is%d, the grade is%f \ n", name, age, score);
int main () {
  //create object
  student stu;
  stu.name="Xiao Ming";
  stu.say ();
  //define pointer
  student * pt_stu =&stu;
  pt_stu->name="Li Lei";
  pt_stu->say ();
  return 0;

operation result:

Xiao Ming's age is 15 and his grade is 92.500000
Li Lei's age is 16 and his grade is 80.000000

public is a keyword in c++,Used to decorate member variables and member functions,Indicates that they are public.We will explain public in detail in the next section, just need to know here,Only member variables and member functions behind public can be accessed by the created object.Like the example at the beginning of this section,Without public, no members can be used after the object is created.

The main function first creates an object stu, and then defines a pointer variable of type student.It can be found,Similar to a struct, an object accesses member variables and member functions through the member selector "."And pointer variables access members through the pointer operator "->".

Object pointers are similar to structure pointers.

Note:The object pointer points to a specific object,Not classes.The following is wrong:

student * pt;
pt=& student;

c++ object member reference

You often need to access members of an object in your program.There are three ways to access members in an object:

Accessing members in an object by object name and member operator; Accessing members in an object through a pointer to the object; Members of an object are accessed through its reference variable.

Accessing members in an object by object name and member operator

For example, the following statement can be written in the program:

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

Indicates that the integer 1001 is assigned to the data member num in the object stud1. Where "." Is a member operator,Used to qualify members,Indicate which member of the object you are accessing.Note that you cannot just write member names and ignore object names.

The general form of the members in the access object is:

Object name.member name

Not only can a public data member of an object be referenced outside the class,And you can also call public member functions of the object,But it must also indicate the object name,Such as:

 stud1.display ();//Correct, call public member function of object stud1
  display ();//Error, no display function indicating which object

Since no object name was specified,Compile the display as a normal function.It should be noted whether the members accessed are public (public) or private (private), only public members can be accessed, but not private members. If num is defined as a private data member,The following statement is wrong:

 stud1.num=10101;//num is a private data member,Cannot be quoted from outside

Only public member functions can be called outside the class.There should be at least one public member function in a class,As an external interface,Otherwise, you cannot perform any operations on the object.

Accessing members in an object through a pointer to the object

Earlier, we introduced pointers to struct variables (click for details:pointers to struct variables), and you can use pointers to refer to members in structs.Accessing members in an object with a pointer is similar.If there are the following blocks:

class time
public://Data members are public
  int hour;
  int minute;
time t, * p;//Define object t and pointer variable p
p =&t;//make p point to object t
cout<<p->hour;//output member hour in the object pointed to by p

With p pointing to t,p->hour, (* p) .hour and t.hour are equivalent.

Accessing members in an object through its reference variable

If a reference variable is defined for an object,They share the same memory cell.They are actually the same object,It's just a different name.So you can access the members of the object through reference variables.

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

 time t1;//define object t1
  time & t2=t1;//Define time class reference variable t2 and initialize it to t1
  cout<t2.hour;//member hour in output object t1

Because t2 and t1 occupy the same storage unit (that is, t2 is an alias of t1), t2.hour is t1.hour.

  • Previous Detailed ajax + jtemplate implementation of dynamic paging
  • Next Android common dialog usage example analysis