NEFU C++实验四派生类与继承(锐格)

5362、

题目内容:

下面的程序可以输出ASCII字符与所对应的数字的对照表,修改下列程序,使其可以输出字母a到字母z。

输入输出说明:

输出:每隔12个字母换行,设置输出的域宽为4
ASCII value-char 
  97a  98b  99c 100d 101e 102f  
 103g 104h 105i 106j 107k 108l
 109m 110n 111o 112p 113q 114r
 115s 116t 117u 118v 119w 120x
 121y 122z
#include<iostream>
#include<iomanip>
using namespace std;
class  table{
public:
    table (int p){
        i=p;
    }
    void ascii(void);
protected:
    int i;
};
void table ::ascii(){
    int k=1;
    for(;i<97+26;i++){
        cout<<setw(4)<<i<<(char)i;
        if(k%6==0){
            cout<<endl;
        }
        k++;
    }
}
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(){
    cout<<c<<endl;
    table::ascii();
}
int main(){
    der_table ob1(97,"ASCII value-char");ob1.print();
}

 5522、

題目內容:

下面的程序包含了Time类和Date类的声明,要求设计一个Birthtime类,它继承了Time类和Date类,并且还有一项出生孩子的名字Childname,同时设计主程序显示一个小孩的出生时间和名字。

输入输出说明:

输出:
dcc
9:18:18
11/22/2007
#include <iostream>
#include <string.h>
using namespace std;
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 << month << "/" << day << "/" << year << endl; }

protected:
    int month, day, year;
};
class Birthtime : public Date,public Time
{
public:
    Birthtime(char *m, int mon, int d, int y, int h, int min, int s) : Date(mon, d, y), Time(h, min, s)
    {
        Childname = m;
    }
    void display()
    {
        cout << Childname <<endl;
      
        Time::display();
          Date::display();
    }

protected:
    char *Childname;
};
int main(){
    Birthtime ob1("dcc",11,22,2007,9,18,18);
    ob1.display();
}

5531、

題目內容:

建立普通的基类building,用来存储一座楼房的层数,房间数以及它的总平方数。建立派生类house,继承building,并存储卧室与浴室的数量,另外,建立派生类office,继承building,并存储灭火器与电话的数目。

输入输出说明:

定义house 类的对象时给的初值分别为8,4,32,4,600.0
定义building 类的对象时给的初值分别为40,40,15,40,2000.0
输出:
floor is 32 house is 4 square is 600
bedroom is 8 bathroom is 4
floor is 15 house is 40 square is 2000
extinguisher is 40 telephone is 40
#include <iostream>
using namespace std;
class building
{
protected:
    int floor, house;
    double squre;

public:
    building(int floor, int house, double squre)
    {
        this->floor = floor;
        this->house = house;
        this->squre = squre;
    }
    void display()
    {
        cout << "floor is" << floor << "house is" << house << "square is" << squre << endl;
    }
};
class house1 : public building
{
protected:
    int beedroom, bathroom;

public:
    house1(int floor, int house, double squre, int beedroom, int bathroom): building(floor, house, squre)
    {
        this->bathroom = bathroom;
        this->beedroom = beedroom;
    }
    void display()
    {
        cout << "floor is " << floor << " house is " << house << " square is " << squre << endl;
        cout << "bedroom is " << beedroom << " bathroom is " << bathroom << endl;
    }
};
class office : public building
{
protected:
    int extinguisher, telephone;
public:
    office(int floor, int house, double squre,int extinguisher,int  telephone):building(floor,house,squre){
        this->extinguisher=extinguisher;
        this->telephone=telephone;
    }
    void display(){
        cout << "floor is " << floor << " house is " << house << " square is " << squre << endl;
        cout <<"extinguisher is " <<extinguisher<< " telephone is "<<telephone <<endl;
    }

};
 int main()
{
    house1 ob1(32, 4, 600.0,8,4);
    ob1.display();
    office ob2(15, 40,2000.0, 40, 40 );
    ob2.display();
}

 5532、

題目內容:

按照要求编写程序。说明person类:含有成员name和id;说明teacher类和student类,teacher类含有成员degree和dep,student类含有成员old和sno。这两个类继承person类;说明stud类,含有成员addr和tel;说明score类,含有成员math和eng。该类继承student类和stud类。定义属于类score的对象c1及类teacher的对象t1,分别输入各数据成员的值后再显示出这些数据。

输入输出说明:

输出:
name is dcc id is 23010620071122
old is 10 sno is 201304
addr is Haping Rord tel is 13804582160
math is 94 eng is 100
name is dbz id is 23010719750412
degree is Ph.D dep is computer
#include<iostream>
using namespace std;
class person{
protected:
    string name;
    string id;
public:
    person(string name,string id){
        this->name=name;
        this->id=id;
    }
    void output(){
        cout<<"name is "<<name<<" id is "<<id<<endl;
    }
};
class teacher: public person{
protected:
    string degree,dep;
public:
    teacher(string name,string id,string degree,string dep):person(name,id){
        this->degree=degree;
        this->dep=dep;
    }
    void output(){
        person::output();
        cout<<"degree is "<<degree<<" dep is "<<dep<<endl;
    }
};
class student: public person{
protected:
    int old,sno;
public:
    student(string name,string id,int old,int sno):person(name,id){
        this->old=old;
        this->sno=sno;
    }
    void output(){
        person::output();
        cout<<"old is "<<old<<" sno is "<<sno<<endl;
    }
};
class stud{
protected:
    string addr ,tel;
public:
    stud(string addr,string tel){
        this->tel=tel;
        this->addr=addr;
    }
    void output(){
        cout<<"addr is "<<addr<<" tel is "<<tel<<endl;
    }
};
class score:public stud,public student{
protected:
    int math,eng;
public:
    score(string name,string id,int old,int sno,string addr,string tel,int math,int eng):student(name,id,old,sno),stud(addr,tel){
        this->math=math;
        this->eng=eng;
    }
    void output(){
        student::output();
        stud::output(); 
        cout<<"math is "<<math<<" eng is "<<eng<<endl;
    }
};
int main(){
    score c1("dcc","23010620071122",10,201304,"Haping Road","13804582160",94,100);
    c1.output();
    teacher t1("dbz","23010719750412","Ph.D","Computer");
    t1.output();
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李小于

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值