DERIVED.CPP
资源名称:C++100.rar [点击查看]
上传用户:wszmarenbt
上传日期:2013-04-26
资源大小:2552k
文件大小:2k
源码类别:
Windows编程
开发平台:
Visual C++
- //THIS PROGRAM IS THE FIRST STEP TO LEARN THE DERIVED CLASS
- //FILE DERIVED.CPP
- #include <stdio.h>
- #include <conio.h>
- class PARENT
- {
- private :
- int NUM_1;
- public :
- PARENT(){};
- void ASSIGN_PRI_1(int NUM1)
- {
- NUM_1=NUM1;
- }
- void NUM_1_INC()
- {
- NUM_1+=1;
- }
- void DISPLAY()
- {
- printf("n NUM_1(PARENT PRIVATE NUMBER)=%d",NUM_1);
- }
- int GET_NUM_1()
- {
- return NUM_1;
- }
- };
- class PRIVATE_DERIVED : private PARENT //Private Inherit
- {
- private :
- int NUM_2;
- public :
- PRIVATE_DERIVED(int NUM2)
- {
- NUM_2=NUM2;
- }
- void ASSIGN_PRI_1(int NUM1)
- {
- PARENT::ASSIGN_PRI_1(NUM1);
- }
- void NUM_1_INC()
- {
- if (NUM_2>5)
- PARENT::NUM_1_INC();
- }
- void NUM_2_INC()
- {
- NUM_2+=1;
- }
- void DISPLAY()
- {
- PARENT::DISPLAY();
- printf("n NUM_2(PRIVATE DERIVED PRIVATE NUMBER)=%dn",NUM_2);
- }
- };
- class PUBLIC_DERIVED : public PARENT //Public Inherit
- {
- private :
- int NUM_3;
- public :
- PUBLIC_DERIVED(int NUM3)
- {
- NUM_3=NUM3;
- }
- void NUM_1_INC()
- {
- if (PARENT::GET_NUM_1()<20)
- PARENT::NUM_1_INC();
- }
- void NUM_3_INC()
- {
- NUM_3+=1;
- }
- void DISPLAY()
- {
- PARENT::DISPLAY();
- printf("n NUM_3(PUBLIC DERIVED PRIVATE NUMBER)=%d",NUM_3);
- }
- };
- int main(void)
- {
- clrscr();
- PARENT PRE;
- printf("nnBEFORE increasing :");
- PRIVATE_DERIVED PRI_D(2);
- PRI_D.ASSIGN_PRI_1(99);
- PRI_D.DISPLAY();
- printf("nnAFTER increasing : ");
- PRI_D.NUM_1_INC();
- PRI_D.NUM_2_INC();
- PRI_D.DISPLAY();
- printf("nn---- Because NUM_2 < 5,NUM_1 did not increase! ----");
- printf("nnPRESS ANY KEY TO CONTINUE... ...");
- getch();
- clrscr();
- printf("nnBEFORE increasing :");
- PUBLIC_DERIVED PUB_D(2);
- PUB_D.ASSIGN_PRI_1(25);
- PUB_D.DISPLAY();
- printf("nnAFTER increasing : ");
- PUB_D.NUM_1_INC();
- PUB_D.NUM_3_INC();
- PUB_D.DISPLAY();
- printf("nn---- Because NUM_1 > 20,NUM_1 did not increase! ----");
- printf("nnPRESS ANY KEY TO CONTINUE... ...");
- getch();
- clrscr();
- printf("nnBEFORE increasing :");
- PUB_D.ASSIGN_PRI_1(25);
- PUB_D.DISPLAY();
- printf("nnAFTER increasing : ");
- PUB_D.PARENT::NUM_1_INC();
- PUB_D.NUM_3_INC();
- PUB_D.DISPLAY();
- printf("nn---- Because use the NUM_1_INC() of PARENT CLASS,"
- " the limitation in PUBLIC_DERIVED did not available!"
- " Though NUM_1 > 20 ,NUM_1 increased !");
- printf("nnPRESS ANY KEY TO EXIT... ...");
- getch();
- return 0;
- }