03.C ++ classes and objects

A new feature, C ++ structure

C language structure does not allow the function, definition of the structure of the variables is: struct 结构体名结构体变量名.

In C ++, the members of the structure can have a function, a variable definition of the structure can be used: 结构体名结构体变量名, struct关键字可以不书写.

Example (book202.c)

/*
 * 程序名:book202.cpp,此程序用于演示C++结构体的新特征。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

struct st_girl
{
  char name[50];     // 姓名
  int  age;          // 年龄
  int  height;       // 身高,单位:厘米cm
  char sc[30];       // 身材,火辣;普通;飞机场。
  char yz[30];       // 颜值,漂亮;一般;歪瓜裂枣。
  int  show();       // 声明结构体成员函数show,用于显示其它成员变量的值。
};

int main()
{
  st_girl stgirl;      // struct关键定可以不书写
  memset(&stgirl,0,sizeof(stgirl));
  strcpy(stgirl.name,"西施");
  stgirl.age=22;
  stgirl.height=168;
  strcpy(stgirl.sc,"火辣");
  strcpy(stgirl.yz,"漂亮");
  
  stgirl.show();  // 调用结构体的成员函数
}

int st_girl::show()   // 结构体st_girl成员函数的定义
{
  printf("name=%s,age=%d,height=%d,sc=%s,yz=%s\n",name,age,height,sc,yz);
}

running result

Here Insert Picture Description

Two, C ++ classes and objects

Let's do a test, modify the contents of book202.cpp.

int st_girl::show()   // 结构体st_girl成员函数的定义

Changed

int st_girl::show1()   // 结构体st_girl成员函数的定义

Modify and then compile.

Here Insert Picture Description

The figure above, the text in the box "is not declared in the class st_girl"? I obviously written by a structure, how to become a class?

C ++ look into the structure of the class (class), class members can be variables and functions, variable definitions out by class also has a specific address, called an object.

Example (book205.cpp)

/*
 * 程序名:book205.cpp,此程序演示用类的基本概念。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

class CGirl    // 定义超女类
{
public:
  char m_name[50];  // 姓名
  int  m_age;       // 年龄
  int  m_height;    // 身高,单位:厘米cm
  char m_sc[30];    // 身材,火辣;普通;飞机场。
  char m_yz[30];    // 颜值,漂亮;一般;歪瓜裂枣。
  int  Show();      // 申明显示超女基本信息的成员函数
};

int main()
{
  CGirl Girl;   // 实例化一个Girl对象

  // 访问对象的成员变量,进行赋值
  strcpy(Girl.m_name,"武则天");
  Girl.m_age=28;      
  Girl.m_height=168;
  strcpy(Girl.m_sc,"火辣");
  strcpy(Girl.m_yz,"漂亮");

  Girl.Show();   // 调用对象的成员函数
}

int CGirl::Show()  // 显示超女基本信息的成员函数体
{
  printf("姓名:%s,年龄:%d,身高:%d,身材:%s,颜值:%s。\n",m_name,m_age,m_height,m_sc,m_yz);
}

running result

Here Insert Picture Description

Explain book205.cpp.

class is C ++ keywords used to define the class, just like the structure of sturct .

public is the C ++ keywords, you just ignore it, and then later to explain, so first copy on the line.

Class member variables and member variables of the same structure, but also the data type and name.

int
CGirl :: Show () member function is defined in the class, in a function with the class name and two colons, indicating that the function is a member of this class, the return value of grammar class member functions, parameters and the same general function.

In C ++, using the class definition of a class called a variable created (or instantiate) an object member variable called the class attribute (Property), a method called member function (method) of the class.

The life cycle of the same scope and with the object member variables and member functions of the class scope and life cycle.

Third, the array of objects

You can create an array of class objects, just as the same array of structures.

  CGirl Girl[10];    // 定义10个超女类数组
  strcpy(Girl[0].m_name,"杨玉环");
  Girl[0].m_age=18;
  ......
  strcpy(Girl[9].m_name,"陈圆圆");
  Girl[9].m_age=21;

Fourth, the pointer to the object

Class is a custom data type, the object is a memory variable, there is the memory address, of course, also has a pointer to the class, the same structure as a pointer. as follows:

CGirl queen;
CGirl *pst=&queen;

Members can access the object by class pointer, the method of writing the same structure.

(*pointer).memberName

or:

pointer->memberName

The above is equivalent to two way, we usually behind writing, so more intuitive.

Fifth, the object as a parameter function

Like the structure, the object can be passed as a parameter to the function, the best way is to pass the address of the object.

Sixth, take up memory initialization and size of the object

According to our previous experience, the definition of variables to be initialized before use, basic data types in C can be directly assigned 0, initialization string and structures using memset function, then the class of objects it? Memset to initialize the object can not be used, the specific approach we introduce later.

Objects can use the sizeof operator to obtain the size of the memory-intensive, however, in the actual development, programmers do not care about the object memory size.

VII Summary

At this stage, as a class member function of the structure, different definitions of keywords and syntax, using exactly the same method.

You might think that the class seems to be no use, no class can be alive and well, this is not necessarily because I use the simplest possible way to introduce the basics of the class, if the actual development of the scene moved to textbooks, we may We can not accept, in the actual development, some kind of code is very long, there are several pages of the class declaration, not including members of the body of the function.

We hope to keep a good attitude, step by step learning.

Eight, OOP (Object Oriented Programming, OOP)

Class is a generic concept, C ++, Java, C # , PHP
many other programming languages are support classes, you can create objects by class. Because C ++, Java, C #, PHP
and other languages support classes and objects, so use programs written in these languages is also known as object-oriented programming, these languages also known as object-oriented programming language. Because the C language does not support the concept of classes and objects, known as process-oriented programming language, a little bullied.

In the C language, we will reuse or code having a function as a function package, in C ++, the multi-layer package, is the class (class), do not underestimate the type (class) of this layer package it has many features, greatly improve the efficiency of development programmers.

Object-oriented programming does not have any advantage in the efficiency of program execution, its main purpose is to facilitate the organization and management of the code programmer, quickly combing programming ideas.

Nine, Homework

1) programming, testing an array of objects, object pointer, a function of the object as a parameter of usage.

2) reference book205.cpp program, add a member function Level, Level function based on the information super woman, super woman judge level, the level of criteria as follows:

Concubine: (1) the age of 18-25 years of age; (2) Height 165-178cm; (3) hot body; (4) the value of beautiful color.

Ladies: (1) age 18-30 years; (2) height 160-165cm; (3), or ordinary hot body; (4) the value of the general color.

Fatigues: (1) the age of 35-40 years of age; (2) Height 155-165cm; (3) the general body or the airport; (4) color values ​​in general.

Ten, Copyright

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 53 original articles · won praise 6 · views 1811

Guess you like

Origin blog.csdn.net/m0_45133894/article/details/104667697