作业12: 类的继承

作业12: 类的继承

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

2.使用虚函数编写程序求球体和圆柱体的体积及表面积。由于球体和圆柱体都可以看作由圆继承而来,所以可以定义圆类Circle作为基类。在Circle类中定义一个数据成员radius和两个虚函数area()和volume()。由Circle类派生Sphere类和Column类。在派生类中对虚函数area()和volume()重新定义,分别求球体和圆柱体的体积及表面积。

3.某学校对教师每月工资的计算规定如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元。副教授的固定工资为3000元,每个课时补贴30元。讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。

 

// H6T1.cpp -- 用 is-a 关系求长方形面积及长方体体积 
/*******************************  作业六第一题  ******************************
1.定义一个Rectangle类,它包含两个数据成员length和width;
以及包含用于求长方形面积的成员函数。再定义Rectangle的派生类Rectangular,
它包含一个新数据成员height和用来求长方体体积的成员函数。
在main函数中,使用两个类,求某个长方形的面积和某个长方体的体积。
******************************************************************************/
#include <iostream>
#include "Rectangular.h"

int main()
{
 	using namespace std;
 	Rectangle one;
 	Rectangular two;
	const char *menu = "1.Count the area of Retangle\n"
				 "2.Count the volume of Rectangular\n"
				 "3.Quit\n"
				 "Please choice:";
	char choice;
	int judge1;
	do			// circulate the menu - do
	{
	  judge1 = 0;
	  cout << menu;
	do          // circulate the choice - do
	{
    cin >> choice;
    switch(choice)
      {
      case '1' : // area
      	   	   	 {
		  		   one.enter1();
		  		   double a;
				   a = one.area();
				   one.display1(a);
				   break;
                 }
      case '2' : // volume
                 {
  		           two.enter1();
  		           two.enter2();
  		           double v;
				   v = two.volume();
			       two.display2(v);
			       break;
                 }
      case '3' : break;  // exit
      default :
	  		   {
			   	cout << "Enter error, please choice again: ";
				cin.clear();
				while(cin.get() != '\n')
				      continue;
				judge1 = 1;
				} 	  
	   } 
     }while(judge1);			// circulate the choice - while
     cout << endl << "Succeed!" << endl << endl;
   }while(choice != '3');     // circulate the menu - while
   return 0;
}

// Rectangle.h -- include two members and one function (长,宽,面积)

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

#include <iostream>

class Rectangle
{
	public:
        Rectangle (double l = 0, double w = 0):length(l),width(w){}
		double area();
		void enter1();
		void display1(double a);
	private:
		double length;
		double width;
};

// count the area
double Rectangle::area()
{
	return (length * width);
}	

// enter the length and width
void Rectangle::enter1()
{
	std::cout << "Please enter the length: ";
	std::cin >> length;
	std::cout << "Please enter the width: ";
	std::cin >> width;
}	

// display the area
void Rectangle::display1(double a)
{
	std::cout << "The area of this rectangle is: ";
	std::cout << a << std::endl;
}

// Rectangular.h -- add one member and a function 加一个高度和求体积

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

#include <iostream>
#include "Rectangle.h"

class Rectangular: public Rectangle			// 公有继承派生类Rectangular
{
	public:
        Rectangular(double h = 0, double l = 0, double w = 0)
			   	 :Rectangle(l, w), height(h){}
		double volume();
		void enter2();
		void display2(double v);
	private:
		double height;
}; 

// count the volume
double Rectangular::volume()
{
	return (area() * height);
}	

// enter the member of height
void Rectangular::enter2()
{
	std::cout << "Please enter the height: ";
	std::cin >> height;
}

// display the volume
void Rectangular::display2(double v)
{
	std::cout << "The Rectangular of this rectangle is: ";
	std::cout << v << std::endl;
}


// H6T2.cpp -- ABC 求圆柱体或球的体积及表面积 
/*******************************  作业六第二题  ******************************
2.使用虚函数编写程序求球体和圆柱体的体积及表面积。
由于球体和圆柱体都可以看作由圆继承而来,所以可以定义圆类Circle作为基类。
在Circle类中定义一个数据成员radius和两个虚函数area()和volume()。
由Circle类派生Sphere类和Column类。
在派生类中对虚函数area()和volume()重新定义,
分别求球体和圆柱体的体积及表面积。
******************************************************************************/

#include <iostream>
#include "Circle.h"
#include "Sphere.h"
#include "Column.h"

int main()
{
 	using namespace std;
 	
// define two class objects
 	Sphere qiu;
 	Column zhu;
 	Circle& q = qiu;
 	Circle& z = zhu;
 	double r;

// make a list
    const char* menu = "+-----------------------------+\n"
    	  			   "+ 1.求出球体的体积及表面积。  +\n"
					   "+ 2.求出圆柱体的体积及表面积。+\n"
					   "+ 3.退出系统。                +\n"
					   "+-----------------------------+\n"
					   "+-->>请选择:";
    int judge;		// to judge circule when cin error
    char choice;
	do				// to circular the menu -- do
    {
  	   judge = 0;
	   cout << menu;
	   do			// to circular cin -- do
	   {	
  			cin >> choice;
  			cout << endl;
  			switch (choice)
  			{
 	   		 	   case '1': // Sphere
				   			 {
				   			   r = q.enter();
				   			   q.area(r);
				   			   q.volume(r);
				   			   break;
				   			  }
                   case '2': // Column
                   			 {
				  		       r = z.enter();
				  		       zhu.enterh();
				  		       z.area(r);
				  		       z.volume(r);
				  		       break;
				  		      }
			       case '3': break;// exit
				   default : 
				   		   {
						   	 cout << "输入错误,请重新选择:";
							 cin.clear();
							 while(choice != '\n')
							       continue;
							 judge = 1;  
							 }
			 }
        }while(judge);   		// to circular cin -- while
        cout << endl << endl;
     }while(choice != '3');		// to circular the menu -- while
     
 return 0;
}
 	
// Circle.h  -- 基类 (圆) 
/*******************************  作业六第二题  ******************************
2.使用虚函数编写程序求球体和圆柱体的体积及表面积。
由于球体和圆柱体都可以看作由圆继承而来,所以可以定义圆类Circle作为基类。
在Circle类中定义一个数据成员radius和两个虚函数area()和volume()。
由Circle类派生Sphere类和Column类。
在派生类中对虚函数area()和volume()重新定义,
分别求球体和圆柱体的体积及表面积。
******************************************************************************/

#include <iostream>
using namespace std;

class Circle
{
 private:
 	    double radius;
 public:
 		double enter();
 		virtual void area(double r) = 0;
 		virtual void volume(double r) = 0;
 		virtual ~Circle(){}
}; 

double Circle::enter()
{
 	 cout << "请输入半径:";
	 cin >> radius;
	 return radius;
} 

// Column.h  -- 圆柱 
/*******************************  作业六第二题  ******************************
2.使用虚函数编写程序求球体和圆柱体的体积及表面积。
由于球体和圆柱体都可以看作由圆继承而来,所以可以定义圆类Circle作为基类。
在Circle类中定义一个数据成员radius和两个虚函数area()和volume()。
由Circle类派生Sphere类和Column类。
在派生类中对虚函数area()和volume()重新定义,
分别求球体和圆柱体的体积及表面积。
******************************************************************************/

#include <iostream>
using namespace std;

class Column: public Circle
{
 public:
 		virtual void area(double r);
 		virtual void volume(double r);
 		void enterh();
 private:
 		double height;
 		double ar;
 		double vo;
}; 

void Column::enterh()
{
 	 cout << "请输入高度:";
	 cin >> height;
} 

void Column::area(double r)
{
 	 ar = (r * 3.14 * r) + (3.14 * (2 * r) * height);
 	 cout << endl << "该圆柱的表面积是:" << ar << endl;
}

void Column::volume(double r)
{
 	 vo = r * 3.14 * r * height;
 	 cout << "该圆柱的体积是:" << vo << endl;
     cout << endl << "计算成功" << endl; 
}


// H6T3.cpp -- ABC 求教师工资 
/*******************************  作业六第三题  ******************************
3.某学校对教师每月工资的计算规定如下:固定工资+课时补贴。
教授的固定工资为5000元,每个课时补贴50元。
副教授的固定工资为3000元,每个课时补贴30元。
讲师的固定工资为2000元,每个课时补贴20元。
定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。
*******************************************************************************/

#include <iostream>
#include "teacher.h"
#include "professor.h"
#include "associate_professor.h"
#include "lecturer.h"


int main()
{
   using namespace std;
   char ifrestart;
   do							// restart the system 
   {
// specification
    const char * menu = "+--------  欢迎来到教师工资计算系统  --------+\n"
    	  	   	 	  	"|                                            |\n"
    	  	   	 	  	"| 教师职称:输入\"p\"代表职称\"教授\";           |\n"
    	  	   	 	  	"| 教师职称:输入\"a\"代表职称\"副教授\";         |\n"
    	  	   	 	  	"| 教师职称:输入\"l\"代表职称\"讲师\";           |\n"
    	  	   	 	  	"| 操作方法:先按提示输入教师的数目;         |\n"
    	  	   	 	  	"| 操作方法:然后输入教师的相关信息;          |\n"
    	  	   	 	  	"| 操作方法:之后输入教师需要补贴的课时。    |\n"
    	  	   	 	  	"| 系统计算:最后系统将列出这些教师的月工资。 |\n"
						"|____________________________________________|\n";
	cout << menu << endl;
	
 	
// define array 
    const int max = 999;
    teacher *(p[max]);
    
	int number;
    cout << "请输入教师的数目:";
	
	while(!(cin >> number))
	      {
		   		cin.clear();
		   		while(cin.get() != '\n');
                cout << "输入错误,请按提示重新正确输入:"; 
			}
			
	int i;
	char pn;
	for  (i = 0; i < number; ++i)
	{
	 	 cout << "请输入教师职称代码:";
		 cin >> pn; 
	 	 if(pn == 'p')
	 	  	   p[i] = new professor(0, 5000, 50, "None", 0);
	 	 else if (pn == 'a')
	       	   p[i] = new associate_professor(0, 3000, 30, "None", 0);
		 else if (pn == 'l')
		   	   p[i] = new lecturer(0, 2000, 20, "None", 0);
         else 
             {
         	  cout << "输入错误,请重新输入正确代码";
         	  cin.clear();
         	  while(cin.get() != '\n')
         	  		  continue;
  	  		  --i;
			  break;
			  }
	     if (pn == 'p' || pn == 'a' || pn == 'l')
		 	  p[i] -> count();
	 }
	 
	 cout << endl << "恭喜,输入完毕,以下是计算结果:";
	 cout << endl << "姓名" << setw(8) 
	 	  	 	  << "职称" << setw (8)  
	   			  << "课时" << setw(8) 
		 		  << "工资" << endl;
     for (i = 0; i < number; ++i)
     	 p[i] -> display();
     
     
	 cout << endl << "如果想重新使用系统,请按字母y,否则请按其它任意键退出:";
	 while(cin.get() != '\n'); 
	 cin.get(ifrestart);
	 cout << endl << endl;
	 }while(ifrestart == 'y');
	 
	 return 0;
 }

	
/***************** 知识补充 ************************************************
当子类protected继承父类之后,子类其实并不是父类的派生,
因此子类没法转换成父类,也就是无法自动up cast(向上转换),二者并不是is-a的关系。
你必须把继承改成public继承才可以真正实现类的派生。 
*****************************************************************************/

/****  用字符指针方法 *******
 char *name
 {
 int n1=strlen(n);
  name=new char[n1+1];
  strcpy(name,n);
  }
*****************************/

	      

// teacher.h -- 定义抽象基类 
/*******************************  作业六第三题  ******************************
3.某学校对教师每月工资的计算规定如下:固定工资+课时补贴。
教授的固定工资为5000元,每个课时补贴50元。
副教授的固定工资为3000元,每个课时补贴30元。
讲师的固定工资为2000元,每个课时补贴20元。
定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。
******************************************************************************/

#include <iostream>


class teacher
{
 	  public: 
	  		  teacher(int r, int c, char* n = "None"):
			  		regular_earnings(r),class_subsidies(c)
					  {
					  int n1 = strlen (n);
					  name = new char [n1+1];
					  strcpy(name, n);
					  }
  		      void entername();
	  		  virtual void count() = 0;
	  		  virtual void display() = 0;
	  		  virtual ~teacher(){}
	  public:	  		   
	  		 int regular_earnings;	// 固定工资 
	  		 int class_subsidies;   // 课时补贴
	  		 char* name;			// 名字 
};

void teacher::entername()
{
 	 std::cout << "请输入教师的名称:";
 	 std::cin >> name;
}

// associate_professor.h 
/*******************************  作业六第三题  ******************************
3.某学校对教师每月工资的计算规定如下:固定工资+课时补贴。
教授的固定工资为5000元,每个课时补贴50元。
副教授的固定工资为3000元,每个课时补贴30元。
讲师的固定工资为2000元,每个课时补贴20元。
定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。
******************************************************************************/

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

class associate_professor:public teacher
{
 public:
  associate_professor(int ch = 0, int r = 3000, int c = 30, char* n = "None", int s = 0):
	 			   teacher(r, c, n),class_hour(ch),sum(s){}
    virtual void count();
    virtual void display();
 private:
	int class_hour;
	int sum;
};

void associate_professor::count()
{
 	 teacher::entername();
 	 cout << "请输入教师需要补贴的课时:";
 	 cin >> class_hour;
 	 sum =  regular_earnings + class_hour * class_subsidies;	 
}

void associate_professor::display()
{
 	 cout << name << setw(10)
	      << "  副教授" << setw(10) 
	      << class_hour << setw(10) 
		  << sum << endl;
}

// lecturer.h 
/*******************************  作业六第三题  ******************************
3.某学校对教师每月工资的计算规定如下:固定工资+课时补贴。
教授的固定工资为5000元,每个课时补贴50元。
副教授的固定工资为3000元,每个课时补贴30元。
讲师的固定工资为2000元,每个课时补贴20元。
定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。
******************************************************************************/

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

class lecturer:public teacher
{
 public:
  lecturer(int ch = 0, int r = 2000, int c = 20, char* n = "None", int s = 0):
	 			   teacher(r, c, n),class_hour(ch),sum(s){}
    virtual void count();
    virtual void display();
 private:
	int class_hour;
	int sum;
};

void lecturer::count()
{
 	 teacher::entername();
 	 cout << "请输入教师需要补贴的课时:";
 	 cin >> class_hour;
 	 sum =  regular_earnings + class_hour * class_subsidies;	 
}

void lecturer::display()
{
 	 cout << name << setw(10)
	      << "  讲师" << setw(10) 
	      << class_hour << setw(10) 
		  << sum << endl;
}

// professor.h 
/*******************************  作业六第三题  ******************************
3.某学校对教师每月工资的计算规定如下:固定工资+课时补贴。
教授的固定工资为5000元,每个课时补贴50元。
副教授的固定工资为3000元,每个课时补贴30元。
讲师的固定工资为2000元,每个课时补贴20元。
定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。
******************************************************************************/

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

class professor:public teacher
{
 public:
  professor(int ch = 0, int r = 5000, int c = 50, char* n = "None", int s = 0):
	 			   teacher(r, c, n),class_hour(ch),sum(s){}
    virtual void count();
    virtual void display();
 private:
	int class_hour;
	int sum;
};

void professor::count()
{
 	 teacher::entername();
 	 cout << "请输入教师需要补贴的课时:";
 	 cin >> class_hour;
 	 sum =  regular_earnings + class_hour * class_subsidies;	 
}

void professor::display()
{
 	 cout << name << setw(10)
	      << "  教授" << setw(10) 
	      << class_hour << setw(10) 
		  << sum << endl;
}



  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值