C++ class declaration and member function definition | reference multiple member objects

The separation of C++ class declaration and member function definition

In C++, the general practice is to put the class declaration in the specified header file. If the programmer wants to use the class, he only needs to include the relevant header file. There is no need to repeat the class declaration in the program to reduce Workload, improve programming efficiency.

Since the header file contains the declaration of the class, the class can be used to define the object in the program. Since the declaration of the member functions is included in the class body, the public member functions of these objects can be called in the program.

In C++, if a class declaration is selected by different programs multiple times, you do not need to recompile it each time, but only need to compile it once, and save the object file formed after the first compilation. Later, when needed, it can be called out and directly connected with the target file of the program.

In actual C++ development, a class declaration is not made into a header file, but several commonly used class declarations with similar functions are gathered together to form a class library.

There are two types of C++ libraries:

The standard class library provided by the C++ compilation system
, the user-defined class library, the user class library created by the user according to his own needs, is provided to himself and his authorized persons.

The C++ class library includes two components:

Class declaration header file
The definition of the compiled member function, it is the object file.

Case: C++ references members of multiple objects.

#include<iostream>//预处理
using namespace std;//命名空间 
class Time
{
    
    
  public: //声明以下是公用的
    int day;
    int hour;
    int minute; 
};
int main()//主函数 
{
    
    
  Time time1;//定义time1为Time类对象
  cout<<"输入天:"<<endl; 
  cin>>time1.day;
  cout<<"输入小时:"<<endl;
  cin>>time1.hour;
  cout<<"输入分钟:"<<endl;
  cin>>time1.minute;
  cout<<"写这篇文章是"<<time1.day<<"号,";
  cout<<time1.hour<<"点:";
  cout<<time1.minute<<"分"<<endl;
  cout<<"------------------------------"<<endl;
  Time time2;//定义time2为Time类对象
  cout<<"输入天:"<<endl; 
  cin>>time2.day;
  cout<<"输入小时:"<<endl;
  cin>>time2.hour;
  cout<<"输入分钟:"<<endl;
  cin>>time2.minute;
  cout<<"写这篇文章是"<<time2.day<<"号,";
  cout<<time2.hour<<"点:";
  cout<<time2.minute<<"分"<<endl;
  return 0; //函数返回值为0;
}

Compile and run results:

输入天:
24
输入小时:
20
输入分钟:
25
写这篇文章是24号,20点:25------------------------------
输入天:
18
输入小时:
15
输入分钟:
56
写这篇文章是18号,15点:56

--------------------------------
Process exited after 19.05 seconds with return value 0
请按任意键继续. . .

Readers can try to optimize the above code by themselves, such as extracting input and output separately as a method.

Above, if you read and think it is helpful to you, please give Xiaolin a compliment, so Xiaolin also has the motivation to update, thank you fathers and folks~
C++ class declaration and member function definition
More cases can go to the official account : C language entry to mastery

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/113105021