Kill someone who wirting C++&Java!
杀了那个写C++和Java的人!
Wirte by 021. Leave a message if i messed up ! : )
封装&继承&多态
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <stdio.h> #include<string.h> #include <stdlib.h>
struct person2 { public: int age; char name; char* heapObject; void printThis() { printf("this当前指针=%p\n",&(this->name)); } person2(int age,char name) { this->age = age; this->name = name; printf("有参构造函数执行了,成员变量赋值初始化 \n"); } person2() { printf("空参构造函数执行了 \n"); } ~person2() { printf("析构函数执行了,对象要销毁了 \n"); } virtual void print(){ printf("person2父类多态方法被调用了 \n"); } };
class teacher :public person2 { private: int coachAge; virtual void print(){ printf("teacher子类多态方法被调用了 \n"); } };
void myPrint(person2* p){ p->print(); }
#include "myMain.hpp"
person2 p1; int main(int argc, const char* argv[]) { person2 p; printf("this会存储在ecx寄存器中,当前指针=%p\n",&p); p.printThis();
int* ip = (int*)&p; printf("ip当前指针=%p\n",&ip); int* ip1 = ++ip; printf("ip当前指针=%p\n",&ip1); person2* p3 = new person2(); delete p3; int* cp = (int*)malloc(sizeof(int)*10); person2* mallocP = (person2*)malloc(sizeof(person2)*10);
free(cp); free(mallocP); int* cp1 = new int[10]; person2* cpp = new person2[10]; delete[] cp1; delete[] cpp;
int x = 10; printf("当前被引用类型指针=%p value=%d \n",&x,x); int& xRef = x; printf("xRef当前指针=%p value=%d \n",&xRef,xRef); xRef = 20; printf("xRef当前指针=%p value=%d \n",&xRef,xRef); int** ipp = (int**)1; int** ipRef = ipp; ipRef = (int**)2; printf("ipRef当前指针=%p value=%d \n",&ipRef,ipRef); int* xp = &x; xp++; printf("xp++当前指针=%p value=%d \n",&xp,xp); xRef++; printf("xRef++当前指针=%p value=%d \n",&xRef,xRef);
myPrint(&p); teacher* t = new teacher(); myPrint(t);
}
|