C++实验18

一、圆类派生

#include<iostream>
using namespace std;
#define pi 3.1415926

class circle
{
    float r;
public:
    circle(float a=0)
    {
        r=a;
        cout<<"circle constructor"<<endl;
    }
    ~circle(){cout<<"circle destructor"<<endl;}
    float getr(){return r;}
    float area();
    void display();
};
float circle::area() {return r*r*pi;}
void circle::display() {cout<<"r="<<r<<endl;}

class cylinder:public circle
{
    float hight;
public:
    cylinder(float r1, float hight1):circle(r1)
    {
        hight=hight1;
        cout<<"cylinder consturctor"<<endl;
    }
    ~cylinder(){cout<<"cylinder destructor"<<endl;}
    float volume() {return area()*hight;}
    void display() {cout<<"base r="<<getr()<<"\t inherit hight="<<hight<<endl;}
};
void main()
{
    cylinder a(2,4);
    a.circle::display();
    //支配规则。若相同,派生类的成员将覆盖基类中的同名成员。
    //:: 域作用符只能向上调用一级。
    a.display();
    cout<<"S="<<a.area()<<endl;
    cout<<"volume="<<a.volume()<<endl;
}

二、人员类派生

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

class person
{
    int id;
    char name[20];
    char sex[6];
    int age;
public:
    person(int a, char b[], char c[], int d)
    {
        id=a;strcpy(name,b);strcpy(sex,c);age=d;
        cout<<"person constructor"<<endl;
    }
    ~person(){cout<<"person destructor"<<endl;}
    void displayp()
    {
        cout<<"id  name  sex  age\n";
        cout<<id<<'\t'<<name<<'\t'<<sex<<'\t'<<age<<endl;
    }
};
class teacher:public person
{
    int classtime;
    float fund;
public:
    teacher(int id1,char name1[],char sex1[],int age1,int classtime1, float fund1):person(id1,name1,sex1,age1)
    {
        classtime=classtime1;
        fund=fund1;
        cout<<"teacher constructor"<<endl;
    }
    ~teacher(){cout<<"teacher destructor"<<endl;}
    void display()
    {
        cout<<"id  name  sex  age  classtime fund\n";
        displayp();
        // display();//递归
        cout<<classtime<<'\t'<<fund<<endl;
    }
};
void main()
{
    teacher a(20171213,"张三","man",21,20,20000);
    a.display();
}

三、学生、课程类共同派生

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

class student
{
    int id;
    char name[20];
    char sex[6];
    int age;
public:
    student(int a,char *b,char *c,int d)
    {
        id=a;strcpy(name,b);strcpy(sex,c);age=d;
        cout<<"student constructor"<<endl;
    }
    ~student(){cout<<"student destructor"<<endl;}
    void stu_display()
    {
        cout<<"id  name  sex  age\n";
        cout<<id<<'\t'<<name<<'\t'<<sex<<'\t'<<age<<endl;
    }
};

class course
{
    int id;
    char name[20];
    int period;
public:
    course(int a, char *b, int c)
    {
        id=a;strcpy(name,b);period=c;
        cout<<"course constructor"<<endl;
    }
    ~course(){cout<<"course destructor"<<endl;}
    void cou_display()
    {
        cout<<"id  name  period"<<endl;
        cout<<id<<'\t'<<name<<'\t'<<period<<endl;
    }
};

class sel_cou:public course,public student //定义course在前,故构造在前,析构在后
//注意继承的两个基中间逗号
{
    float grade;
public:
    sel_cou(int a,char *b,char *c,int d,int a1, char *b1, int c1,float e):student(a,b,c,d),course(a1,b1,c1)
    {
        grade=e;
        cout<<"sel_cou constructor\n";
    }
    ~sel_cou(){cout<<"sel_cou destructor"<<endl;}
    void display()
    {
        stu_display();
        cou_display();
        cout<<"grade\n"<<grade<<endl;
    }
};
void main()
{
    sel_cou a(20150989,"张三","man",25,1234,"math",48,99);
    a.display();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值