作业4 派生类与继承

【4.19】输出字母a-z所对应的数字的对照表。

#include<iostream>
using namespace std;
#include<iomanip>

class table{
    public:
        table(int p, int q){
            i=p;
            j=q;
        }
        void ascii(void);
    protected:
        int i;
        int j;
};
void table::ascii(void){
    int k=1;
    for(;i<=j;i++){
        cout<<setw(4)<<i<<" "<<(char)i;
        if(k%12==0){
            cout<<"\n";
        }
        k++;
    }
    cout<<"\n";
}

class der_table:public table{
    public:
        der_table(char p, char q, char *m):table(p,q){
            c=m;
        }
        void print(void);
    protected:
        char *c;
};
void der_table::print(void){
    cout<<c<<"\n";
    table::ascii();
}

int main(){
    der_table ob1('a','z', "ASCII value---char");
    ob1.print();
    return 0;
}

【4.20】

#include<iostream>
using namespace std;
#include<iomanip>

class area_c1{
    protected:
        double height;
        double width;
    public:
        area_c1(double r, double s){
            height=r;
            width=s;
        }
        virtual double area()=0;
};

class rectangle:public area_c1{
    public:
        rectangle(double r, double s):area_c1(r, s){};
        double area(){
            return height*width;
        }
};

class isosceles:public area_c1{
    public:
        isosceles(double r, double s):area_c1(r, s){};
        double area(){
            return height*width/2;
        }
};

int main(){
    area_c1 *p;
    rectangle a(10.0, 5.0);
    isosceles b(4.0, 6.0);
    p=&a;
    cout<<"rectangle's area = "<<p->area()<<endl;
    p=&b;
    cout<<"iosceles's area = "<<p->area()<<endl;
    return 0;
}

【4.21】

#include<iostream>
using namespace std;
#include<string>

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;
        int minutes;
        int 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;
};

class Birthtime:public Time,public Date{
    public:
        Birthtime(int ho, int mi, int se, int mo, int da, int ye, string n):Time(ho, mi, se),Date(mo, da, ye){
            name=n;
        }
        void display(){
            cout<<"名字:";
            cout<<name<<endl;
            Date::display();
            Time::display();
        }
    protected:
        string name;
};

int main(){
    Birthtime a(20, 36, 10, 5, 23, 2020, "Marry" );
    a.display();
    return 0;
}

在这里插入图片描述
【4.22】编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称、部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。

#include<iostream>
using namespace std;

class person{
    protected:
            int id;
            string name;
    public:
        void input(){
            cout<<"姓名:";
            cin>>name;
            cout<<"编号:";
            cin>>id;
        }
        void display(){
            cout<<"姓名:"<<name<<endl;
            cout<<"编号:"<<id<<endl;
        }
};
class student: public person{
    protected:
        int Class;
        double Score;
    public:
        void input(){
            person::input();
            cout<<"班号:";
            cin>>Class;
            cout<<"成绩:";
            cin>>Score;
        }
        void display(){
            person::display();
            cout<<"班号:"<<Class<<endl;
            cout<<"成绩:"<<Score<<endl;
        }
};

class teacher: public person{
    protected:
        string position;
        string department;
    public:
        void input(){
            person::input();
            cout<<"职称:";
            cin>>position;
            cout<<"部门:";
            cin>>department;
        }
        void display(){
            person::display();
            cout<<"职称:"<<position<<endl;
            cout<<"部门:"<<department<<endl;
        }
};

int main(){
    student s;
    teacher t;
    cout<<"输入一个学生数据:"<<endl;
    s.input();
    cout<<"输入一个教师数据:"<<endl;
    t.input();
    cout<<"显示学生数据:"<<endl;
    s.display();
    cout<<"显示教师数据:"<<endl;
    t.display();   
    return 0;
}

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

#include<iostream>
using namespace std;
#include<string>
#include<cmath>
class prime{
    private:
        int x;
    public:
        prime(int p){
            x=p;
        }
        int pri_function(int j);
};

int prime::pri_function(int j){
    int flag=1;
    for(int t=2; t<sqrt(j) && flag==1; t++)
        if(j%t==0) flag=0;
    return flag;
}
class derived:public prime{
    protected:
        char *c;
    public:
        derived(char *m):prime(2){
            c=m;
        }
        int pf(int l){
            return prime::pri_function(l);
        }
};
int main(){
    derived obj("prime:\n");
    int n;
    cout<<"please input a int:";
    cin>>n;
    if(obj.pf(n)==1)
        cout<<n<<" is prime."<<endl;
    else
        cout<<n<<" not prime."<<endl;
    return 0;
}

【4.24】编写一个程序,递归调用被继承的基类成员函数,求最大公约数。

#include<iostream>
using namespace std;
class gcd_class{
    private:
        int n;
        int d;
    public:
        gcd_class(int p, int q){
            n=p;d=q;
        }
        long func(int j, int k);
};
long gcd_class::func(int j, int k){
    if(k==0)
        return j;
    else
        return func(k, j%k);    
}
class derived:public gcd_class{
    protected:
        char *c;
    public:
        derived(char *m):gcd_class(1,1){
            c=m;
        }
        long pfunc(int l, int r){
            return gcd_class::func(l,r);
        }
};

int main(){
    derived obj("output n!\n");
    int n,d;
    cout<<"please intput 2 int: ";
    cin>>n>>d;
    cout<<"gcd("<<n<<","<<d<<")="<<obj.pfunc(n,d)<<endl;
    return 0;
}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值