zoukankan      html  css  js  c++  java
  • 重载与覆盖

    重载与覆盖

    成员函数被重载的特征:

    (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<<'	';
    41         cout<<st[i].name<<'	';
    42         cout<<st[i].maths<<'	';
    43         cout<<st[i].physics<<'	';
    44         cout<<st[i].chemistry<<'	';
    45         cout<<st[i].total<<endl;
    46      }
    47     return 0;
    48 }
  • 相关阅读:
    IBinder在进程之间传递一个对象的形式(一)
    Xaml在string(串)定义常量和处理空间
    c 有意思的数组初始化
    C 文件直接包含
    [面试技巧]16个经典面试问题回答思路
    centos6安装bt工具transmission
    clearcase 中一些概念和操作
    C/C++ Resources
    Linux I/O 重定向详解及应用实例
    c/c++ 直接使用动态库 dlopen
  • 原文地址:https://www.cnblogs.com/borter/p/9406509.html
Copyright © 2011-2022 走看看