Home>

Object-oriented programmingThe main idea of ​​object oriented programming (oop, object oriented programming) is to decompose the various transactions that constitute the problem into various objects,The purpose of creating an object is not to complete a step,Rather, it is intended to describe the behavior of a thing throughout the problem-solving steps.

Process-oriented is the analysis of the steps required to solve the problem,Then use functions to implement step by step,It can be called in turn.

Object-oriented and process-oriented are two different programming ideas,No one is absolutely perfect,Development plans should be formulated according to specific needs.For example, developing a small software or application,Small amount of work,Can be completed in a short time,It is entirely possible to adopt a process-oriented development approach,Using object orientation,Instead it will increase the amount of code,Reduce development efficiency.

Process-oriented programming languages ​​(such as C) cannot create classes and objects,Cannot develop programs with object-oriented thinking;Object-oriented programming languages ​​(such as C++, PHP, etc.) retain process-oriented keywords and statements,Programs can still be developed using process-oriented thinking.

Object-oriented is a process-oriented complement and improvement.

Be careful not to understand conceptuallyMany experienced programmers cannot even fully explain the difference between object-oriented and process-oriented,Focus on practice,Keep thinking about programming language ideas.

Basic concepts of classes and objects

For convenience,We will start with real-life examples.

we know,The castings used in industry (electric rice cooker liners, automobile sites, engine bodies, etc.) are all cast from molds.One mold can cast many identical castings,Different molds can be used to cast different castings.The mold here is what we call "class", and the casting is what we call "object".

Classes are templates for creating objects,A class can create multiple identical objects;Objects are instances of classes,It is created according to the rules of the class.

Properties and methods

The casting (object) cast from the mold has many parameters (length, width, height, etc.) and can perform different operations (cooking rice, bearing, protecting internal parts, etc.) The parameter here is the "property" of the object, and the operation completed is the "method" of the object.

Attribute is a variable,Used to represent the characteristics of an object,Such as color, size, weight, etc .;the method is a function,Used to represent the operation of the object,Such as running, breathing, jumping, etc.

Object properties and methods are collectively called members of the object.

Class inheritance

A class (subclass) can inherit the characteristics of another class (parent class), just as a son inherits a father's DNA, character, and property.

Subclasses can inherit all the characteristics of the parent class,You can also inherit part of it,Flexible control by the program.

C++ object-oriented programming example

Here we will use a few simple examples to demonstrate how to design the program from an object-oriented perspective,And the benefits of using classes.

[Example] The simplest example.

#include<iostream>
using namespace std;
class time //Define the time class
{
public://Data members are public
  int hour;
  int minute;
  int sec;
};
int main ()
{
  time t1;//Define t1 as a time class object
  cin>>t1.hour;//Enter the set time
  cin>>t1.minute;
  cin>>t1.sec;
  //Output time:
  cout<t1.hour<<<<<<t1.minute<>
  return 0;
}

The operation is as follows:

1232 43↙
12:32:43

A few notes:

1) When referring to the data members hour, minute, and sec, do not forget to specify the object name in the front.

2) Don't mistype it as a class name,As written

time.hour, time.minute, time.sec

Is not right.Because a class is an abstract data type,Not an entity,Does not take up storage space,Objects are actual entities,Is taking up storage space,Its data members are valuable,Can be quoted.

3) If you delete the three input statements of the main function,That is, no value is assigned to these data members,Then their values ​​are unpredictable.

[Example] Reference members of multiple objects.

1) Procedure (a)

#include<iostream>
using namespace std;
class time
{
public:
  int hour;
  int minute;
  int sec;
};
int main ()
{
  time t1;//define object t1
  cin>>t1.hour;//Enter data into the data member of t1
  cin>>t1.minute;
  cin>>t1.sec;
  cout<t1.hour<<<>:<t1.minute<<<<<t1.sec<<endl;//output the value of the data member in t1
  time t2;//define object t2
  cin>>t2.hour;//input data to the data member of t2
  cin>>t2.minute;
  cin>>t2.sec;
  cout<t2.hour<<<<><t2.minute<<<<t2.sec<<endl;//output the value of the data member in t2
  return 0;
}

The operation is as follows:

1032 43↙
10:32:43
22 32 43↙
22:32:43

The program is clear and understandable,But in the main function, write out the relevant operations one by one for different objects.Makes the program lengthy.To solve this problem,You can use functions for input and output.See procedure (b).

2) Procedure (b)

#include<iostream>
using namespace std;
class time
{
public:
  int hour;
  int minute;
  int sec;
};
int main ()
{
  void set_time (time&);//function declaration
  void show_time (time&);//function declaration
  time t1;//Define t1 as a time class object
  set_time (t1);//Call the set_time function to input data to the data members in the t1 object
  show_time (t1);//Call the show_time function to output the data in the t1 object
  time t2;//Define t2 as a time class object
  set_time (t2);//Call the set_time function to input data to the data members in the t2 object
  show_time (t2);//Call the show_time function to output the data in the t2 object
  return 0;
}
void set_time (time&t) //Define the function set_time, the formal parameter t is a reference variable
{
  cin>>t.hour;//Enter the set time
  cin>>t.minute;
  cin>>t.sec;
}
void show_time (time&t) //Define function show_time, the parameter t is a reference variable
{
  cout<t.hour<<<>:<t.minute<<<<
}

The operation is the same as procedure (a).

3) Procedure (c)

You can make some modifications to the above program,Data member values ​​are no longer entered by the keyboard,And given by the arguments when calling the function,And use the default parameters in the function.Replace the following part of line 8 of procedure (b) with the following:

int main ()
{
  void set_time (time&, int hour=0, int minute=0, int sec=0);//function declaration
  void show_time (time&);//function declaration
  time t1;
  set_time (t1,12,23,34);//Pass the hour, minute, and second values ​​through the actual parameters
  show_time (t1);
  time t2;
  set_time (t2);//Use the default hours, minutes and seconds
  show_time (t2);
  return 0;
}
void set_time (time&t, int hour, int minute, int sec)
{
  t.hour=hour;
  t.minute=minute;
  t.sec=sec;
}
void show_time (time&t)
{
  cout<t.hour<<<<<<t.hour<>
}

The output when the program is run is:

12:23:34 (hours, minutes, seconds at t1)
0:0:0 (hours, minutes, seconds at t2)

The classes defined in the above two programs have only data members.No member functions,This obviously does not show the superiority of using classes.In the following example,The class body contains member functions.

[Example] Use a class with member functions instead.

#include<iostream>
using namespace std;
class time
{
public:
  void set_time ();//Public member function
  void show_time ();//Public member function
private://Data members are private
  int hour;
  int minute;
  int sec;
};
int main ()
{
  time t1;//define object t1
  t1.set_time ();//Call the member function set_time of object t1, input data to the data member of t1
  t1.show_time ();//Call the member function show_time of the object t1, and output the value of the data member of t1
  time t2;//define object t2
  t2.set_time ();//Call the member function set_time of the object t2, input data to the data member of t2
  t2.show_time ();//Call the member function t_show_time of the object, output the value of the data member of t2
  return 0;
}
void time ::set_time () //Define set_time function outside the class
{
  cin>>hour;
  cin>>minute;
  cin>>sec;
}
void time ::show_time () //Define show_time function outside the class
{
  cout<hour<<":<minute<<":"<sec<endl;
}

The operation is the same as the procedure (a) in Example 8.2.

A few notes:

When calling two member functions in the main function,The object names (t1, t2) should be specified. Member function indicating which object is called.

When defining functions outside the class,The scope of the function should be indicated (eg void time ::set_time ()). When a member function refers to a data member of this object,Just write the data member name directly,At this time, the C++ system will default it to the data member of this object.You can also write the class name explicitly and use the domain operator.

Attention should be paid to distinguish the occasion using the field operator "::" and the occasion using the member operator ".", Do not confuse.

[Example] Find the maximum value of the elements in an integer array.This problem can be solved without class methods,Now use classes to handle,The reader can compare the characteristics of different methods.

#include<iostream>
using namespace std;
class array_max //Declaration class
{
public://The following 3 line member function prototype declarations
  void set_value ();//Set value for array elements
  void max_value ();//Find the largest element in the array
  void show_value ();//output the maximum value
private:
  int array [10];//Integer array
  int max;//max is used to store the maximum value
};
void array_max ::set_value () //Member function definition,Enter values ​​into array elements
{
  int i;
  for (i=0;i<10;i ++)
   cin>>array [i];
}
void array_max ::max_value () //Member function definition,Find the maximum value in an array element
{
  int i;
  max=array [0];
  for (i=1;i<10;i ++)
   if (array [i]>max) max=array [i];
}
void array_max ::show_value () //Member function definition,Output maximum
{
  cout<<"max<<max;
}
int main ()
{
  array_max arrmax;//Define object arrmax
  arrmax.set_value ();//Call arrmax's set_value function and enter a value into the array element
  arrmax.max_value ();//Call arrmax's max_value function to find the maximum value in the array element
  arrmax.show_value ();//Call arrmax's show_value function to output the maximum value in the array element
  return 0;
}

The results are as follows:

12 12 39 -34 17 134 045 -91 76↙ (enter the value of 10 elements)
max=134 (enter the maximum of 10 elements)

Note the relationship between member function definitions and calling member functions.Defining a member function is just a set of operation codes.Not actually implemented,This set of operations is actually performed only when called.

It can be seen that the main function is very simple,Very few sentences,Just call the member function of the relevant object,Go to complete the corresponding operation.In most instances,Control structures (judgment structures and loop structures) do not even appear in the main function, but control structures are used in member functions.In object-oriented programming,The most critical task is the design of the class.All data and operations on the data are embodied in classes.Just define the class,The job of writing a program is easy.

c
  • Previous C # implements the ComboBox control to display multiple data source properties
  • Next ASPNET MVC5 Website Development Article Management Architecture (7)