Analysis of C + + object-oriented programming ideas _c language

Source: Internet
Author: User
Tags define function function definition set time

Object-Oriented Programming
Object-oriented programming (object oriented Programming,oop, object-oriented programming) is the main idea of the composition of the problem of the various transactions into various objects, the purpose of building objects is not to complete a step, But to describe the behavior of a thing in the whole process of solving the problem.

Process-oriented is to analyze the necessary steps to solve the problem, and then use the function step-by-step implementation, and then call on it.

Object-oriented and process-oriented are two different programming ideas, none of which is absolutely perfect, and the development plan should be developed according to the specific requirements. For example, the development of a small software or applications, a small amount of work, a short period of time can be completed, fully adopt the process-oriented development approach, the use of object-oriented, but will increase the amount of code, reduce development efficiency.

Process-oriented programming languages (such as C) cannot create classes and objects, and cannot develop programs with object-oriented ideas; object-oriented programming languages (such as C + +, PHP, etc.) retain process-oriented keywords and statements, and can still use process-oriented thinking to develop programs.

Object oriented is the process-oriented complement and perfection.

Note, do not "die knock" concept of understanding, many experienced programmers can not even fully explain the object-oriented and process-oriented differences, to focus on practice, and constantly try to figure out the idea of programming language.
Basic concepts of classes and objects

For the convenience of illustration, we will start from real life examples.

We know that the industrial use of castings (rice cooker liner, car site, engine fuselage, etc.) are cast out by the mold, a mold can be cast a lot of the same casting, no mold can be cast out of different castings. The mold here is what we call "class", and casting is what we call "the object".

Class is the template that creates the object, a class can create multiple identical objects, an instance of the class, and is created according to the rules of the class.
Properties and Methods

Casting from the Mold (object), there are a lot of parameters (length, width, height, etc.), can complete different operations (cooking, load-bearing, protection of internal parts, etc.). The argument here is the object's "property", and the complete operation is the object's "method".

A property is a variable that represents the characteristics of an object, such as color, size, weight, and so on; A method is a function that represents the operation of an object, such as running, breathing, jumping, and so on.

The properties and methods of an object are collectively referred to as members of the object.
Inheritance of Classes

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

Subclasses can inherit all the characteristics of the parent class, or they can inherit a part, which is controlled by the program flexibly.

Example of C + + object-oriented programming
Here are a few simple examples to demonstrate how to design programs from an object-oriented perspective, as well as the benefits of using classes.

The simplest example of "example".

#include <iostream>
using namespace std;
Class Time//define time class
{
public://data member is a common
  int hour;
  int minute;
  int sec;
the int main ()
{
  time t1;//defines T1 to enter a
  set of times for cin>>t1.hour;//object
  cin>>t1.minute;
  cin>>t1.sec;
  Output time:
  cout<<t1.hour<< ":" <<t1.minute<< ":" <<t1.sec<<endl;
  return 0;
}

The operating conditions are as follows:

1232 43↙
12:32:43

A few notes:
1 Do not forget to specify the object name before referencing the data member Hour,minute,sec.

2 Do not write the wrong class name, as written
Time.hour,time.minute,time.sec
Is wrong. Because a class is an abstract data type, not an entity or a storage space, an object is an actual entity, is a storage space, and its data members are valued and can be referenced.

3 If you delete the 3 input statements of the main function that do not assign values to those data members, their values are unpredictable.


"Example" refers to 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 to T1 data member
  cin>>t1.minute;
  cin>>t1.sec;
  cout<<t1.hour<< ":" <<t1.minute<< ":" <<t1.sec<<endl;//output value of data members in T1 time
  T2 ;//define Object T2
  cin>>t2.hour;//input data to the T2 data member
  cin>>t2.minute;
  cin>>t2.sec;
  cout<<t2.hour<< ":" <<t2.minute<< ":" <<t2.sec<<endl;//output value of data member in T2 return
  0;
}

The operating conditions are as follows:

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

The program is clear and understandable, but writing about the different object one by one in the main function will make the program verbose. 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 for the time class object
  set_time (t1);//Call the Set_time function, enter data T1 (show_time) to the data member in the T1 object
  , and call the Show_time function to output data from the T1 object Time
  t2;//defines T2 as the time class object
  set_time (t2);//Call the Set_time function to enter data T2 (show_time) to the data member in the T2 object
  ;//Call Show_ Time function, output the data in the T2 object return
  0;
}
void Set_time (time& t)//define function Set_time, parameter T is reference variable
{
  cin>>t.hour;//input set time
  cin>> T.minute;
  cin>>t.sec;
}
void Show_time (time& t)//define function Show_time, parameter T is reference variable
{
  cout<<t.hour<< ":" <<t.minute << ":" <<t.sec<<endl;//the data in the output object
}

The operation is the same as program (a).

3) procedure (c)
You can make some modifications to the above program, and the value of the data member is no longer entered by the keyboard, given by the argument when the function is called, and the default parameter is used in the function. In the following section, line 8th of the procedure (b) should read:

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)//The value of the time, minute, and second passed through the argument
  show_time (t1);
  Time T2;
  Set_time (T2);//Use the default time, minute, and second value
  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 of the program at run time is:

12:23:34 (time, minutes, seconds) 0:0:0 in T1
(time, minutes, seconds in T2)

The classes defined in the above two programs have only data members and no member functions, which clearly does not reflect the advantages of using classes. In the following example, a member function is included in the class body.

"Example" is handled using a class that contains a member function.

#include <iostream>
using namespace std;
Class time
{
public:
  void Set_time ()//Common member function
  void Show_time ()//public member function
private://data member is private
  int hour;
  int minute;
  int sec;
int main ()
{time
  t1;//define object T1
  t1.set_time ();//Invoke the member function Set_time of the object T1, enter data T1 to the data member of the T1.show_time
  ( )//Call Object T1 member function show_time, output value time of T1 data member
  t2;//define Object T2
  t2.set_time ();//Call Object T2 member function Set_time, Enter data T2.show_time () to the T2 data member
  ()//Call the member function of the object T2 Show_time, the value of the data member of the output 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 functions outside the class
{
  cout<< hour<< ":" << minute<< ":" < < sec<< Endl;
}

The operation is the same as the program (a) in example 8.2.

A few notes:
You should indicate the object name (T1,T2) when you call two member functions in the main function. Represents the member function of which object is invoked.
When you define a function outside of a class, you should indicate the scope of the function (such as void Time∷set_time ()). When a member function references a data member of this object, you simply write the data member name, and the C + + system defaults to the data member of this object. You can also explicitly write out the class name and use the field operator.
Should pay attention to distinguish what situation with the domain operator "", where the use of the member operator ".", do not confuse.

"Example" finds the maximum value of an element in an integer array. This problem can not be solved by the class method, now with the class to deal with, the reader can compare the characteristics of different methods.

#include <iostream>
using namespace std;
Class Array_max//Declaration classes
{public
://The following 3 behavior member function prototypes declare
  void Set_value ();
  Find the maximum element
  void Show_value () in the array;//output maximum
private:
  int array[10];//integer array
  int max;//max for maximum value
};< C12/>void Array_max::set_value ()//member function definition, enter value to array element
{
  int i;
  for (i=0;i<10;i++)
   cin>> array[i];
}
void Array_max::max_value ()//member function definition, find the maximum value in the 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 max
{
  cout<< "max=" << Max;
}
int main ()
{
  Array_max Arrmax;//define Object Arrmax
  arrmax.set_value ();//Call Arrmax's Set_value function, enter numeric values into array elements
  arrmax.max_value ();//Call the Arrmax Max_value function to find the maximum value in the array element
  Arrmax.show_value ();//Call Arrmax function of Show_value. The maximum value in the output array element return
  0;
}

The results of the operation are as follows:

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

Note that the member function definition is related to the invocation of a member function, and the definition of a member function simply designs a set of operational code that is not actually executed, and does not really perform this set of actions until it is invoked.

Can see: The main function is very simple, the statement is very few, just call about the object's member function, to complete the appropriate operation. In most cases, the control structure is not even present in the main function (judging the structure and looping structure), but the control structure is used in the member function. In object-oriented program design, the most critical work is class design. All the data and the operation of the data are embodied in the class. As long as the class is well defined, the task of writing the program becomes simple.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.