C++ study notes nine-class definition and initialization

In C++, you can use classes to define your own data types, so how to understand your own data types? For example, we are common such as int, float, char... and so on, which are all a data type, then for example, I want to define a data type called clock, which contains hours, minutes, seconds, and can be displayed, then We can use the class to define our own data type clock.
Defined as follows

class clock {
    
    
public:
	void setime(int hour, int minute, int second);
	void showtime();
	clock(int newh, int newm, int news);//构造函数
	clock();//默认构造函数
private:
	int hour;
	int minute;
	int second;

};

class : The keyword for declaring the class, the basic form of declaring the class.
clock : Class name, which means hour, minute, and second.
public, private: indicates the member attributes in the class, where public indicates that the class member attributes are public and can be accessed outside the class. The specific access form is: class name. member name, where the members of the class can be either data or The function name, for example, I want to access the setime function in time whose data type is clock, and the access form is time.setime()
private: it is a private member of the class, which cannot be accessed outside the class but can be accessed in the member.
In fact, there is also a protected attribute member, which is similar to private, but is slightly different in some aspects, which will be introduced in a later article.
Constructor : It contains the default constructor and the constructor defined by yourself. The function of the constructor is to initialize the members, such as the members in private. The construction form is the class name (parameter), and the constructor cannot have a return value. Taking the above clock as an example, the form of the constructor is:

clock(int newh, int newm, int news);//构造函数
clock();//默认构造函数

Function definition : After defining the function members in the class, the function can be defined outside the class or within the class. The definition method is the return value type class name: function name (parameter). Take the showtime function above as an example. The way of definition is

void clock::showtime()
{
    
    
	cout << "time" << " is   " << hour << " : " << minute << "   : " << minute << endl;

}

Here is an example to facilitate understanding of the definition of the class and the role of the constructor. Simply define a clock class and initialize it in two different ways, and print the initialized data. The complete code is as follows

# include <iostream>
# include <string>
# include <vector>
using namespace std;
class clock {
    
    
public:
	void setime(int hour, int minute, int second);
	void showtime();
	clock(int newh, int newm, int news);//构造函数
	clock();//默认构造函数
private:
	int hour;
	int minute;
	int second;

};

void clock::setime(int newhour, int newminute, int newsecond)
{
    
    
	hour = newhour;
	minute = newminute;
	second = newsecond;
}
void clock::showtime()
{
    
    
	cout << "time" << " is   " << hour << " : " << minute << "   : " << minute << endl;

}
 clock::clock(int newh, int newm, int news):hour(newh),minute(newm),second(news)
{
    
    
	 //初始化列表,对象进行简单的初始化时可以采用此方式,可以优先执行初始化列表从而提高运行的速度
}
 clock::clock():hour(0),minute(0),second(0) {
    
    }
int main()
{
    
    
	/*clock  time;*/
	//time.setime(16, 41, 35);
	//time.showtime();

	//默认构造函数
	clock time1;
	time1.showtime();
	//构造函数初始化
	clock time2(9, 27, 6);
	time2.showtime();
}

The initialization list used above is actually a form of quick initialization, because simple initialization is to assign a value to the corresponding variable. It is the same to write the assignment statement in the function body, and the running result is shown in the figure.
Program running results
The above is the definition of a simple class and the use of the constructor.

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/112506471