C++ Classes and Objects | Classes and Objects

C++ classes and objects

The types of objects in C++ are called classes. Classes represent the commonalities and characteristics of a certain batch of objects. Classes are abstractions of objects, and objects are concrete instances of classes. Classes are abstract and do not occupy memory, while objects are concrete. Occupy storage space, this is very important, readers need to keep in mind.

C++ declare class type

A class is a type specified by the user. If you want to use a class type in the program, you must declare it according to your needs, or use a class designed by others. The C++ standard itself does not provide the name, structure and content of the ready-made class. C++ Declaring a class type is similar to declaring a structure type.

C++'s declaration of class type, the general form is as follows

class 类名
{
    
    
 private:私有的数据和成员函数;
 public:公用的数据和成员函数;
};

Private and public are called member access qualifiers. In addition to private and public, there is also a member access qualifier protected. Members declared with protected are called protected members. They cannot be accessed outside the class, but can be derived from the class. Member function access.

When C++ declares a class type, the order of the members declared as private and the members declared as public is arbitrary, either the private part or the public part appears first.

C++ If neither the keyword private nor public is written in the class body, it defaults to private.

In a class body, the keywords private and public can appear multiple times, and the effective range of each part ends when another access qualifier or the end of the class body appears. But it is best to make each member access qualifier appear only once in the class definition body.

Commonly used C++ compilation systems often provide users with class libraries that contain commonly used basic classes for programmers to use. Many programmers also put classes frequently used by themselves or their units in a special class library. Call it directly when used, which reduces the workload of program design.

Case: C++ creates a student class.

class Student //class开头 
{
    
    
  int number;//学号
  char name[10];//姓名
  char sex;//性别
  char address[20];//住址
  void print_Student()
  {
    
    
    cout<<number<<endl;
    cout<<name<<endl;
    cout<<sex<<endl;
    cout<<address<<endl;
  } 
};
Student stu1,stu2;//定义了两个Student类的对象stu1与stu2

C++ classes and objects
More cases can go to the public account: C language entry to proficient

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/112588539