c#实验四已完成.doc
实验课程名称: 面向对象程序设计 实验项目名称: 四:类的继承 【实验目的】 1.掌握继承的工作机制和意义。 2.掌握派生类的定义方法和实现。 3.掌握base关键字的使用。 4.编写体现类的继承性(成员变量,成员方法,成员变量隐藏)的程序。 5.掌握虚拟方法和重载方法的使用。 6.掌握抽象类和抽象方法的使用。 7. 理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使用虚函数的简单程序写出程序结果。【实验要求】1. 写出程序,并调试程序,要给出测试数据和实验结果。2. 整理上机步骤,总结经验和体会。3. 完成实验日志和上交程序。【实验内容】1.进一步理解继承的含义新类可从现有的类中产生,并保留现有类的成员变量和方法并可根据需要对它们加以修改。新类还可添加新的变量和方法。这种现象就称为类的继承。当建立一个新类时,不必写出全部成员变量和成员方法。只要简单地声明这个类是从一个已定义的类继承下来的,就可以引用被继承类的全部成员。被继承的类称为父类或超类(superclass),这个新类称为子类。2. 进一步理解继承的意义C# 提供了一个庞大的类库让开发人员继承和使用。设计这些类是出于公用的目的,因此,很少有某个类恰恰满足你的需要。你必须设计自己的能处理实际问题的类,如果你设计的这个类仅仅实现了继承,则和父类毫无两样。所以,通常要对子类进行扩展,即添加新的属性和方法。这使得子类要比父类大,但更具特殊性,代表着一组更具体的对象。继承的意义就在于此。3 理解类的多态性类的继承发生在多个类之间,而类的多态只发生在同一个类上。在一个类中,可以定义多个同名的方法,只要确定它们的参数个数和类型不同。这种现象称为类的多态。多态使程序简洁,为程序员带来很大便利。在OOP 中,当程序要实现多个相近的功能时,就给相应的方法起一个共同的名字,用不同的参数代表不同的功能。这样,在使用方法时不论传递什么参数,只要能被程序识别就可以得到确定的结果。类的多态性体现在方法的重载(overload)上,包括成员方法和构造方法的重载。4 构造方法的重载方法的重载是指对同名方法的不同使用方式。构造方法的名称和类同名,没有返回类型。尽管构造方法看起来和一般的成员方法没有差别,但它不是方法,也不是类的成员。因此,构造方法不能直接调用,只能由new 操作符调用。构造方法对于类是十分重要的,对象的初始化任务要靠构造方法来完成。重载构造方法的目的是提供多种初始化对象的能力,使程序员可以根据实际需要选用合适的构造方法来初始化对象。题目一:类的继承和基类构造方法的应用程序功能要求如下:编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。代码如下:person using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 继承 class Person /定义person类 string name; string number; public void Input() Console.Write("请输入姓名:"); name=Console.ReadLine(); Console.Write("请输入编号:"); number=Console.ReadLine(); public void PersonDisplay() Console.WriteLine(); Console.WriteLine("相关信息:"); Console.WriteLine("姓名:"+name); Console.WriteLine("编号:"+number); class Student : Person /Student类继承Person类 string studentClass; double score; public void StudentIput() Console.Write("请输入班级:"); studentClass = Console.ReadLine(); Console.Write("请输入成绩:"); score = Convert.ToDouble(Console.ReadLine(); public void StudentDisplay() Console.WriteLine("班级:" + studentClass); Console.WriteLine("成绩:" + score); Console.WriteLine("-"); class Teacher : Person /Teacher类继承Person类 string title; string department; public void TeacherIput() Console.Write("请输入职称:"); title = Console.ReadLine(); Console.Write("请输入部门:"); department = Console.ReadLine(); public void TeacherDisplay() Console.WriteLine("职称:" + title); Console.WriteLine("部门:" + department); Console.WriteLine("-"); class Program static void Main(string args) START:Console.Write("是否添加信息?请输入“Y/y”或“N/n”选择添加或退出:"); string choose = Console.ReadLine(); if (choose = "Y" | choose = "y") SELECT: Console.Write("输入“S/s”添加学生信息,输入“T/t”添加教师信息:"); string select = Console.ReadLine(); if (select = "S" | select = "s") /添加学生信息 Student stu = new Student(); stu.Input(); stu.StudentIput(); stu.PersonDisplay(); stu.StudentDisplay(); goto START; else if (select = "T" | select = "t") /添加教师信息 Teacher tea = new Teacher(); tea.Input(); tea.TeacherIput(); tea.PersonDisplay(); tea.TeacherDisplay(); goto START; else Console.WriteLine("请输入正确的选择!"); goto SELECT; else if (choose = "N" | choose = "n") goto END; else Console.WriteLine("请输入正确的选择!"); goto START; /重新执行主函数 END: Console.WriteLine("正退出程序。"); /退出 运行结果如下:题目二:类的继承和构造函数的灵活应用编写一个程序计算出球、圆柱和圆锥的表面积和体积。要求:定义一个基类圆,至少含有一个数据成员半径;定义基类的派生类球、圆柱、圆锥,都含有求体积函数,可以都在构造函数中实现,也可以将求体积和输出写在一个函数中,或者写在两个函数中,请比较使用。定义主函数,求球、圆柱、圆锥的和体积。代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 圆和圆的子类 class Circle /圆类 public double PI=3.1415; public double r,s; public Circle(double _r) /构造函数 r = _r; s = PI * r * r; class Ball:Circle /球类 double r; public Ball(double r) : base(r) /构造函数 this.r = r; Console.WriteLine("半径为0的球:",r); Console.WriteLine(" 面积:" + 4 * PI * r * r); Console.WriteLine(" 体积:" + 4.0 / 3 * PI * r * r); Console.WriteLine("-"); class Cylinder:Circle /圆柱类 double r,h; public Cylinder(double r, double h) : base(r) /构造函数 this.r = r; this.h = h; Console.WriteLine("半径为0,高为1的圆柱:", r, h); Console.WriteLine(" 表面积:" + (2 * s + 2 * PI * r * h); Console.WriteLine(" 体积:" + s * h); Console.WriteLine("-"); class Cone : Circle /圆锥类 double r, h; public Cone(double r, double h) : base(r) /构造函数 this.r = r; this.h = h; Console.WriteLine("半径为0,高为1的圆锥:", r, h); Console.WriteLine(" 表面积:" + (PI * r * r + PI * r * System.Math.Sqrt(r * r + h * h); Console.WriteLine(" 体积:" + (1 / 3.0 * PI * r * r * h); Console.WriteLine("-"); class Program static void Main(string args) Ball ball = new Ball(5); Cylinder cylinder = new Cylinder(5,4); Cone cone = new Cone(5, 4); Console.ReadLine(); 运行结果如下:题目三:多态程序练习:功能要求如下基类shape 类是一个表示形状的抽象类,area( )为求图形面积的函数。请从shape 类派生三角形类(triangle)、圆类(circles)、并给出具体的求面积函数,并在主函数中多态地实现调用。代码如下:#include<iostream>using namespace std;class Shapepublic:Shape()Shape()virtual float GetArea()return -1;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)Circle()float GetArea()return 3.14*itsRadius*itsRadius;private:float itsRadius;class Trianglepublic:Triangle(float loweredge,float height):itsLoweredge(loweredge),itsHeight(height);Triangle()virtual float GetArea()return itsLoweredge*itsHeight/2;virtual float GetLowerEdge()return itsLoweredge;virtual float GetHeight()return itsHeight;private:float itsLoweredge;float itsHeight;class Trapezoid/这个模仿上面public:Triangle(float upbottom,float lowerbottom,float height)/.Triangle();virtual float GetArea()return . /.virtual float GetUpBottom()return./.virtual float GetLowerBottom()return./.private:float itsUpBottom;float itsLowerBottom;int main()Shape *sp;sp=new Circle(5);cout<<"The area of the Circle is:"<<sp->GetArea()<<endl;delete sp;/根据需要添加return 0;【实验总结与分析】通过实验掌握了继承的工作机制和意义,派生类的定义方法和实现。