zoukankan      html  css  js  c++  java
  • C++-有静态成员变量的类继承

     

    • 声明和定义

    1.变量的定义

       变量的定义用于为变量分配存储空间,还可以为变量指定初始值。在一个程序中,变量有且仅有一个定义。

    2.变量的声明

       用于向程序表明变量的类型和名字。程序中变量可以声明多次,但只能定义一次。个人理解声明就是只能看不能用。

    3.两者联系与区别:

      (1)定义也是声明,因为当定义变量时我们也向程序表明了它的类型和名字;

      (2)但声明不是定义,可以通过使用extern关键字声明变量而不定义它。不定义变量的声明包括对象名、对象类型和对象类型前的关键字extern;

    例:

    extern int i;//声明但不定义

    int i;//声明也定义

    extern声明不是定义,也不分配存储空间。事实上,它只是说明变量定义在程序的其他地方。

    注意:如果声明有初始化式,那么它可被当作是定义,此时声明也是定义了,即使声明标记为extern,

               例如:extern double pi = 3.1416;//声明也定义,此句话虽然使用了extern,但是这条语句还是定义了pi,分配并初始化了存储空间。

    注意:只有当extern声明位于函数外部时,才可以含有初始化式。

    注意:因为初始化的extern声明已经是定义了,而变量在程序中只能定义一次,所以对该变量随后的任何定义都是错误的:

    extern double pi = 3.1416;//定义了

    double pi;//重定义,不合法

    注意:在C++语言中,变量必须仅能定义一次,而且在使用变量之前必须定义或声明变量。

    4.为什么需要区分声明和定义:

    C++程序通常由许多文件组成。为了让多个文件访问相同的变量,C++区分了声明和定义。任何在多个文件中使用的变量都需要既有定义又有声明。在这种情况下,在一个文件中定义了变量,在其他使用改变了的文件中则只能包含变量的声明(不能再包含定义,因为变量只能定义一次)。

    • 实例

    通过类的继承实现不同角色的权限控制。权限:博士生>研究生>本科生,本科生只能访问关于本科的课程信息,研究生可以访问研究生和本科的课程信息,博士生可以访问博士、研究生和本科的课程信息。

    #include<iostream>
    #include<string>
    #include"student.h"
    #include"teacher.h"
    
    using namespace std;
    
    class System{
    protected:
      //这里只是声明,没有定义
    static Student students1[100]; static Teacher teachers1[100]; static int s1; static int t1; static Student students2[100]; static Teacher teachers2[100]; static int s2; static int t2; static Student students3[100]; static Teacher teachers3[100]; static int s3; static int t3; }; class StudentSystem1 :virtual public System{ public: virtual void registe(string, string, string, string); void display1(); virtual void menu(); virtual bool search(string, string); }; class StudentSystem2 :virtual public StudentSystem1{ public: virtual void registe(string, string, string, string); void display2(); virtual void menu(); virtual bool search(string, string); }; class StudentSystem3 :virtual public StudentSystem2{ public: virtual void registe(string, string, string, string); void display3(); void menu(); bool search(string, string); };
    #include"studentsystem.h"
    
    Student System::students1[100];
    Teacher System::teachers1[100];
    int System::s1;
    int System::t1;
    Student System::students2[100];
    Teacher System::teachers2[100];
    int System::s2;
    int System::t2;
    Student System::students3[100];
    Teacher System::teachers3[100];
    int System::s3;
    int System::t3;
    
    bool StudentSystem1::search(string nu,string p){
        for (int i = 0; i < s1; i++){
            if (students1[i].getNumber()==nu&&students1[i].getPassword() == p)
                return true;
        }
        return false;
    }
    bool StudentSystem2::search(string nu, string p){
        for (int i = 0; i < s2; i++){
            if (students2[i].getNumber() == nu&&students2[i].getPassword() == p)
                return true;
        }
        return false;
    }
    bool StudentSystem3::search(string nu, string p){
        for (int i = 0; i < s3; i++){
            if (students3[i].getNumber() == nu&&students3[i].getPassword() == p)
                return true;
        }
        return false;
    }
    
    void StudentSystem1::registe(string number, string password, string name, string room){
        Student stu(number, password, name, room);
        students1[s1] = stu;
        s1++;
        cout << "注册成功" << endl;
    }
    void StudentSystem2::registe(string number, string password, string name, string room){
        Student stu(number, password, name, room);
        students2[s2] = stu;
        s2++;
        cout << "注册成功" << endl;
    }
    void StudentSystem3::registe(string number, string password, string name, string room){
        Student stu(number, password, name, room);
        students3[s3] = stu;
        s3++;
        cout << "注册成功" << endl;
    }
    
    void StudentSystem1::display1(){
        cout << "本科:"<<endl;
        for (int i = 0; i < t1; i++){
            teachers1[i].display();
        }
        system("pause");
        system("cls");
    }
    void StudentSystem2::display2(){
        cout << "研究生:" << endl;
        for (int i = 0; i < t2; i++){
            teachers2[i].display();
        }
        system("pause");
        system("cls");
    }
    void StudentSystem3::display3(){
        cout << "博士生:" << endl;
        for (int i = 0; i < t3; i++){
            teachers3[i].display();
        }
        system("pause");
        system("cls");
    }
    
    void StudentSystem1::menu(){
        char opt = ' ';
        while (opt != '2'){
            cout << "--------------------" << endl;
            cout << "1 查看课程信息" << endl;
            cout << "2 返回" << endl;
            cout << "--------------------" << endl;
            cin >> opt;
            switch (opt)
            {
            case '1':
                display1();
    
                break;
            case '2':
                break;
            default:
                cout << "输入错误" << endl;
                system("pause");
                system("cls");
                break;
            }
    
        }
    }
    void StudentSystem2::menu(){
        char opt = ' ';
        while (opt != '3'){
            cout << "--------------------" << endl;
            cout << "1 查看本科课程信息" << endl;
            cout << "2 查看研究生课程信息" << endl;
            cout << "3 注销" << endl;
            cout << "--------------------" << endl;
            cin >> opt;
            switch (opt)
            {
            case '1':
                display1();
                break;
            case '2':
                display2();
                break;
            case '3':
                break;
            default:
                cout << "输入错误" << endl;
                system("pause");
                system("cls");
                break;
            }
    
        }
    }
    void StudentSystem3::menu(){
        char opt = ' ';
        while (opt != '4'){
            cout << "--------------------" << endl;
            cout << "1 查看本科课程信息" << endl;
            cout << "2 查看研究生课程信息" << endl;
            cout << "3 查看博士生课程信息" << endl;
            cout << "4 注销" << endl;
            cout << "--------------------" << endl;
            cin >> opt;
            switch (opt)
            {
            case '1':
                display1();
    
                break;
            case '2':
                display2();
    
                break;
    
            case '3':
                display3();
    
                break;
            case '4':
                break;
            default:
                cout << "输入错误" << endl;
                system("pause");
                system("cls");
                break;
            }
    
        }
    }
  • 相关阅读:
    poj2823单调队列认知
    有关二叉树的三序遍历的题目
    hdu4757 可持续字典树
    ZOJ2532判断边是否是割集中的边
    poj2455 k条路最小化最长边
    乘法逆元模板
    poj1699 KMP+壮压DP
    Innodb存储引擎——非聚集索引
    java集合框架笔记
    jvm垃圾回收
  • 原文地址:https://www.cnblogs.com/rogersma/p/10924925.html
Copyright © 2011-2022 走看看