• 类基类实验5 派生与继承


    每日一贴,今天的内容关键字为类基类

        1.试验目标和要求:

       此试验用两个单元的时光完成,是一个综合性的试验,要求编写的程序涉及到C++的许多知识点,如类的定义,动态分配内存,构造函数,派生,虚基类等。试验内容给出的是一个完全的程序,4个题目实际是类品级。同窗也可以在此基础上发挥增加新的内容。

        2.试验内容:

        1.定义一个基类MyArray,基类中可以寄存一组数组。

        class Myarray

        {

        int *alist;

        int length;

        public:

        Myarray( int leng);

        ~Myarray();

        void input();

        void display();

        };

        编写构造函数,析构函数及其它函数,实现动态分配内存,释放内存和数据输入输出等功能。并进行调试。

    #include<iostream>
    using namespace std;
    class Myarray
    {
        int *alist;
        int length;
    public:
        Myarray();
        Myarray( int leng);
        ~Myarray();
        void input();
        void display();
    };
    Myarray::Myarray()
    {
        length=0;
        alist=NULL;
    }
    Myarray::Myarray(int leng)
    {
        cout<<"Constructing"<<endl;
        length=leng;
        alist=new int[length+1];
    }
    Myarray::~Myarray()
    {
        if(alist!=NULL)
        {
            delete []alist;
            length=0;
            cout<<"Destructing"<<endl;
        }
    }
    void Myarray::input()
    {
        if(alist!=NULL)
        {
            cout<<"Input"<<length<<" numbers for Myarray"<<endl;
            for(int i=0; i<length; i++)
            {
                cin>>alist[i];
            }
        }
    }
    void Myarray::display()
    {
        cout<<"Display elements of Myarray"<<endl;
        for(int i=0; i<length; i++)
        {
            cout<<alist[i]<<" ";
        }
        cout<<endl;
    }
    int main()
    {
        Myarray a(10);
        a.input();
        a.display();
        return 0;
    }

        2.定义一个类averarray继承自myarray,在类中求数组的平均值,并输出。进行调试。

        3.定义一个类revarray继承自 myarray, 使数组按反序寄存,并输出。进行调试。

        4.  定义一个类Nawarray继承自averarray和revarray。在继承过程当中声明为虚基类,领会虚基类在解决二义性中的问题中的作用。调试中可以试一试不必虚基类出现的问题。

    #include<iostream>
    using namespace std;
    class Myarray//定义一个基类MyArray,基类中可以寄存一组数组
    {
    public:
        int *alist;
        int length;
    
        Myarray();
        Myarray( int leng);
        ~Myarray();
        void input();
        void display();
    };
    Myarray::Myarray()
    {
        length=0;
        alist=NULL;
    }
    Myarray::Myarray(int leng)
    {
        cout<<"Constructing"<<endl;
        length=leng;
        alist=new int[length+1];
    }
    Myarray::~Myarray()
    {
        if(alist!=NULL)
        {
            delete []alist;
            length=0;
            cout<<"Destructing"<<endl;
        }
    }
    void Myarray::input()
    {
        if(alist!=NULL)
        {
            cout<<"Input"<<length<<" numbers for Myarray"<<endl;
            for(int i=0; i<length; i++)
            {
                cin>>alist[i];
            }
        }
    }
    void Myarray::display()
    {
        cout<<"Display elements of Myarray"<<endl;
        for(int i=0; i<length; i++)
        {
            cout<<alist[i]<<" ";
        }
        cout<<endl;
    }
    class Averarray:virtual public Myarray//类averarray继承自myarray,
    {
    public:
        double aver;
        Averarray(int n):Myarray(n)
        {
            cout<<"Averarray Constructing"<<endl;
            aver=0;
        }
        ~Averarray()
        {
            cout<<"Averarray Destructing"<<endl;
        }
        void getAver()
        {
            for(int i=0; i<length; i++)
            {
                aver+=alist[i];
            }
            cout<<"Aver="<<aver/length<<endl;
        }
    };
    class Revarray:virtual public Myarray//类Revarray继承自myarray,
    {
    public:
        Revarray(int n):Myarray(n)
        {
            cout<<"Revarray Constructing"<<endl;
        }
        ~Revarray()
        {
            cout<<"Revarray Destructing"<<endl;
        }
        void Reverse()
        {
            cout<<"The original Revarray"<<endl;
            display();
            for(int i=0; i<(length)/2; i++)
            {
                int temp=alist[i];
                alist[i]=alist[length-1-i];
                alist[length-1-i]=temp;
            }
            cout<<"After reverse"<<endl;
            display();
        }
        void display()
        {
            cout<<"Display elements of Nawarray"<<endl;
            for(int i=0; i<length; i++)
                cout<<alist[i]<<" ";
            cout<<endl;
        }
    };
    class Nawarray:public Averarray,public Revarray
    {
    public:
        Nawarray(int length):Averarray(length),Revarray(length),Myarray(length)
        {
            cout<<"Nawarray Constructing"<<endl;
        }
        ~Nawarray()
        {
            cout<<"Nawarray Destructing"<<endl;
        }
        void display()
        {
            cout<<"Display elements of Nawarray"<<endl;
            for(int i=0; i<length; i++)
                cout<<alist[i]<<" ";
            cout<<endl;
        }
    };
    int main()
    {
        Myarray m(5);
        m.input();
        m.display();
        Averarray a(5);
        a.input();
        a.getAver();
        Revarray r(5);
        r.input();
        r.Reverse();
        Nawarray n(5);
        n.input();
        n.display();
        return 0;
    }
        每日一道理
    听,是谁的琴声,如此凄凉,低调的音,缓慢的节奏,仿佛正诉说着什么。音低调得略微有些抖动,听起来似乎心也有些抖动,我感觉到一种压抑的沉闷气息,是否已凝结在这空气中……

        5. 读程序,回答问题

    #include <iostream>
    using namespace std;
    class A
    {
    public:
        void f1();
        A()
        {
            i1=10;
            j1=11;
        }
    protected:
        int j1;
    private:
        int i1;
    };
    class B:private A
    {
    public:
        void f2();
        B()
        {
            i2=20;
            j2=21;
        }
    protected:
        int j2;
    private:
        int i2;
    };
    class C: public B
    {
    public:
        void f3();
        C()
        {
            i3=30;
            j3=31;
        }
    protected:
        int j3;
    private:
        int i3;
    };
    int main()
    {
        return 0;
    }

        回答以下问题:

        派生类B中成员函数f2()能否拜访基类A中的成员f1()、i1和j1?

        派生类B的对象b能否拜访基类A中的成员f1()、i1和j1?

        派生类C中成员函数f3()能否拜访直接基类B中的成员f2()、i2和j2?能否拜访直接基类A中的成员f1()、j1和i1?

        派生类C的对象c能否拜访直接基类B中的成员f2()、i2和j2?能否拜访直接基类A中的成员f1()、j1和i1?

        6. 编写一个程序,实现字符串操纵:

        一个简单串类string,包括输入字符串、返回字符串长度及内容等功能;尚有一个具有编辑功能的串类edit_string,它继承string类,在其中设置一个光标,使其能支持在光标处的插入、替换和删除等编辑功能。

    #include<iostream>
    #include<cstring>
    using namespace std;
    class String
    {
    public:
        int length;
        char *s;
        String(char *str=NULL);      //普通构造函数
        String(const String &other); //拷贝构造函数
        ~ String(void);              // 析构函数
        void print();
        int getLen();
    };
    String::String(char *str)
    {
        if(str)
        {
            this->length=strlen(str);
            s= new char[length+1];
            strcpy(s,str);
        }
    }
    String::String(const String &other)
    {
        length=strlen(other.s);
        s= new char[length+1];
        strcpy(s,other.s);
    }
    String::~String()
    {
        cout<<"Destructing String "<<s<<endl;
        if(s) delete []s;
        length=0;
    }
    void String::print()
    {
        cout<<s<<endl;
    }
    int String::getLen()
    {
        return length;
    }
    
    class edit_String:public String
    {
    public:
        edit_String(char *s):String(s)
        {
            cout<<"Constructing edit_String"<<endl;
        }
        void Delete(int p);         //实现删除功能
        void Insert(int p,char c);  //实现插入功能
        bool Replace(int p,char c); //实现替换功能
    };
    void edit_String::Delete(int p) //实现删除功能
    {
        if(s==NULL)
        {
            cout<<"Fail to delete,s is empty"<<endl;
            return;
        }
        if(p>=length)
        {
            cout<<"p is too big"<<endl;
            return;
        }
        for(int i=0; i<length-1; i++)
        {
            if(i>=p-1)
            {
                s[i]=s[i+1];
            }
        }
        length--;
    }
    bool edit_String::Replace(int p,char c)//实现替换功能
    {
        if(s==NULL)
        {
            cout<<"Fail to replace,s is empty"<<endl;
            return false;
        }
        if(p>=length)
        {
            cout<<"p is too big"<<endl;
            return false;
        }
        for(int i=0; i<length; i++)
        {
            if(i==p)
            {
                s[i]=c;
                return true;
            }
        }
        return false;
    }
    void edit_String::Insert(int p,char c)//实现插入功能
    {
        char* temp=new char[length+1];
        for(int i=0; i<=length; i++)
        {
            if(i<p)
                temp[i]=s[i];
            else if(i==p)
            {
                temp[i]=c;
            }
            else
            {
                temp[i]=s[i-1];
            }
        }
        if(s!=NULL) delete s;
        s= new char[strlen(temp)+1];
        length++;
        strcpy(s,temp);
        delete []temp;
    }
    
    int main()
    {
        char s1[9]="dutclass";
        edit_String es(s1);
        es.print();
        es.Delete(3);
        es.print();
        es.Replace(1,'m');
        es.print();
        es.Insert(3,'w');
        es.print();
        return 0;
    }

    文章结束给大家分享下程序员的一些笑话语录: PC软件体积大,是因为一个PC软件功能往往较多,能够满足你一个方面的需求,而一个iphone软件往往没几行代码,干一件很小的事情,自然需要的软件就多。就像吃西瓜和吃瓜子的来比数目,单位不同啊。

  • 相关阅读:
    day24 Pyhton学习 反射
    正则表达式练习
    day23 Pyhton学习 昨日回顾.re模块.序列化模块
    day22 函数整理
    day22 Pyhton学习 re模块和正则表达式
    day21 Pyhton学习 模块
    函数整理
    一个关于浮点数运算需要注意的地方
    关于逻辑运算案例笔记
    数据的表现形式和进制之间的转换
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3086075.html
Copyright © 2020-2023  润新知