首页 > 代码库 > C++结构体 和类

C++结构体 和类

  1 #include <iostream>
  2 using namespace std;
  3  
  4 struct father
  5 {
  6     /*
  7     virtual bool set() = 0;
  8     virtual bool get() = 0;
  9     */
 10      
 11     virtual bool set(int a, int b) = 0;
 12     virtual bool get(int a, int *b) = 0;
 13      
 14 };
 15 struct jack :public father
 16 {
 17     int x;
 18     int y;
 19     bool set(int a, int b);
 20     bool get(int a,int *p);
 21  
 22 };
 23  
 24 struct mack :public father
 25 {
 26     int x;
 27     int y;
 28     bool set(int a, int b);
 29     bool get(int a, int *p);
 30 };
 31  
 32 struct mackson :public mack
 33 {
 34     int x;
 35     int y;
 36     bool set(int a, int b);
 37     bool get(int a, int *p);
 38 };
 39 bool jack::set(int a, int b)
 40 {
 41     x = a;
 42     y = b;
 43     if (a == x && b == y)
 44         return true;
 45     else
 46         return false;
 47 }
 48 bool jack::get(int a,int *p)
 49 {
 50     if (0 == a)
 51     {
 52         p = &x;
 53         return true;
 54     }
 55     else if (1 == a)
 56     {
 57         p = &y;
 58         return true;
 59     }
 60     else
 61         return false;
 62 }
 63  
 64 bool mack::set(int a, int b)
 65 {
 66     x = a;
 67     y = b;
 68     if (a == x && b == y)
 69         return true;
 70     else
 71         return false;
 72 }
 73 bool mack::get(int a, int *p)
 74 {
 75     if (0 == a)
 76     {
 77         p = &x;
 78         return true;
 79     }
 80     else if (1 == a)
 81     {
 82         p = &y;
 83         return true;
 84     }
 85     else
 86         return false;
 87 }
 88  
 89 bool mackson::set(int a, int b)
 90 {
 91     x = a;
 92     y = b;
 93     if (a == x && b == y)
 94         return true;
 95     else
 96         return false;
 97 }
 98 bool mackson::get(int a, int *p)
 99 {
100     if (0 == a)
101     {
102         p = &x;
103         return true;
104     }
105     else if (1 == a)
106     {
107         p = &y;
108         return true;
109     }
110     else
111         return false;
112 }
113  
114 int main()
115 {
116      
117     father *p1;
118     int a=0;
119     bool (father::*pf)(int a, int b) = 0;
120     bool (father::*pb)(int a, int *b) = 0;
121  
122     p1 = new jack;
123  
124     pf = &father::set;
125     pb = &father::get;
126  
127     (p1->*pf)(5,6);
128     (p1->*pb)(1, &a);
129     cout << a << endl;
130  
131  
132  
133     return 0;
134 }

这是 C++结构体 实现的 重载,多重继承,多态,虚基类,抽象类,类成员指针
主要是为了 让大家更好认识C++结构体和类的区别

C++结构体 和类