重载与覆盖

重载与覆盖

成员函数被重载的特征:

(1)相同的范围(在同一个类中);

(2)函数名字相同;

(3)参数不同;

(4)virtual 关键字可有可无。

 

 覆盖是指派生类函数覆盖基类函数,特征是:

(1)不同的范围(分别位于派生类与基类);

(2)函数名字相同;

(3)参数相同;

(4)基类函数必须有 virtual 关键字。

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6     int i;
 7     //定义结构类型 
 8     struct student {
 9            int  num;
10            char  name[10];
11            float maths;
12            float physics;
13            float chemistry;
14            double  total;
15     };
16 
17      //声明结构数组st
18      student st[3];
19 
20      //从键盘上为结构数组输入值 
21      cout<<"    num  name     maths physics chemistry "<<endl;
22      for (i=0;i<3;i++)
23      {
24         cout<<i+1<<"   ";
25         cin>>st[i].num;
26         cin>>st[i].name;
27         cin>>st[i].maths;
28         cin>>st[i].physics;
29         cin>>st[i].chemistry;
30      }
31 
32     //计算每个学生的总成绩
33     for (i=0;i<3;i++)
34          st[i].total=st[i].maths+st[i].physics+st[i].chemistry;
35 
36     //输出结构数组各元素的值 
37     for (i=0;i<3;i++)
38     {
39         cout<<"st["<<i<<"]:   ";
40         cout<<st[i].num<<'\t';
41         cout<<st[i].name<<'\t';
42         cout<<st[i].maths<<'\t';
43         cout<<st[i].physics<<'\t';
44         cout<<st[i].chemistry<<'\t';
45         cout<<st[i].total<<endl;
46      }
47     return 0;
48 }

 

posted @ 2018-08-02 12:45  borter  阅读(121)  评论(0编辑  收藏  举报