The difference between c ++ based learning notes --c structure with c ++ class

1.C structure

C language structure is a custom data type, in the structure may contain various different types of data. The following example declares a complex structure:

# include <iostream.h> 
# include <math.h>
struct complex
{ 
	double real;  // 复数的实部
    double imag; // 复数的虚部 
 	void init ( double r, double i) // 给real和imag赋初值 
 	{ 
 		read = r;
 		imag = i; 
 	} 
 	double realcomplex ( ) // 求复数的实部值
  	{ 
  		return real; 
  	} 
    double imagcomplex( ) // 求复数的虚部值
    { 
   		return imag; 
    } 
   double abscomplex( ) // 求复数的绝对值
    { 
    	double t; 
    	t = real *real + imag *imag;
     	return sqrt(t) ;
    } 
} A;

int main( ) 
{
	A .init(1.1, 2.2 ) ; 
	cout<<"real of complex A = "<<A.realcomplex( )<<endl; 
	cout<<"imag of complex A = "<<A.imagcomplex( )<<endl; 
	cout<<"abs of complex A ="<<A.abscomplex ( )<<endl;
	return 0; 
}

Result of the program are as follows:
  Real Complex of A = 1.1
   imag of Complex A = 2.2
   ABS of Complex A = 2.459675

In this declaration of the structure of complex of containing two double-precision data real and imag, represent the number of part of the real and imaginary complex, additionally comprising four functions belonging to the structure of complex of: init (), realcomplex (), imagcomplex () and abscomplex (). init () function for a real and imag initial value, realcomplex (), imagcomplex () and abscomplex () three functions are used to calculate the real part of the complex value, and the absolute value of the imaginary part.

2.C ++ class

Member of the structure and function of the data structure are respectively referred to as data members and function members. In C ++, member functions usually called member functions . In this construction, real and imag are data members, init (), realcomplex () , imagcomplex () and abscomplex () member functions.In C ++, a member of a structure is usually divided into two categories: private members (private) and members of the public (public). Private members (including data and functions) can only be accessed by other members of the structure, and public members (including data and functions) can be accessed within the structure of other members, may be accessed by other portions of the outer structure.
For example, in the embodiment, only variables that need to be real and imag member function to access the structure, can be declared as private members; four member functions need to be called in vitro structure can be declared as public member. as follows:

# include <iostream.h> 
# include <math.h> 
class complex
{ 
private : 
	double real; 
	double imag; 
public: 
	void init( double r, double i) 
	{ 
		real = r;
		imag = i; 
	} 
	double realcomplex ( ) 
	{ 
		return real; 
	} 
	double imagcomplex( )
	{ 
		return imag; 
	} 
	double abscomplex( )
 	{
 		double t ; 
 		t = real *real + imag *imag;
  		return sqrt(t) ;
   }
} A; 

int main ( ) 
	{
    	A.init( 1.1, 2.2) ; 
    	cout<<"real of complex A = "<<A.realcomplex( )<<endl;
    	cout<<"image of complex A ="<<A.imagcomplex( )<<endl; 
     	cout<<"abs of complex A ="<<A.bscomplex( )<<endl;
      	return 0 ; 
     }

As can be seen, the structure and function is substantially the same class.In C ++, then why use class instead of the structure? The reason is that, in the case of default (default), the class members are private, the class provides a default security. This provision consistent with the data object-oriented thinking hidden criteria. Data Hiding class members that are better protected than the average local variables.

Description:

Statement (1) class in the private and public key two times as may appear in any order . However, if all the members of the private and public members are classified together, the program will be more clear. And we should develop all the private members in front of the public member habit, because once a user forgets to use specifier private, because the default is private, which will enable users' data remains protected.

(2) In addition to the private and public, may also be members of the class with another protected keyword will be described. Is known as a protective member explained protected members, it can not be used outside the function, but it may be used by other methods , such as friend function.

(3) data members can be any data type , but not using an automatic (Auto), register (register) or external (extern) will be described.

(4) not to the initial value data members in the class declaration , for example:

class abc
{ 
private : 
	char a ='q'; 	// 错误
 	int b = 33; 	// 错误 
 public: 
 	... 
 } ;

C ++ provides only data members to initial value after the class object definition .

Published 31 original articles · won praise 0 · Views 1265

Guess you like

Origin blog.csdn.net/qq_40754866/article/details/104550108