Analyze the object oriented programming ideas of C++

  • 2020-05-05 11:31:18
  • OfStack

object-oriented programming
The main idea of
object-oriented programming (Object Oriented Programming, OOP, object-oriented programming) is to decompose the various transactions that make up a problem into individual objects. Objects are created not to complete one step, but to describe the behavior of one thing in the whole process of solving the problem.

Process-oriented is to analyze the steps needed to solve the problem, and then use the function to gradually implement, and then call in turn.

Object-oriented and process-oriented programming are two different ideas, neither of which is absolutely perfect, according to the specific needs of the development plan. For example, the development of a small software or application program, the amount of work is small, can be completed in a short time, you can fully adopt the process-oriented development method, the use of object-oriented, but will increase the amount of code, reduce the development efficiency.

Procedural programming languages (such as C) cannot create classes and objects, nor can they develop programs with object-oriented ideas. Object-oriented programming languages (such as C++, PHP, etc.) retain process-oriented keywords and statements, and still can adopt process-oriented ideas to develop programs.

Object - oriented is the complement and perfection of process - oriented.

Be careful not to "crash" conceptually. Many experienced programmers can't even fully explain the difference between object-oriented and process-oriented.
Basic concepts of classes and objects

To illustrate, we'll start with real-life examples.

As we know, the castings used in industry (electric rice cooker liner, automobile site, engine fuselage, etc.) are all cast by mold. One mold can cast many identical castings, and different castings can be cast by different molds. Here the mold is what we call a class, and the casting is what we call an object.

Class, is the template to create objects, a class can create more than one same object; An object, which is an instance of a class, is created according to the rules of the class.
Properties and methods

A casting made from a mold has many parameters (length, width, height, etc.) and can perform different operations (cooking, load-bearing, protecting internal parts, etc.). Here the parameters are the "properties" of the object, and the completed operations are the "methods" of the object.

An attribute is a variable that represents the characteristics of an object, such as color, size, weight, etc. Method is a function that represents the operation of an object, such as running, breathing, jumping, etc.

The properties and methods of an object are collectively called members of the object.
Class inherits

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

A subclass can inherit all or part of its parent class, which is flexibly controlled by the program.

Example of C++ object-oriented programming
Here we will use a few simple examples to demonstrate how to design programs from an object-oriented perspective and the benefits of using classes.

The simplest example.


#include <iostream>
using namespace std;
class Time // define Time class 
{
public : // Data members are public 
  int hour;
  int minute;
  int sec;
};
int main( )
{
  Time t1;// define t1 for Time Class object 
  cin>>t1.hour;// Enter the set time 
  cin>>t1.minute;
  cin>>t1.sec;
  // The output of time :
  cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;
  return 0;
}

Here's how it works:


1232 43 � 
12:32:43

A few notes:
1) don't forget to specify the object name before referring to data members hour, minute, sec.

2) don't write it as a class name, such as
  Time.hour,Time.minute,Time.sec
That's not true. Because a class is an abstract data type, it is not an entity, nor does it occupy storage space, while an object is an actual entity that occupies storage space, and its data members are valued and can be referenced.

3) if you delete the three input statements of the main function, that is, do not assign values to these data members, their values are unpredictable.


Reference [di 'spknt] n.members of multiple objects.

1) procedures (a)


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

Here's how it works:


1032 43 � 
10:32:43
22 32 43 � 
22:32:43

The program is straightforward, but writing out the operations for different objects in the main function can make the program verbose. To solve this problem, you can use functions for input and output. See program (b).

2) procedures (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 for Time Class object 
  set_time(t1);// call set_time Function, t1 The data member in the object inputs data 
  show_time(t1);// call show_time Function, output t1 Data in an object 
  Time t2;// define t2 for Time Class object 
  set_time(t2);// call set_time Function, t2 The data member in the object inputs data 
  show_time(t2);// call show_time Function, output t2 Data in an object 
  return 0;
}
void set_time(Time& t) // Define a function set_time The 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 a function show_time The parameter t Is a reference variable 
{
  cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;// Output the data in the object 
}

It runs the same as the program (a).

3) program (c)
You can make some changes to the program above. Instead of being entered by the keyboard, the values of the data members are given by arguments when the function is called, and the default parameters are used in the function. Change the following part of line 8 of the program (b) to:


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);// Values of time, minutes, and seconds are passed through arguments 
  show_time(t1);
  Time t2;
  set_time(t2);// Use the default values of time, 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.minute<<":"<<t.sec<<endl;
}

The output when the program runs is:


12:23:34 (t1 In time, minutes and seconds )
0:0:0 (t2 In time, minutes and seconds )

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

Switch to a class with a member function.


#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 the object t1
  t1.set_time( );// Call object t1 Member function of set_time to t1 Data member input data 
  t1.show_time( );// Call object t1 Member function of show_time And the output t1 The value of the data member 
  Time t2;// Define the object t2
  t2.set_time( );// Call object t2 Member function of set_time to t2 Data member input data 
  t2.show_time( );// Call object t2 Member function of show_time And the output t2 The value of the data member 
  return 0;
}
void Time::set_time( ) // Defined outside the class set_time function 
{
  cin>>hour;
  cin>>minute;
  cin>>sec;
}
void Time::show_time( ) // Defined outside the class show_time function 
{
  cout<< hour<<":"<< minute<<":"<< sec<< endl;
}

It runs the same as the program in example 8.2 (a).

A few notes:
When two member functions are called in the main function, the object name (t1,t2) should be specified. Represents the member function of which object is called.
When defining functions outside the class, specify the scope of the function (for example, void Time∷set_time()). When a member function references a data member of the object, it simply writes the data member name directly, and the C++ system defaults it to the data member of the object. You can also explicitly write out the class name and use the field operator.
Be careful not to confuse the use of the domain operator "∷" and the use of the member operator "."

Find the maximum value of the elements in an integer array. This problem can be solved without using the methods of the class, but now with the methods of the class, the reader can compare the characteristics of the different methods.


#include <iostream>
using namespace std;
class Array_max // Class declarations 
{
public : // The following 3 Behavior member function prototype declaration 
  void set_value( ); // Sets values for array elements 
  void max_value( ); // Find the largest element in the array 
  void show_value( ); // Output maximum 
private :
  int array[10]; // The integer array 
  int max; //max For maximum value 
};
void Array_max::set_value( ) // A member function is defined to enter a value into an array element 
{
  int i;
  for (i=0;i<10;i++)
   cin>> array[i];
}
void Array_max::max_value( ) // The member function is defined to 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 value 
{
  cout<< "max="<< max;
}
int main( )
{
  Array_max arrmax; // Define the object arrmax
  arrmax.set_value( ); // call arrmax the set_value Function to enter a value into an array element 
  arrmax.max_value( ); // call arrmax the max_value Function to find the maximum value of an array element 
  arrmax.show_value( ); // call arrmax the show_value Function that outputs the maximum value of an array element 
  return 0;
}

The result is as follows:


12 12 39 -34 17 134 045 -91 76 �  ( The input 10 The value of )
max=134 ( The input 10 The maximum number of elements )

Note the relationship between the member function definition and the calling of the member function. Defining a member function is just a set of operations designed, not actually executed, that are actually performed only when called.

You can see: the main function is very simple, very few statements, just call the object's member function, to complete the corresponding operation. In most cases, the control structure (the judgment structure and the loop structure) is not even present in the main function, but is used in the member function. In object-oriented programming, the most critical work is class design. All the data and operations on the data are embodied in the class. As long as the class is well defined, the work of programming is easy.


Related articles: