Virfunc.cpp
资源名称:C++100.rar [点击查看]
上传用户:wszmarenbt
上传日期:2013-04-26
资源大小:2552k
文件大小:1k
源码类别:
Windows编程
开发平台:
Visual C++
- //THE PROGRAM IS TO REALITY THE USE OF THE VIRTUAL METHOD
- //FILE VIRFUNC.CPP
- #include <stdio.h>
- #include <conio.h>
- class PARENT
- {
- protected:
- int DATA;
- public:
- virtual void READ_NUM()
- {
- printf("nNow,this is function in PARENT");
- printf("nInput a number please:");
- scanf("%d",&DATA);
- }
- virtual int GET_NUM()
- {
- printf("Now,this is function in PARENT");
- return DATA;
- }
- };
- class DERIVED:public PARENT
- {
- protected:
- int DATA;
- public:
- void READ_NUM()
- {
- printf("nNow,this is function in DERIVED");
- printf("nInput a number please:");
- scanf("%d",&DATA);
- }
- int GET_NUM()
- {
- printf("Now,this is function in DERIVED");
- return DATA;
- }
- };
- int main(void)
- {
- int NUM;
- PARENT *P,X;
- DERIVED Y;
- clrscr();
- P=&X;
- P->READ_NUM();
- NUM=P->GET_NUM();
- printf("nNUM (PARENT) =%dn",NUM);
- P=&Y;
- P->READ_NUM();
- NUM=P->GET_NUM();
- printf("nNUM (DERIVED) =%dn",NUM);
- getch();
- return 0;
- }