1.多態的例子
題目:
某小型公司,主要有四類員工(Employee):經理(Manager)、技術人員(Technician)、銷售經理(SalesManager)和推銷員(SalesMan)。現在,需要存儲這些人員的姓名(name)、編號(id)、當月薪水(salary)。計算月薪總額並顯示全部信息。人員編號基數為 1000,每輸入一個人員工信息編號順序加 1。
月薪計算辦法是:經理拿固定月薪 8000 元;技術人員按每小時 100 元領取月薪;推銷員的月薪按該推銷員當月銷售額的 4%提成;銷售經理既拿固定月薪也領取銷售提成,固定月薪為 5000 元,銷售提成為所管轄部門當月銷售總額的5%。
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<iostream> 3 #include<string> 4 using namespace std; 5 6 //創建一個抽象類 7 class Employee 8 { 9 public: 10 Employee() 11 { 12 inum++; //這里是要點: 當時沒有想出來,在寫一個變量來接收靜態變量的變化值 13 mID = inum; //可以做到當每增加一個員工,它的 ID 會自動 加一。 14 mName = "NoDefined"; 15 mSalary = 0.0; 16 } 17 18 virtual void GetPay() = 0; //不同的計算工資方法; 19 virtual void Show() //在基類中實現出來,如果內容相同,子類就不用再實現了。當時沒想到 20 { 21 cout << "姓名:" << mName << " ID:" << mID << " 工資:" << mSalary << endl; 22 } 23 virtual ~Employee(){ } //基類的析構函數一般寫成虛繼承,可以做到把基類和子類對象一起析構 24 protected: 25 int mID; 26 string mName; 27 double mSalary; 28 static int inum; //員工ID的基數,每進一個員工,在基數上加一,就為它的ID。 29 }; 30 int Employee::inum = 1000; 31 32 //經理 33 class Manager : virtual public Employee 34 { 35 public: 36 Manager(){} 37 Manager(string name) 38 { 39 mName = name; 40 base = 8000; 41 } 42 43 virtual void GetPay() 44 { 45 mSalary = base; 46 } 47 48 protected: 49 double base; 50 }; 51 //技術人員 52 class Technician : public Employee 53 { 54 public: 55 Technician(string name, int hour = 0) 56 { 57 mName = name; 58 mHour = hour; 59 } 60 61 virtual void GetPay() 62 { 63 mSalary = mHour * 100; 64 } 65 protected: 66 int mHour; 67 }; 68 69 //銷售人員 70 class SalesMan : virtual public Employee 71 { 72 public: 73 SalesMan(){} 74 SalesMan(string name, double count = 0) 75 { 76 sum += count; 77 mName = name; 78 mCount = count; 79 } 80 81 virtual void GetPay() 82 { 83 mSalary = mCount * 4 / 100; 84 } 85 protected: 86 double mCount; 87 static double sum; //累加每個銷售的業績,然后傳遞給派生類。 (經典) 88 }; 89 double SalesMan::sum = 0; 90 91 //銷售經理(多繼承,要點是: 虛繼承(解決二義性))。 92 class SalesManager : public SalesMan, public Manager //此處用了虛繼承,解決了二義性,也是重點。 93 { 94 public: 95 SalesManager() 96 { 97 98 } 99 SalesManager(string name) 100 { 101 base = 5000; 102 mName = name; 103 104 } 105 106 virtual void GetPay() 107 { 108 mSalary = base + sum * 5 / 100; 109 cout << "銷售總額" << sum << endl; 110 } 111 }; 112 113 void printInfo(Employee& emp) 114 { 115 emp.GetPay(); 116 emp.Show(); 117 } 118 void test03() 119 { 120 Manager m1("xiaoyong"); 121 Technician t1("xiaoming", 200); 122 SalesMan s1("xiaohong", 80000); 123 SalesMan s2("xiaohong", 10000); 124 125 SalesManager sm1("xiaoyong"); 126 127 printInfo(m1); 128 printInfo(t1); 129 printInfo(s1); 130 printInfo(sm1); 131 } 132 133 int main03() 134 { 135 136 test03(); 137 138 system("pause"); 139 return EXIT_SUCCESS; 140 }
一. 知識點:
1.多態實現的三步:1)有繼承, 2)虛函數重寫, 3)基類指針或引用指向派生類對象。
2.要想讓多態顯實現不同的功能,一定記得要把基類的函數調用起來(或者讓你想使用的那個變量的函數調用起來,)不要忘記調用,或少調用了。
3.類中三種屬性或變量的初始化:
1.) static 靜態成員變量時: (正確寫法:在類外初始化:類型 類名::成員名)
2.) const 成員初始化; (正規寫法:在類內用初始化列表初始化)
3.) const static 成員初始化: (正確寫法:本地初始化;在類內定義的地方初始化)
1 class person{ 2 public: 3 person(int num = 0) : Age(num) "初始化列表" 4 { } 5 void show() 6 { 7 cout<<"Age:"<<Age<<" ID"<<ID<<" size"<<size<<endl; 8 } 9 private: 10 static int ID; "在類外初始化" 11 const int Age; "初始化列表" 12 const static int size = 100; "在本地初始化:即定義的地方" 13 }; 14 int person::ID = 30;
2.設計一個基類Shape包含成員函數Show(), 將Show()聲明為純虛函數。
Shape類公有派生矩形類Square(正方形)和圓類Circle(圓形),
問題1: 分別定義Show()實現其主要集合元素的顯示。使用抽象類Shape
類型的指針,當它指向某個派生類的對象時,就可以通過訪問該對象的虛函數
成員Show()。
問題2:用ShowArea()分別顯示各種圖形的面積.最后還要顯示所有圖形
的各個面積。要求積累指針數組,數組的每個元素指向一個派生類對象。
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<iostream> 3 using namespace std; 4 const double PI = 3.14;
//基類 5 class shape 6 { 7 public: 8 virtual void show() = 0; //顯示各類基本元素 9 virtual void showArea() = 0; //顯示面積 10 virtual void Caculate() = 0; //面積算法 11 ~shape(){ } 12 }; 13 //正方形 14 class Square : public shape 15 { 16 public: 17 Square() 18 { 19 Lenth = 0; 20 Width = 0; 21 } 22 Square(double lenth, double width) 23 { 24 Lenth = lenth; 25 Width = width; 26 } 27 virtual void show() 28 { 29 cout << "Lenth:" << Lenth << " Width" << Width << endl; 30 } 31 virtual void showArea() 32 { 33 cout << "SquareArea:" << SquareArea << endl; 34 } 35 virtual void Caculate() 36 { 37 SquareArea = Lenth * Width; 38 } 39 private: 40 double Lenth; 41 double Width; 42 double SquareArea; 43 }; 44 45 //圓形 46 class Circle : public shape 47 { 48 public: 49 Circle(){ Radius = 0; } 50 Circle(double radius) 51 { 52 Radius = radius; 53 } 54 virtual void show() 55 { 56 cout << "半徑:" << Radius << endl; 57 } 58 virtual void showArea() 59 { 60 cout << "CircleArea" << CircleArea << endl; 61 } 62 virtual void Caculate() 63 { 64 CircleArea = PI * Radius * Radius; 65 } 66 private: 67 double Radius; 68 double CircleArea; 69 }; 70 71 72 void printNews(shape& sp) 73 { 74 sp.Caculate(); 75 sp.show(); 76 sp.showArea(); 77 78 } 79 80 void test03() 81 { 82 Square sq1(5, 4); 83 Circle cr1(7); 84 Square sq2; 85 printNews(sq1); 86 printNews(cr1); 87 printNews(sq2); 88 89 } 90 91 int main03() 92 { 93 94 test03(); 95 96 system("pause"); 97 return EXIT_SUCCESS; 98 }