实验六 类的继承

一、实验目的:

了解继承的作用,掌握三种不同的继承方法,掌握派生类的设计方法。

二、实验内容:

1.定义一个Rectangle类,它包含两个数据成员length和width,以及用于求长方形面积的成员函数。再定义Rectangle的派生类Rectangular,它包含一个新数据成员height和用来求长方体体积的成员函数。在主函数中,使用两个类,求某个长方形的面积和某个长方体的体积。

#include<iostream>

using namespace std;

 

class Rectangle

{

public :

       voidarea();

       voidsetareadata(double x,double y);

protected:

       doublelength;

       doublewidth;

 

 

};

 

void Rectangle::setareadata( double x,double y)

{

       length=x;

       width=y;

}

 

void Rectangle::area()

{

       doubles=length*width;

       cout<<"长方形的面积:"<<s<<endl;

 

}

 

 

 

class Rectangular:public Rectangle

{

public:

       voidvolume();

       voidsetvolumedata(double z);

protected :

       doubleheight;

 

};

 

void Rectangular ::setvolumedata(double z)

{

       height=z;

 

}

 

void Rectangular :: volume()

{

       doublev=length*width*height;

       cout<<"长方形的体积:"<<v<<endl;

}

 

void main()

{

       RectangleA;

       A.setareadata(1.1,1.1);

       A.area();

       RectangularB;

       B.setareadata(1.1,1.1);

       B.setvolumedata(2.0);

       B.volume();

 

}



 

2. 教材312页程序设计题第1题。

#include<iostream>

#include<cstring>

#include<string>

using namespace std;

class Employee

{

public :

   Employee(charSnumber[]="\0",char Sname[]="\0",double bSalary=2000)

   {

      strcpy(number,Snumber);

      strcpy(name,Sname);

      basicSalary=bSalary;

   }

   voidinput()

   {

      cout<<"编号:";

      cin>>number;

      cout<<"姓名:";

      cin>>name;

   }

   voidprint()

   {

      cout<<"员工:"<<name<<"\t\t编号:"<<number<<"\t\t本月工资:"<<basicSalary<<endl;

   }

protected:

   charnumber[5];

   charname[10];

   doublebasicSalary;

 

 

};

 

class Salesman : public Employee

{

public:

   Salesman(intsal=0)

   {

      sales=sal;

  

   }

   voidinput()

   {

      Employee::input();

      cout<<"本月个人销售额:";

      cin>>sales;

   }

   voidpay()

   {

      salary=basicSalary+sales*commrate;

   }

   voidprint()

   {

      pay();

      cout<<"销售员:"<<name<<"\t\t编号:"<<number<<"本月工资:"<<salary<<endl;

   }

protected:

   staticdouble commrate;

   intsales;

   doublesalary;

};

double Salesman::commrate=0.005;

 

class Salesmanager:public Salesman

{

public:

   Salesmanager(doublejSalary=3000)

   {

      jobSalary=jSalary;

   }

   voidinput()

   {

      Employee::input();

      cout<<"本月销售额:";

      cin>>sales;

   }

   voidpay()

   {

      pay();

      cout<<"销售经理:"<<name<<"\t\t编号:"<<number<<"\t\t本月工资:"<<salary<<endl;

   }

private:

   doublejobSalary;

};

 

int main()

{

   cout<<"基本员工\n";

   Employeep1;

   p1.input();

   p1.print();

   cout<<"销售员\n";

   Salesmanp2;

   p2.input();

   p2.print();

   cout<<"销售经理\n";

   Salesmanagerp3;

   p3.input();

   p3.print();

 

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值