C++编程实例-继承(一)

实验11 继承(一)

【实验目的】

通过本实验,掌握类的public继承的概念。

【实验要求】

熟练掌握类的public继承方式。

【实验内容】

B 公有继承类 A

#include< c string>

#include<iostream>

using namespace std;

const int N=4;

class C{

private:

    char c[10];

    int t[N];

public:

    C(char *p){strcpy(c,p);}

    char *cf1(){ return c; }

    int *cf2(){ return t; }

    void cf3();

};

void C::cf3(){

    cout<<"course value: ";

    for(int i=0;i<N;i++)cin>>t[i];//input: 4 4 4 3

}

class A{

protected:

      void af3(){cout<<endl<<"Please input "; }

public:

    void af1(int *p,int n);

    float af2(int *p,int n,int *m);

};

void A::af1(int *p,int n){

    cout<<"student score: ";

    for(int i=0;i<n;i++)cin>>p[i];//input: 80 65 60 60

}

float A::af2(int *p,int n,int *m){

    for(int f=0,s=0,i=0;i<n;i++){

        s+=p[i]*m[i]; f+=m[i];}

    return (float)s/f;

}

class B: public A{

private:

    C k;

    int s[N];

    float st;

public:

    B():k("Zhao"),st(0){}

    void bf(){

        af3(); af1(s,N);

        af3(); k.cf3();

        st=af2(s,N,k.cf2());

        cout<<k.cf1()<<"'s="<<st<<endl;

      }

};

int main(){

    B g;

    g.bf();

    return 0;

}

转载于:https://my.oschina.net/u/141969/blog/609572

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值