C ++ class and object further discussion (2)

Source: Internet
Author: User

 

We often see that an object of another class may appear as its data member in a class. Since it is an object, it will involve the initialization of this object member. The sharing of various data in the program breaks through the data security to a certain extent. How can C ++ ensure data sharing and prevent data changes. In addition to the above two questions, I will also talk about the multi-file program of C ++.

1. When we create a class object, if this class has an embedded object, the object member will be automatically created. Therefore, when creating an object, you must initialize the basic data member of this class and the embedded object. How can we do this? I still use the following two classes: Employee (Employee) and Salary (Salary:

1 # include "stdafx. h"

2 # include <string>

3 # include <iostream>

4

5 class Employee

6 {

7 private:

8 std: string id;

9 std: string name;

10 public:

11 Employee (std: string id, std: string name );

12 ~ Employee ();

13 void showEmployee ();

14 };

15

16 Employee: Employee (std: string id, std: string name): id (id), name (name)

17 {

18 std: cout <"constructor employee" <std: endl;

19}

20

21. Employee ::~ Employee ()

22 {

23 std: cout <"Release object employee memory space" <std: endl;

24}

25

26 void Employee: showEmployee ()

27 {

28 std: cout <"No.:" <id <std: endl;

29 std: cout <"name:" <name <std: endl;

30}

31

32

33 class Salary

34 {

35 private:

36. Employee

37 double wage; // salary

38 double bonus; // bonus

39 double commission; // commission

40 double allowance; // allowance

41 double subsidy; // subsidy

42 public:

43 Salary (double wage, double bonus, double commission, double allowance, double subsidy, std: string id, std: string name );

44 ~ Salary ();

45 void showSalary ();

46 };

47

48 Salary: Salary (double wage, double bonus, double commission, double allowance, double subsidy, std: string id, std: string name): wage (wage ), bonus (bonus), commission (commission), allowance (allowance), subsidy (subsidy), employee (id, name) // initialize the employee

49 {

50 std: cout <"constructor salary" <std: endl;

51}

52

53 Salary ::~ Salary ()

54 {

55 std: cout <"Release object salary memory space" <std: endl;

56}

57

58 void Salary: showSalary ()

59 {

60 employee. showEmployee (); // display employee information

61 std: cout <"Salary:" <std: endl;

62 std: cout <"Salary:" <wage <std: endl;

63 std: cout <"bonus:" <bonus <std: endl;

64 std: cout <"commission:" <commission <std: endl;

65 std: cout <"subsidy:" <subsidy <std: endl;

66 std: cout <"allowance:" <allowance <std: endl;

67}

68

69

70 int main ()

71 {

72 {

73 Salary salary (0,200,100, "001", "aaa ");

74 salary. showSalary ();

75}

76

77 return 0;

78}

Result:

 

From the code that initializes the member object, we can see that the data member initialization form is the same as that of the previously mentioned member initialization table. When you see the implementation part in the main function body, someone may ask whether the braces are superfluous. There is no need to consider the process of running the program. I specifically added this braces to illustrate the sequence of the braces during system compilation and running. The displayed results show that the constructor of Employee () is called first, when you call your own Constructor (Salary (), the opposite is true when you call your own destructor. Some may ask, if there are more than one object member in Salary, what is the sequence in which the constructors of these object Members call? In fact, the order in which the system calls constructor for object members during compilation and running is based on the order in the class declaration, and the object space (destructor) is released) the process is the opposite;

2. The introduction of C ++'s common types aims to ensure data sharing and prevent data changes. In object-oriented systems, common types include common objects, common data members, and common member functions. (1) Common objects are: Class Name const object name [parameter table] Or const class name object name [parameter table], the data member values of a common object cannot be changed during the lifetime of the entire object. A common object cannot call a common member function, but can only call a common member function. (2) The form of common data members is actually the same as that of C ++'s non-object-oriented feature expansion (1, however, it should be noted that the regular data member in the class can only initialize it through the initialization list, and other functions cannot assign values to it; (3) the regular member function form: type Function Name (parameter table) const, const is an integral part of the function type. Therefore, when declaring a function and defining a function, you must add the keyword const. Similarly, we take the Employee (Employee) class as an example:

1 # include "stdafx. h"

2 # include <string>

3 # include <iostream>

4

5 class Employee

6 {

7 private:

8 const std: string id; // regular data member

9 const std: string name; // regular data member

10 public:

11 Employee (std: string id, std: string name );

12 ~ Employee ();

13 void showEmployee (); // common member function

14 void showEmployee () const; // constant member function

15 };

16

17 Employee: Employee (std: string id, std: string name): id (id), name (name)

18 {

19 // std: cout <"constructor employee" <std: endl;

20}

21

22. Employee ::~ Employee ()

23 {

24 // std: cout <"Release object employee memory space" <std: endl;

25}

26

27 void Employee: showEmployee ()

28 {

29 std: cout <"common member function:" <std: endl;

30 std: cout <"No.:" <id <std: endl;

31 std: cout <"name:" <name <std: endl;

32}

33

34 void Employee: showEmployee () const

35 {

36 std: cout <"common member function:" <std: endl;

37 std: cout <"No.:" <id <std: endl;

38 std: cout <"name:" <name <std: endl;

39}

40

41 int main ()

42 {

43. Employee ("001", "aa ");

44 employee. showEmployee ();

45

46 std :: cout <"********************************" <std:: endl;

47

48 Employee const const_employee ("002", "bb"); // constant object

49 const_employee.showEmployee ();

50

51 return 0;

52}

Result:

 

In the preceding sample code, we can see two void showEmployee () functions with the same name. One is a common member function and the other is a common member function. They are overloaded, therefore, const can be used to distinguish between overloaded functions;

3. the C ++ source program consists of three parts: the declaration part of the class, the implementation part of the class, And the use part of the class. For the three parts, C ++ contains three files: class declaration files (*. h file), Class implementation file (*. cpp) and Class usage files (*. cpp, main file of the main function ). So why do I need to use multiple files in C ++ and use one *. cpp class to solve the problem? Why do I have to take three steps? There are mainly the following aspects: (1) the class implementation file is usually large, and the class declaration and implementation are put together, which is not conducive to the reading, management and maintenance of the program. We usually say that the interface should be separated from the implemented part, which makes it easier to modify the program. (2) Place the implementation of class member functions in the Declaration file and put them in the implementation file separately, which is different during compilation. The former is processed as an inline function of the class. (3) For software vendors, it only needs to provide interfaces open to users, rather than making public program source code. From this point of view, I think C ++ is clearer and clearer than C # In the object-oriented aspect.

4. finally, I will use an example to summarize what we are talking about today. At the same time, I will divide the program into three files according to the principle of C ++ multi-file (development tool: vs2010 ):

1. Declaration file *. h:

Employee. h (employee class declaration file)

View Code

Salary. h (salary declaration file)

View Code

2. Implementation file *. cpp:

Employee. cpp (employee class implementation file)

View Code

Salary. cpp (salary implementation file)

View Code

3. Use the file *. cpp

Main function File

1 # include "stdafx. h"

2 # include <iostream>

3

4 # include "salary. h"

5 int main ()

6 {

7 {

8 Salary salary (0,200,100, "001", "aaa ");

9 salary. showSalary ();

10}

11

12 return 0;

13}

Result:

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.