类的构造、析构、赋值构造、拷贝构造函数

 1 #include <iostream> 
 2 using namespace std;
 3 //class String 
 4 //{ 
 5 //public: 
 6 // String(const char *str = NULL);
 7 // 普通构造函数 // String(const String &other); 
 8 // 拷贝构造函数 
 9 // ~ String(void); // 析构函数 
10 // String & operate =(const String &other); // 赋值函数 
11 //private: 
12 // char *m_data; 
13 // 用于保存字符串 
14 //};
15 class NumCal
16 {
17 public:~NumCal();// 析构函数 
18        NumCal();// 普通构造函数 
19        NumCal(const NumCal &temp);// 拷贝构造函数 
20        NumCal &operator=(const NumCal &temp); // 赋值函数
21        NumCal &operator+(const NumCal &temp);
22        NumCal &operator-(const NumCal &temp);
23        NumCal &operator+=(const NumCal &temp);
24        NumCal &operator-=(const NumCal &temp); 
25 public:
26     int i;
27     int j;
28 };
29 NumCal::~NumCal()
30 {
31     i=0; 
32     j=0;
33 }
34 NumCal::NumCal()//构造函数
35 {
36     i=0;
37     j=0;
38 }
39 NumCal::NumCal(const NumCal &temp)//赋值构造函数
40 {
41     i=temp.i;
42     j=temp.j;
43 }
44 NumCal &NumCal::operator+(const NumCal &temp)
45 {
46     i=i+temp.i;
47     j=j+temp.j;
48     return *this;
49 }
50 NumCal &NumCal::operator-(const NumCal &temp)
51 {
52     i=i-temp.i;
53     j=j-temp.j;
54     return *this;
55 }
56 NumCal &NumCal::operator=(const NumCal &temp)
57 {
58     i=temp.i;
59     j=temp.j;
60     return *this;
61 }
62 NumCal &NumCal::operator+=(const NumCal &temp)
63 {
64     i=i+temp.i;
65     j=j+temp.j;
66     return *this;
67 }
68 NumCal &NumCal::operator-=(const NumCal &temp)
69 {
70     i=i-temp.i;
71     j=j-temp.j;
72     return *this;
73 }
74 int main()
75 {
76     NumCal a,b,c;
77     a.i=3;
78     a.j=4;
79     b.i=6;
80     b.j=6;
81     a+=b;
82     cout<<a.i<<a.j<<endl;
83 }

 

posted @ 2013-08-20 23:10  cheshulin  阅读(215)  评论(0编辑  收藏  举报