Class C ++

zhaozj2021-02-16  68

C is an object-oriented language, an object is an instance of a class. C programmers put the focus on the user-defined type called "class".

The class in C is evolved from the structure of C, and the structure is a polymeric data type consisting of other types of related elements. For example, the following is a definition of a structure:

Struct time {

Int Hour;

Int minute;

INT Second;

}

The program can directly access the elements in the structure and can be arbitrarily modified, such as:

Time TimeObject;

TimeObject.Hour = 200; // 0 <= Hour <= 23

This gives some invalidates to members of the structure. This is the shortcomings of the C structure, and the C class has solved this problem.

Class time {// 1

PUBLIC: // 2

Time (); // 3

Void SetTime (int, int, int); // 4

Void PrintMilitary (); //5

Void PrintStandard (); //6

PRIVATE: / / 7

INT Hour; // 8

INT minute; // 9

INT Second; // 10

}; // 11

As you can see, the class not only encapsulates the data member, but also encapsulates the member function. "Public" and "private" are members access specifiers, 3-6 lines belong to public members, 8-10 lines belong to private members. Public members can call by the user of the class, and the private member can only be accessed by the member function and friend. We generally declare the member function as a public for public, and the Public member function is also called the interface. Data members declare that Private is to make any modifications to data without letting the class, even errors. Everyone is possible to pay attention to the Time () member function of the same name as the class, which is a class constructor that is automatically called when the class object is declared, which is used to initialize the data member.

Let's take a specific question so that you can understand the C class.

#include

Class time {

PUBLIC:

Time (); // 3

Void SetTime (int, int, int);

Void PrintMilitary ();

Void PrintStandard ();

Private:

Int Hour;

Int minute;

INT Second;

}; // 13

Time :: time () {hours = minute = second = 0;

Void Time :: SetTime (int H, int M, int S)

{

Hour = (h> = 0 && h <24)? h: 0;

Minute = (m> = 0 && m <60)? m: 0;

SECOND = (S> = 0 && S <60)? S: 0;

}

Void Time :: PrintMilitary ()

{

COUT << (Hour <10? ":") << Hour << ":" << (Minute <10? ":" ") << minute;

}

Void Time :: PrintStandard ()

{

Cout << ((Hour == 0 || Hour == 12)? 12: Hour% 12)

<< ":" << (Minute <10? "0": "" "<< minute

<< ":" << (Second <10? "0": "") << SECOND

<< (Hour <12? "am": "pm"); // 35

}

int main ()

{

Time T;

T.PrinTmilitary (); // Output automatic initialization using military format.

Cout << "/ n";

T.PrintStandard (); // Output automatic initialization using standard format.

Cout << "/ n / n";

T.SETTIME (13, 20, 15); / / correctly set time.

T.PrintMilitary ();

Cout << "/ n";

T.PrintStandard ();

Cout << "/ n / n";

T.SETTIME (99, 99, 99); // Error setting time.

T.PrintMilitary ();

Cout << "/ n";

T.PrintStandard ();

Cout << Endl;

Return 0;

}

3

-

13

Behavioral definition,

15

-

35

Row is the definition of member functions, you can define member functions in the definition of the class, but this is not a good habit. It is recommended to define the class definition outside the class definition to order the class name and "::" "Operator.

转载请注明原文地址:https://www.9cbs.com/read-18132.html

New Post(0)