Last active
August 29, 2015 13:56
-
-
Save kkdai/8876212 to your computer and use it in GitHub Desktop.
About class assign for public or private variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
class aClass | |
{ | |
private: | |
int a1=1; | |
public: | |
aClass():a1(2), a2(3) | |
{ | |
printOut(); | |
a1=a2; | |
printOut(); | |
} | |
int a2=1; | |
void printOut() | |
{ | |
printf("a1 = %d, a2=%d\n", a1, a2); | |
} | |
void change_A1() | |
{ | |
a1=7; | |
} | |
}; | |
class bClass :public aClass | |
{ | |
private: | |
int b1=1; | |
public: | |
bClass():b1(2), b2(3) | |
{ | |
aClass::printOut(); | |
b1=b2; | |
printOut(); | |
}; | |
virtual void printOut() | |
{ | |
printf("b1 = %d, b2=%d\n", b1, b2); | |
} | |
int b2=1; | |
}; | |
int main(int argc, const char * argv[]) | |
{ | |
aClass A1; | |
printf("memory address of A1=%d\n", &A1); | |
aClass A2; | |
printf("memory address of A2=%d\n", &A2); | |
bClass B1; | |
printf("memory address of B1=%d\n", &B1); | |
//Class variable assignment. | |
A1.a2=5; | |
A1.change_A1(); | |
A2=A1; | |
printf("memory address of A2=%d, after assign!\n", &A2); | |
A2.printOut(); | |
// Class instance address will not change. | |
// No matter pulick or private member variable will assign fro A1 to A2. | |
//Inherience variable assignment. | |
//B1 = A1; will failed. | |
printf("size of A1=%d\n", sizeof(A1)); // size = a1(4) + a2(4) = 8 | |
printf("size of B1=%d\n", sizeof(B1)); // size = a1(4)+a2(4) + b1(4) + b2(4) + virtual vptr(8)=24 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment