1. 重载运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 #include <iostream> using namespace std ;class Person {private : int x, y; public : Person(int x, int y) : x(x), y(y) {}; void setX (int x) { this ->x = x; } int getX () { return this ->x; } void setY (int y) { this ->y = y; } int getY () { return this ->y; } Person operator +(Person person2) { int x = this ->x + person2.getX(); int y = this ->y + person2.getY(); Person res (x, y) ; return res; } Person operator +(const Person & person2) { int x = this ->x + person2.x; int y = this ->y + person2.y; Person res (x, y) ; return res; } void operator ++ (){ this ->x = this ->x + 1 ; this ->y = this ->y + 1 ; } void operator ++ (int ){ this ->x = this ->x + 1 ; this ->y = this ->y + 1 ; } friend void operator << (ostream & _START, const Person & person) { _START << person.x << "--" << person.y << endl ; } friend istream & operator >> (istream & _START, const Person & person) { _START >> person.x; _START >> person.y; return _START; } }; Person operator +(Person person1, Person person2) { int x = person1.getX() + person2.getX(); int y = person1.getY() + person2.getY(); Person res (x,y) ; return res; } int main () { Person person1 (10 , 20 ) ; Person person2 (30 , 40 ) ; Person person3 = person1 + person2; cout << "x:" << person3.getX() << "y:" << person3.getY() << endl ; cout << person1; cout << person1 << person1 << person1 << person1 << person1; }
2. 括号运算符重载 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include <iostream> using namespace std ;class ArrayClass {private : int size = 0 ; int * arrayValue; public : void set (int index, int value) { arrayValue[index] = value; size+=1 ; } int getSize () { return this ->size; } int operator [](int index) { return this ->arrayValue[index]; } }; void printfArryClass (ArrayClass arrayClass) { cout << arrayClass.getSize() << endl ; for (int i = 0 ; i < arrayClass.getSize(); ++i) { cout << arrayClass[i] << endl ; } } int main () { ArrayClass arrayClass; arrayClass.set (0 , 1000 ); arrayClass.set (1 , 2000 ); arrayClass.set (2 , 3000 ); arrayClass.set (3 , 4000 ); arrayClass.set (4 , 5000 ); printfArryClass(arrayClass); return 0 ; }
3. 对象继承 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include <iostream> using namespace std ;class Person {public : char *name; int age; public : Person(char *name, int age) : name(name) { this ->age = age; cout << "Person 构造函数" << endl ; } void print () { cout << this ->name << " , " << this ->age << endl ; } }; class Student : public Person {private : char * course; public : Student(char * name, int age, char * course) : Person(name, age) , course(course) { cout << "Student 构造函数" << endl ; } void test () { cout << name << endl ; cout << age << endl ; print(); } }; int main () { Student stu ("李元霸" , 99 , "C++" ) ; stu.name = "李四" ; return 0 ; }
4. C++多继承 在JAVA语言中只能单继承,可以多实现,很好解决了二义性。但是在C++中是支持多继承的,因此就会出现二义性,下面我们来看一下C++中是如何解决二义性的。
4.1 明确指定对象的方法 方案一是明确指定要调用某个对象的方法;
方案二是在子类中实现父类的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 #include <iostream> using namespace std ;class BaseActivity1 {public : void onCreate () { cout << "BaseActivity1 onCreate" << endl ; } void onStart () { cout << "BaseActivity1 onStart" << endl ; } void show () { cout << "BaseActivity1 show" << endl ; } }; class BaseActivity2 {public : void onCreate () { cout << "BaseActivity2 onCreate" << endl ; } void onStart () { cout << "BaseActivity2 onStart" << endl ; } void show () { cout << "BaseActivity2 show" << endl ; } }; class BaseActivity3 {public : void onCreate () { cout << "BaseActivity3 onCreate" << endl ; } void onStart () { cout << "BaseActivity3 onStart" << endl ; } void show () { cout << "BaseActivity3 show" << endl ; } }; class MainActivity1 : public BaseActivity1, public BaseActivity2, public BaseActivity3 {public : void onCreate () { cout << "MainActivity1 onCreate" << endl ; } void onStart () { cout << "MainActivity1 onStart" << endl ; } void showSonInfo () { cout << "MainActivity1 showSonInfo" << endl ; } void show () { cout << "MainActivity1 show" << endl ; } }; int main () { MainActivity1 mainActivity1; mainActivity1.onCreate(); mainActivity1.onStart(); mainActivity1.showSonInfo(); mainActivity1.BaseActivity3::show(); mainActivity1.BaseActivity2::show(); mainActivity1.BaseActivity1::show(); mainActivity1.show(); return 0 ; }
4.2重写成员属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <iostream> using namespace std ;class Object {public : int number; }; class BaseActivity1 : public Object {}; class BaseActivity2 : public Object {}; class Son : public BaseActivity1, public BaseActivity2 { public : int number; }; int main () { Son son; son.BaseActivity1::number = 1000 ; son.BaseActivity2::number = 1000 ; son.number = 3000 ; return 0 ; }
4.3使用虚函数来过渡继承 使用虚继承可以为所欲为的玩。
使用virtual修饰的继承,子类不能重写父类的属性和方法。所有都共用一份。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include <iostream> using namespace std ;class Object {public : int number; void show () { cout << "Object show run..." << endl ; } }; class BaseActivity1 : virtual public Object {}; class BaseActivity2 : virtual public Object {}; class Son : public BaseActivity1, public BaseActivity2 {}; int main () { Object object; BaseActivity1 baseActivity1; BaseActivity2 baseActivity2; Son son; object.number = 100 ; baseActivity1.number = 200 ; baseActivity2.number = 300 ; son.number = 400 ; object.show(); baseActivity1.show(); baseActivity2.show(); son.show(); cout << object.number << endl ; cout << baseActivity1.number << endl ; cout << baseActivity2.number << endl ; cout << son.number << endl ; return 0 ; }
版权声明: 此文章版权归Jack Ou所有,如有转载,请註明来自原作者