C++ 类的学习(非礼勿视)
类的声明程序代码:
#ifndef STOCK00_H_ #define STOCK00_H_ class Cstock{ private: int a,b; public: void function1(int num1,int num2); int function2(); }; #endif
类的实现
程序代码:
#include "StdAfx.h" #include "Cstock.h" void Cstock::function1(int num1,int num2) { a=num1; b=num2; } int Cstock::function2() { return a+b; }
主程序
#include "stdafx.h"
#include "Cstock.h"
#include <stdio.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
Cstock stock1;
int num;
stock1.function1(3,4);
num=stock1.function2();
printf("%d\n",num);
getchar();
}
#include "Cstock.h"
#include <stdio.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
Cstock stock1;
int num;
stock1.function1(3,4);
num=stock1.function2();
printf("%d\n",num);
getchar();
}
主程序反编译
程序代码:
.text:00411460 wmain proc near ; CODE XREF: j_wmainj .text:00411460 .text:00411460 var_DC = byte ptr -0DCh .text:00411460 var_18 = dword ptr -18h .text:00411460 var_C = byte ptr -0Ch .text:00411460 .text:00411460 push ebp .text:00411461 mov ebp, esp .text:00411463 sub esp, 0DCh .text:00411469 push ebx .text:0041146A push esi .text:0041146B push edi .text:0041146C lea edi, [ebp+var_DC] .text:00411472 mov ecx, 37h .text:00411477 mov eax, 0CCCCCCCCh .text:0041147C rep stosd .text:0041147E push 4 .text:00411480 push 3 .text:00411482 lea ecx, [ebp+var_C] --------------------〉这句值得注意 .text:00411485 call j_Cstock__function1 调用函数 function1 .text:0041148A lea ecx, [ebp+var_C] .text:0041148D call j_Cstock__function2 .text:00411492 mov [ebp+var_18], eax .text:00411495 mov esi, esp .text:00411497 mov eax, [ebp+var_18] .text:0041149A push eax .text:0041149B push offset Format ; "%d\n" .text:004114A0 call ds:__imp__printf .text:004114A6 add esp, 8 .text:004114A9 cmp esi, esp .text:004114AB call j__RTC_CheckEsp .text:004114B0 mov esi, esp .text:004114B2 call ds:__imp__getchar .text:004114B8 cmp esi, esp .text:004114BA call j__RTC_CheckEsp .text:004114BF xor eax, eax .text:004114C1 push edx .text:004114C2 mov ecx, ebp .text:004114C4 push eax .text:004114C5 lea edx, dword_4114E8 .text:004114CB call j__RTC_CheckStackVars .text:004114D0 pop eax .text:004114D1 pop edx .text:004114D2 pop edi .text:004114D3 pop esi .text:004114D4 pop ebx .text:004114D5 add esp, 0DCh .text:004114DB cmp ebp, esp .text:004114DD call j__RTC_CheckEsp .text:004114E2 mov esp, ebp .text:004114E4 pop ebp .text:004114E5 retn .text:004114E5 wmain endp
Function1 反编译
程序代码:
.text:004113C0 Cstock__function1 proc near ; CODE XREF: j_Cstock__function1j .text:004113C0 .text:004113C0 var_CC = byte ptr -0CCh .text:004113C0 var_8 = dword ptr -8 .text:004113C0 arg_0 = dword ptr 8 .text:004113C0 arg_4 = dword ptr 0Ch .text:004113C0 .text:004113C0 push ebp .text:004113C1 mov ebp, esp .text:004113C3 sub esp, 0CCh .text:004113C9 push ebx .text:004113CA push esi .text:004113CB push edi .text:004113CC push ecx .text:004113CD lea edi, [ebp+var_CC] .text:004113D3 mov ecx, 33h .text:004113D8 mov eax, 0CCCCCCCCh .text:004113DD rep stosd .text:004113DF pop ecx --------------------------〉这句对应于主函数 lea ecx, [ebp+var_C] ecx指向主函数的栈底往后数C的地址上 .text:004113E0 mov [ebp+var_8], ecx ---------〉将这个地址存放到本函数栈内 .text:004113E3 mov eax, [ebp+var_8] ---------->将这个值给eax .text:004113E6 mov ecx, [ebp+arg_0] -----------〉ecx =3 .text:004113E9 mov [eax], ecx ----------->这句将3的值存放到 eax指向的内存中去 而eax的值是指向主函数的栈ebp-12的地方 .text:004113EB mov eax, [ebp+var_8] .text:004113EE mov ecx, [ebp+arg_4] .text:004113F1 mov [eax+4], ecx ------------〉这个地方是将4的值存放到主函数的栈ebp-8的地方 .text:004113F4 pop edi .text:004113F5 pop esi .text:004113F6 pop ebx .text:004113F7 mov esp, ebp .text:004113F9 pop ebp .text:004113FA retn 8 .text:004113FA Cstock__function1 endp
Function2 反编译
程序代码:
.text:00411410 Cstock__function2 proc near ; CODE XREF: j_Cstock__function2j .text:00411410 .text:00411410 var_CC = byte ptr -0CCh .text:00411410 var_8 = dword ptr -8 .text:00411410 .text:00411410 push ebp .text:00411411 mov ebp, esp .text:00411413 sub esp, 0CCh .text:00411419 push ebx .text:0041141A push esi .text:0041141B push edi .text:0041141C push ecx .text:0041141D lea edi, [ebp+var_CC] .text:00411423 mov ecx, 33h .text:00411428 mov eax, 0CCCCCCCCh .text:0041142D rep stosd .text:0041142F pop ecx -------------------〉这句对应于主函数 lea ecx, [ebp+var_C] .text:00411430 mov [ebp+var_8], ecx .text:00411433 mov eax, [ebp+var_8] 让eax指向主函数栈 ebp-12的地方 .text:00411436 mov eax, [eax] 取主函数栈ebp-12存放的值 .text:00411438 mov ecx, [ebp+var_8] 给让ecx也指向驻函数ebp-12的地方 .text:0041143B add eax, [ecx+4] ---------------------------〉return a+b .text:0041143E pop edi .text:0041143F pop esi .text:00411440 pop ebx .text:00411441 mov esp, ebp .text:00411443 pop ebp .text:00411444 retn .text:00411444 Cstock__function2 endp
结论
1. 类的私有变量 定义类的实例 在哪个函数里,那么类的私有变量就在哪个函数的栈里面
2. 类的方法调用,和普通的函数调用是没多大区别的
3。类的私有变量不能被外部程序访问,并不是指可执行程序的场景,而是指编译器在编译的过程中不允许发生这样的事情
[ 本帖最后由 zhu224039 于 2014-6-12 18:21 编辑 ]