派生类与继承

1、下面的程序可以输出ASCII字符与所对应的数字的对照表。修改下列程序,使其可以输出字母a到z(或任意两个字符间)与所对应的数字的对照表。

class table

{

   public:

       table(int p)

       {

           i=p;

       }

      void ascii(void);

   protected:

          int i;

};

void table::ascii(void)

{

    int k=1;

    for (;i<127;i++)

    {

        cout<<setw(4)<<i<<" "<<(char)i;

        if((k)%12==0)

             cout<<" ";

        k++;

    }

    cout<<" ";

 

}

class der_table:public table

{

public:

    der_table(int p,char *m):table(p)

    {

        c=m;

    }

    void print(void);

protected:

    char *c;

};

void der_table::print(void)

{

    cout<<c<<" ";

    table::ascii();

}

int main()

{

    der_table obl(32,"ASCII value---char");

    obl.print();

    return 0;

}

提示:修改后的主程序为:

int main()

{

    der_table ob('a','z',"ASCII value---char");

    ob.print();

    return 0;

}

 

 

#include<iostream>

#include<iomanip>

using namespace std;

class table

{

protected:

    int i;

    int j;

public:

    table(int p,int q)

    {

        i=p;

        j=q;

    }

    void ascii()

    {

        int k=1;

        for(;i<=j;i++)

        {

             cout<<setw(4)<<i<<" "<<(char)i;

             if((k)%12==0)

                 cout<<endl;

             k++;

        }

        cout<<endl;

    }

};

class der_table:public table

{

protected:

    char *c;

public:

    der_table(int p,int q,char *m):table(p,q)

    {

        c=m;

    }

    void print()

    {

        cout<<c<<endl;

        table::ascii();

    }

};

int main()

{

    der_table ob('a','z',"ASCII value---char");

    ob.print();

    return 0;

}

2、已有类Time和Date,要求设计一个派生类Birthtime,它继承类Time和Date,并且增加一个数据成员Childname用于表示小孩的名字,同事设计主程序显示一个小孩的出生时间和名字。

class Time

{

    public:

        Time(int h,int m, int s)

        {

             hours=h;

             minutes=m;

             seconds=s;

        }

        void display()

        {

             cout<<"出生时间:"<<hours<<"时"<<minutes<<"分"<<seconds<<"秒"<<endl;

        }

    protected:

        int hours,minutes,seconds;

};

class Date

{

   public:

      Date(int m,int d,int y)

      {

         month=m;

         day=d;

         year=y;

      }

      void display()

      {

         cout<<"出生年月:"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;

      }

   protected:

      int month,day,year;            

};

 

 

 

#include<iostream>

#include<string>

using namespace std;

class time

{

protected:

    int hours,minutes,seconds;

public:

    time(int h,int m,int s)

    {

        hours=h;

        minutes=m;

        seconds=s;

    }

    void display()

    {

        cout<<"出生时间:"<<hours<<"时"<<minutes<<"分"<<seconds<<"秒"<<endl;

    }

};

class date

{

protected:

    int month,year,day;

public:

    date(int m,int d,int y)

    {

        month=m;

        day=d;

        year=y;

    }

    void display()

    {

        cout<<"出生年月"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;

    }

};

    class birthtime:public time,public date

    {

    private:

        string childname;

    public:

        birthtime(int h,int m,int s,int m1,int d,int y,string n):time(h,m,s),date(m1,d,y)

        {

             childname=n;

        }

        void display()

        {

           time::display();

           date::display();

             cout<<"名字是:"<<childname<<endl;

        }

    };

    int main()

    {

        birthtime a(0,6,33,6,29,2000,"jassic");

        a.display();

        return 0;

    }

3、编写一个学生和教师的数据输入和显示程序。学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。要求将编号、姓名、性别、年龄的输入和显示设计成一个类Person,并作为学生类Student和教师类Teacher的基类。

 

 

 

#include<iostream>

#include<string>

using namespace std;

class person

{

private:

    int num;

    string name;

public:

    person(int n,string na)

    {

        cout<<"putin the num:";

        cin>>n;

        num=n;

        cout<<endl<<"putin the name:";

        cin>>na;

        name=na;

    }

    void show()

    {

        cout<<"the name is:"<<name<<endl<<"the number is:"<<num<<endl;

    }

};

class student:public person

{

private:

    int classnum;

    float score;

public:

    student(int n,string na,int cn,float s):person(n,na)

    {

        cout<<"please putin the classnum:";

        cin>>cn;

        cout<<endl<<"please putin the score:";

        classnum=cn;

        cin>>s;

        score=s;

    }

    void show1()

    {

        show();

        cout<<"the classnumber is:"<<classnum<<endl<<"the score is:"<<score<<endl;

    }

};

class teacher:public person

{

private:

    string title;

    string depart;

public:

    teacher(int n,string na,string t,string d):person(n,na)

    {

        cout<<"please putin the title:";

        cin>>t;

        title=t;

        cout<<endl<<"please putin the department:";

        cin>>d;

        depart=d;

    }

    void show2()

    {

        show();

        cout<<"the title is :"<<title<<endl<<"the department is:"<<depart<<endl;

    }

};

int main()

{

    int m,cln;

    string n;

    float sc;

    student s(m,n,cln,sc);

    s.show1();

    string t;

    string d;

    teacher te(m,n,t,d);

    te.show2();

    return 0;

}

4、给出下面的基类:

class area

{

   protected:

       double height;

       double width;

   public:

       area(double h,double w)

       {

           height=h;

           width=w;

       }

       virtual double getarea()=0;

};

【要求】

(1)建立基类area的俩个派生类rectangle与isosceles,让每一个派生类都包含一个函数getarea(),分别用来返回矩形与三角形的面积。用构造函数对height与width进行初始化。

(2)写出主程序,用来求height与width分别为10.0与5.0的矩形面积,以及求height与width分别为4.0与6.0的三角形面积。

(3)要求通过使用基类指针访问虚函数的方法(即运行时的多态性)分别求出矩形和三角形面积。

 

 

#include<iostream>

using namespace std;

class area

{

protected:

    double height;

    double width;

public:

    area(double h,double w)

       {

           height=h;

           width=w;

       }

       virtual double getarea()=0;

};

class rectangle:public area

{

private:

    double a;

public:

    rectangle(double a,double b):area(a,b)

    {}

    virtual double getarea()

    {

        a=height*width;

        return a;

    }

};

class isosceles:public area

{

private:

    double a;

public:

    isosceles(double a,double b):area(a,b)

    {}

    virtual double getarea()

    {

        a=0.5*height*width;

        return a;

    }

};

    int main()

{

    area *mp;

    rectangle r(10.0,5.0);

    mp=&r;

    cout<<"the area of rectangle is:"<<mp->getarea()<<endl;

    isosceles i(4.0,6.0);

    mp=&i;

    cout<<"the area of isosceles is:"<<mp->getarea()<<endl;

    return 0;

}

5、编写一个程序,递归调用被继承的基类成员函数,实现求素数的功能。

 

 

#include<iostream>

using namespace std;

class base

{

private:

    int x;

public:

    base(int p)

    {

        x=p;

    }

    int fun(int j)

    {

        int t=2,f=1;

        for(;t<j&&f==1;t++)

        {

             if(j%t==0)

                 f=0;

             return f;

        }

    }

};

class derive:public base

{

protected:

    char *c;

public:

    derive(char *m):base(2)

    {

        c=m;

    }

    int fun1(int l)

    {

        return base::fun(l);

    }

};

int main()

{

    derive a("base ");

    int n;

    cout<<"please input a int :";

    cin>>n;

    if(a.fun1(n)==1)

        cout<<n<<"is prime number"<<endl;

    else

        cout<<n<<"is not prime number"<<endl;

    return 0;

}

朝闻道
原文地址:https://www.cnblogs.com/wander-clouds/p/8443774.html