Detailed description of C++ classes

Article Directory


Examples of Super Girls have been playing for a long time. To learn from them and leave them temporarily, I will use examples of actual project development to explain more about classes.

File operations have been learned in the C language basics. In practice, in order to improve efficiency, I will encapsulate file operations into a class with the following declarations:

// File Action Class Declaration
class CFile
{
private:
  FILE *m_fp;        // field name pointer
  bool  m_bEnBuffer; // Whether buffers are enabled, true-enabled; false-not enabled

public:
  CFile();   // Constructor of class
  CFile(bool bEnBuffer);   // Constructor of class

 ~CFile();   // Class destructor

  void EnBuffer(bool bEnBuffer=true);  // Enable or Disable Buffers

  // Open the file with the same parameters as fopen, open successfully true, fail to return false          
  bool Open(const char *filename,const char *openmode);

  // Call fprintf to write data to file
  void Fprintf(const char *fmt, ... );

  // Call fgets to read a line from a file
  bool Fgets(char *strBuffer,const int ReadSize);

  // Close file pointer
  void Close();
};

1. Access rights of class members

C++ Through public, protected, private
Three keywords control access to member variables and member functions. They represent public, protected, and private, respectively, and are called member access qualifiers.Access rights refer to code outside a class that accesses the permissions of its members.

Inside a class, that is, in a member function of the class, whether the member is declared public, protected, or
private, are accessible to each other without restrictions on access rights.

Outside the class (outside the code that defines the class), only members of the public can be accessed through objects, not private, protected
Member of the property.

This section focuses on public and private, and protected will be introduced later.

Members behind private are private, such as m_fp and m_bEnBuffer, until there is public
Only when it appears will it become common; there are no other qualifiers after public, so public
The latter members are all in common.

private
The purpose of the keyword is to better hide the internal implementation of the class, and the exposed interfaces (members accessible through the object) are declared as
public, members that do not want to be known externally, used only within a class, or have no external impact are recommended to be declared as
private.

Members declared private and members declared public can appear private first in any order
Section, you can also start with the public section.If neither private nor Public is written, the default is
private.

In a class body, private and public
It can occur multiple times.The valid range for each part ends when another access qualifier or body appears (the last right curly bracket).

You might say it's easy to set all member variables to public, and indeed, 99.9%
None of these are errors, and I don't think it's inappropriate to do so; however, setting member variables to
private is a software design specification, especially in large and medium-sized projects, or do you want to adhere to this principle as much as possible.

2. Naming of member variables

Most of the member variables start with m_which is a convention, not a grammatical requirement.Starting with m_you can see at a glance that this is a member variable and distinguish it from the parameter names in member functions.

For example, the body of the member function EnBuffer is as follows:

// Enable or Disable Buffers
void CFile::EnBuffer(bool bEnBuffer)
{
  m_bEnBuffer=bEnBuffer;
}

3. Constructors

There are special member functions in the declaration of the CFile class, CFile(), which are constructor s.

  CFile();   // Constructor of class
  CFile(bool bEnBuffer);   // Constructor of class	

The constructor has the same name and class name, does not return a value, cannot be called explicitly, and is automatically executed when an object is created.

Constructors have the following characteristics:

1) The constructor must be a public property.

2) The constructor does not return a value because there are no variables to receive the return value, which is useless even if it is used. Neither the declaration nor the definition precedes the function name with a return value type, even if it is
void is also not allowed.

3) Constructors can have parameters that allow overloading.A class can have multiple overloaded constructors, and the object is created to determine which constructor to call based on the parameters passed.

4) Constructors are widely used in practical development. They are often used to do some initialization work, initialize member variables, etc. Note that the whole class cannot be initialized with memset.

Example

CFile::CFile()   // Constructor of class
{
  m_fp=0;
  m_bEnBuffer=true;
}

CFile::CFile(bool bEnBuffer)   // Constructor of class
{
  m_fp=0;
  m_bEnBuffer=bEnBuffer;  
}

4. Destructors

There is also a special member function in the declaration of the CFile class, CFile(), which is a destructor.

~CFile();   // Class destructor

Destructor names are prefixed by class names with ~, no return value, but can be explicitly called and automatically executed when an object is destroyed for cleanup purposes, such as freeing allocated memory, closing open files, etc. This purpose is important to prevent programmers from making mistakes.

Destructors have the following characteristics:

1) The constructor must be public.

2) The constructor does not return a value because there are no variables to receive the return value, which is useless even if it is used. Neither the declaration nor the definition precedes the function name with a return value type, even if it is
void is also not allowed.

3) Destructors do not allow overloading.A class can only have one destructor.

CFile::~CFile()   // Class destructor
{
  Close();  // Call Close to release resources
}

5. C++ programs are also elegant

Many people say that C/C++ programs are boring, Python programs are elegant, and this person is ridiculous because C/C++ doesn't understand it. If we want, we can write code as elegant and concise as python. In book210.cpp, the main function is extremely concise.

Example (book210.cpp)

/*
 * Program name: book210.cpp, this program demonstrates more knowledge of using C++ classes.
 * Author: C Language Technology Network (www.freecplus.net) Date: 20190525
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

// File Action Class Declaration
class CFile
{
private:
  FILE *m_fp;        // field name pointer
  bool  m_bEnBuffer; // Whether buffers are enabled, true-enabled; false-not enabled

public:
  CFile();   // Constructor of class
  CFile(bool bEnBuffer);   // Constructor of class

 ~CFile();   // Class destructor

  void EnBuffer(bool bEnBuffer=true);  // Enable or Disable Buffers

  // Open the file with the same parameters as fopen, open successfully true, fail to return false          
  bool Open(const char *filename,const char *openmode);

  // Call fprintf to write data to file
  void Fprintf(const char *fmt,... );

  // Call fgets to read a line from a file
  bool Fgets(char *strBuffer,const int ReadSize);

  // Close file pointer
  void Close();
};

int main(int argc,char *argv[])
{
  if (argc !=2) { printf("Please enter the name of the file to be opened.\n"); return -1; }

  CFile File;

  if (File.Open(argv[1],"r")==false) { printf("File.Open(%s)Failed.\n",argv[1]); return -1; }
  
  char strLine[301];

  while (true)
  { // Read each line from a file
    if (File.Fgets(strLine,300)==false) break;

    printf("%s",strLine);   // Show screen what you read from the file
  }
}

CFile::CFile()   // Constructor of class
{
  m_fp=0;
  m_bEnBuffer=true;
}

CFile::CFile(bool bEnBuffer)   // Constructor of class
{
  m_fp=0;
  m_bEnBuffer=bEnBuffer;
}

// Close file pointer
void CFile::Close() 
{
  if (m_fp!=0) fclose(m_fp);  // Close file pointer
  m_fp=0;
}

CFile::~CFile()   // Class destructor
{
  Close();  // Call Close to release resources
}

// Enable or Disable Buffers
void CFile::EnBuffer(bool bEnBuffer)
{
  m_bEnBuffer=bEnBuffer;
}

// Open the file with the same parameters as fopen, open successfully true, fail to return false          
bool CFile::Open(const char *filename,const char *openmode)
{
  Close();  // Close a new file if it is already open before opening it.

  if ( (m_fp=fopen(filename,openmode)) == 0 ) return false;

  return true;
}

// Call fprintf to write data to file
void CFile::Fprintf(const char *fmt,...)
{
  if ( m_fp == 0 ) return;

  va_list ap;
  va_start(arg,ap);
  vfprintf(m_fp,fmt,ap);
  va_end(ap);

  if ( m_bEnBuffer == false ) fflush(m_fp);
}

// Call fgets to read a line from a file
bool CFile::Fgets(char *strBuffer,const int ReadSize)
{
  if ( m_fp == 0 ) return false;

  memset(strBuffer,0,ReadSize);

  if (fgets(strBuffer,ReadSize,m_fp) == 0) return false;

  return true;
}

The effect of book210 is to show the contents of the file line by line, a cat command for the type of linux system.

6. Other knowledge of classes

Other knowledge about classes, including this pointer, static members, friends and so on, is of little significance. I will not introduce it. Time is too precious, there is too much important knowledge to learn, there is no need to waste time on these knowledge points which are neither painful nor practical, so you can look at them later.

7. Variable parameters

We have already introduced the printf, fprintf, sprintf, snprintf functions, which are a set of functions with similar functions and have one common point: the parameter list of functions can be changed.

The function is declared as follows:

int printf(const char *format, ...);        // Format Output to Screen
int fprintf(FILE *stream, const char *format, ...);  // Format Output to File
int sprintf(char *str, const char *format, ...);     // Formatting output to string
int snprintf(char *str, size_t size, const char *format, ...); // Formats the output of a specified length of content to a string

In practical development, our custom functions also use variable parameters to implement functions like these, such as Fprintf member functions of the CFile class.

The C language uses va_start macros, va_end macros, and a series of functions to implement variable parameter functions.

void CFile::Fprintf(const char *fmt,...)
{
  if ( m_fp == 0 ) return;

  va_list ap;
  va_start(arg,ap);
  vfprintf(m_fp,fmt,ap);
  va_end(ap);

  if ( m_bEnBuffer == false ) fflush(m_fp);
}

Take the Fprintf member function of the CFile class as an example.

void CFile::Fprintf(const char *fmt,...);     // Declarations for custom functions with variable parameters

The va_list pointer, va_start macro, and va_end macro are used to analyze parameters and are difficult to understand. You'll use them, but I won't go into details.

  va_list ap;
  va_start(ap,fmt);
  vfprintf(m_fp,fmt,ap);     
  va_end(ap);

The vfprintf function outputs the results of the macro analysis to a file, along with a series of similar functions, as declared below:

// Output Screen
int vprintf(const char *format, va_list ap);
// output to a file
int vfprintf(FILE *stream, const char *format, va_list ap);
// Output to String
int vsprintf(char *str, const char *format, va_list ap);
// Output to a string, the second parameter specifies the length of the output, similar to the snprintf function.
int vsnprintf(char *str, size_t size, const char *format, va_list ap);

8. Homework

1) Write a sample program to test the access rights of class members of a class.

2) Write an example program, test the constructor of a class and its overload, and use gdb to track the execution of the constructor.

3) Write a sample program to test the destructor of a class and use gdb to track the execution of the destructor.

4) Write a sample program to implement the functions of printf, sprintf and snprintf functions, which are declared as follows:

int myprintf(const char *format, ...);
int mysprintf(const char *format, ...);
int mysnprintf(const char *format, ...);

5) Class definitions include the declaration of member variables and functions and the definition of member functions. In practice, we usually put the declaration of public classes in the header file (e.g. _public.h) and the definition of member functions in the program file (e.g. _public.cpp). Modify the book210.cpp program in this way, add the _public.h and _public.cpp programs, and modify the makefile.

9. Copyright Statement

C Language Technology Network original article, reproduced with links to the source, author and text of the article.
Source: C Language Technology Network (www.freecplus.net)
Author: Weinong Youdao

If the article has mistyped words, incorrect content, or other suggestions and opinions, please leave a message to correct, thank you very much!!!

33 original articles published, 2 praised, 1031 visits
Private letter follow

Keywords: C network Python Linux

Added by webzyne on Thu, 05 Mar 2020 04:36:35 +0200