This part is the main content of C ++ upgrading C, and is also the essence of C ++. C is process-oriented, and C ++ is object-oriented. The most important features of Object-Oriented Programming (OOP) are:
* Abstraction
* Encapsulation and Data Hiding
* Polymorphism
* Inheritance
*CodeReusability
1. abstraction and class:
The class in C ++ is a C ++ tool that converts abstraction into user-defined types (similar to other object-oriented languages, such as Java ), it combines data representation and data manipulation methods into an integer.
Clean the package.
The definition class specification has two parts: class declaration and class method definition.
Class Declaration: Describe the data part as a data member and describe the public interface as a member function (also known as a method.
Class method definition: describes how to implement class member functions.
Simply put, the class Declaration provides a blueprint for the class, while the method definition of the class provides implementation details.
Control Access to class members: As one of the main goals of OOP when hiding data, data items are usually put in the private part, and the member functions that constitute the class interfaces are put in the public part. Otherwise, no
FranceProgramTo call these functions. Generally, Private member functions are used to process implementation details of Non-public interfaces. Sometimes you do not need to use the private keyword, because in C ++, private is the default class object.
Access control.
Now I have learned the class, and I have a better understanding of the difference between the class and the structure: in fact, the class is an extension of the structure. They have many identical features. The only difference is that, the default access control room of the structure is public,
Class is private. Note: struct can also be defined as follows:
Public struct person
{
String name;
Double height;
Double weight;
Public void overweight ()
{
.....
}
}
The definition of a member function is very similar to that of a conventional function, but has two characteristics:
(1) defining a member function is to use the scope resolution operator (: :) to identify the class to which the function belongs.
(2) the class method can be a private component of the category class.
Sample Code:
# Include <iostream>
# Include <cstring>
Using namespace STD;
// Class declaration
Class stock
{
PRIVATE:
Char company [30]; // company name
Int shares; // number of shares held
Double pai_val; // price per share
Double total_val; // The total value of the stock.
Void settol () // This is an inland Function
{
Total_val = shares * cmd_val;
}
Public:
Void acquire (const char * CO, int N, double pr); // get the stock
Void buy (INT num, double price); // increase the stock holdings
Void sell (INT num, double price); // sell the stock
Void Update (double price); // update the stock price
Void show (); // display information about the stock you hold
};
// Define the member functions of the class
Void stock: acquire (const char * CO, int N, double pr)
{
Strncpy (Company, CO, 29 );
Company [29] = '\ 0 ';
If (n <0)
{
Cerr <"the number of stocks cannot be negative! "<Endl;
The number of cerr <company <"shares is set to 0" <Endl;
Shares = 0;
}
Else
Shares = N;
Pai_val = Pr;
Settol ();
}
Void stock: Buy (INT num, double price)
{
If (Num <0)
{
Cerr <"the number of holdings cannot be negative! "<Endl;
}
Else
{
Shares + = num;
Pai_val = price;
Settol ();
}
}
Void stock: Random (INT num, price)
{
If (Num <0)
{
Cerr <"the number of stocks sold cannot be negative! "<Endl;
}
If (Num> shares)
{
Cerr <"the number of stocks you sell cannot be greater than the number you hold" <Endl;
}
Else
{
Shares = shares-num;
Pai_val = price;
Settol ();
}
}
Void stock: Update (double price)
{
Pai_val = price;
Settol ();
}
Void stock: Show ()
{
Cout <"Company:" <company;
Cout <"shares:" <shares <Endl;
Cout <"share price: $" <interval _val;
Cout <"total worth: $" <total_val <Endl;
}
Cerr object: Like a cout object, it is also an ostream object. The difference is that operating system redirection only affects cout and does not affect cerr. The cerr object is used for error messages.
Inline method: All functions defined in the class declaration will automatically become inline functions. Therefore, the above settol () function is an inline function.
Note: When calling a member function, it uses the member data of the object to call it. The created object has its own storage space, which is used to store its internal variables and class members. However, all tired objects share the same group of class methods, and each method (function) is set) there is only one copy.
2. constructor and destructor of the class:
Constructor: the constructor is automatically called when an object is created, but cannot be called by an object. Also, the class member name cannot be used as a constructor parameter!
Destructor: After an object is created using a constructor, the program traces the object until it expires. When an object expires, the program automatically calls a special function to complete the cleaning. This function is the destructor.
Sample Code:
Stock. h file code:
# Include <iostream>
# Include <cstring>
Using namespace STD;
// Class declaration
Class stock
{
PRIVATE:
Char company [30]; // company name
Int shares; // number of shares held
Double pai_val; // price per share
Double total_val; // The total value of the stock.
Void settol () // This is an inland Function
{
Total_val = shares * cmd_val;
}
Public:
Stock ();
Stock (const char * CO, int N, double pr );
~ Stock ();
Void acquire (const char * CO, int N, double pr); // get the stock
Void buy (INT num, double price); // increase the stock holdings
Void sell (INT num, double price); // sell the stock
Void Update (double price); // update the stock price
Void show (); // display information about the stock you hold
};
Code of the stock. cpp file:
# Include <iostream>
# Include <cstring>
# Include "stock. H"
Using namespace STD;
// Class implementation
// Define the member functions of the class
Stock: stock () // default constructor
{
Cout <"default constructor called:" <Endl;
Strcpy (company, "noname ");
Shares = 0;
Pai_val = 0.0;
Total_val = 0.0;
}
Stock: stock (const char * CO, int N, double pr) // Constructor
{
Cout <"constructor using:" <Endl;
Strncpy (Company, CO, 29 );
Company [29] = '\ 0 ';
If (n <0)
{
Cerr <"the number of stocks cannot be negative! "<Endl;
The number of cerr <company <"shares is set to 0" <Endl;
Shares = 0;
}
Else
Shares = N;
Pai_val = Pr;
Settol ();
}
Stock ::~ Stock () // destructor
{
Cout <"bye," <company <"! "<Endl; // The output is only used to understand when the Destructor is called.
}
Void stock: Buy (INT num, double price)
{
If (Num <0)
{
Cerr <"the number of holdings cannot be negative! "<Endl;
}
Else
{
Shares + = num;
Pai_val = price;
Settol ();
}
}
Void stock: Show ()
{
Cout <"Company:" <company;
Cout <"shares:" <shares <Endl;
Cout <"share price: $" <interval _val;
Cout <"total worth: $" <total_val <Endl;
Cout <Endl;
}
Main. cpp file code:
# Include <cstdlib>
# Include <iostream>
# Include <cstring>
# Include "stock. H"
Using namespace STD;
Int main (INT argc, char * argv [])
{
Cout. SETF (ios_base: fixed); // format
Cout. Precision (2); // format
Cout. SETF (ios_base: showpoint); // format
Cout <"using constructor to new objects:" <Endl;
Stock stock1 ("nanosmart", 12, 20.0 );
Stock1.show ();
Stock stock2 = stock ("Boffo objects", 2, 2.0 );
Stock2.show ();
Cout <"assigning stock1 to stock2:" <Endl;
Stock2 = stock1;
Cout <"listing stock1 and stock2:" <Endl;
Stock1.show ();
Stock2.show ();
Cout <"using a constructor to reset an object:" <Endl;
Stock1 = stock ("nity food", 10, 50.0 );
Cout <"revised stock1:" <Endl;
Stock1.show ();
Cout <"done \ n ";
Return 0;
}
After the above three files are compiled and connected, the results are as follows:
Think about the result. Why ??
Const member function: Place const after the class function, such as void show () Const. This definition is the const member function. Generally, as long as the class method does not modify the object, then it should be declared as Const.
Special case of constructor: constructor that accepts a parameter allows the use of the value assignment syntax to initialize an object with a value. For example, the constructor prototype is Bob (INT age );
Then we can initialize it like this: Bob czm = 22; this is the third method to initialize the object.
3. This pointer: indicates the object used to call the member function (this is passed to the method as a hidden parameter ). Each member function (including constructor and destructor) has a this pointer, which points to the calling object. This is also a pointer, so it is the same as a general pointer, * This Pointer Points to the value.
Sample Code:
Const stock & stock: topval (const stock & S) const
{
If (S. total_val> total_val)
Return S;
Else
Return * this;
}
4. Object array: To create an array of class objects, this class must have a default constructor. When initializing an object array, first use the default constructor to create an array element. Then, the constructor in curly brackets creates a temporary variable, and then copies the content of the temporary variable to the corresponding element. For example: stock STK [4] = {stock ("caizhiming",), stock ("caizhicong"), stock ("caizhiming2", 333,34 ),};
Now I have a better understanding of this pointer, and now I have a better understanding of how C ++ works. For example, when C ++ frontend cfront converts C ++ programs to C Programs: when the processing method is defined, just define the following C ++:
Void stock: Show () const
{
Cout <"Company:" <company;
Cout <"shares:" <shares <Endl;
Cout <"share price: $" <interval _val;
Cout <"total worth: $" <total_val <Endl;
}
Definition of converting to a class C style:
Void show (const stock * This)
{
Cout <"Company:" <this-> company;
Cout <"shares:" <this-> shares <"\ n ";
Cout <"share price: $" <this-> interval _val;
Cout <"total worth: $" <this-> total_val <"\ n ";
}
Similarly, convert top. Show () to show (& Top)
5. Summary:
Object-Oriented programming emphasizes how a program represents data. A class is a user-defined type, and an object is a class instance, that is, an object is a variable of this type. If you want a member function to operate on multiple objects, you can pass the additional object as a parameter to it. If the method needs to display the reference object that calls it, you can use this pointer.