面向对象的程序设计课程设计报告

  • 格式:doc
  • 大小:657.50 KB
  • 文档页数:45

面向对象的程序设计课程设计报告姓名:学号:班级:院系:日期:一.设计目的课程设计是课程教学中的一项重要内容,是完成教学计划达到教学目标的重要环节,是教学计划中综合性较强的实践教学环节,它对帮助学生全面牢固地掌握课堂教学内容、培养学生的实践和实际动手能力、提高学生全面素质具有很重要的意义。

本次课程设计通过设计和实现几个应用程序,达到以下目标:1.使学生能够比较熟练地掌握C++语言的基本语法规则;2.熟练掌握基本的面向对象的程序设计方法,如类的实现、创建类的实例—对象、实现类的继承等;3.掌握各种面向对象的程序设计技术,如继承和派生,虚继承、虚函数、抽象类、多态的实现、运算符重载、使用C++标准库等;4.掌握在VC++平台上进行程序设计和调试的方法。

二.内容及要求1.内容与功能要求题目一:通过组合和派生构成新的类本设计题目的任务是使用Point类产生Line类。

分别通过组合类及派生类两种方法实现,并要求分别给出使用类模板实现的程序。

本设计题的目的是使学生掌握在不同的实现方法中,如何设计相应的构造函数和拷贝构造函数,进一步理解程序调用它们及析构函数的执行顺序,掌握组合类和派生类。

另外本设计题目要让学生进一步掌握和理解类模板的技术及其实现方法。

题目二:人员信息管理系统设计题目:本设计题目的任务是设计一个人员信息管理系统,并使用虚函数实现多态性,完成显示不同人员信息的任务。

设计要求:设计employee类,用来存储雇员的编号,级别,月薪等信息,能够对其进行操作和显示。

以employee为基类派生出technician类,表示兼职技术人员,以及manager类,表示经理,以及salesman类,表示兼职推销员。

利用manage类和salesman类派生出salesmanager 类,表示销售经理。

注意使用虚基类。

题目三:学生考勤管理系统设计题目:在学生考勤管理系统中,考勤信息记录了学生的缺课情况,它包括:缺课日期、第几节课、课程名称、学生姓名、缺课类型(迟到、早退、请假及旷课)。

系统功能包括信息录入、修改、查询、统计等。

设计要求:录入学生的缺课记录;修改某个学生的缺课记录;查询某个学生的缺课情况;统计某段时间内,旷课学生姓名及旷课次数,按旷课次数由多到少排序;统计某段时间内,有学生旷课的课程及旷课人次,按旷课人次由多到少排序;2.软件开发环境Windows 7,Visual C++6.0第一题通过组合和派生构成新的类1.设计思路本题目的任务是使用Point类产生Line类。

分别通过组合类及派生类两种方法实现,并要求分别给出使用类模板实现的程序。

本题目的目的是使学生掌握在不同的实现方法中,如何设计相应的构造函数和拷贝构造函数,进一步理解程序调用它们及析构函数的执行顺序,掌握组合类和派生类。

另外本设计题目要让学生进一步掌握和理解类模板的技术及其实现方法。

2.程序详细代码及描述(1)//cpp1.h头文件#if !defined(CPP1_H)#define CPP1_H#include <iostream.h>#include <math.h>class Point{double X,Y;public:Point(double=0,double=0);Point(Point&);void Display(){cout<<"X="<<X<<",Y="<<Y<<endl;}double Distance(Point&);double Getx(){return X;}double Gety(){return Y;}~Point();};struct Cow{int Color;int Width;};class Line{Point a,b;Cow cw;public:Line(Point&,Point&,Cow&);double Display(Line&);Line(Line&);~Line();#endif(2)//cpp1.cpp文件#include "cpp1.h"Point::Point(double a,double b):X(a),Y(b){cout<<"调用Point的构造函数"<<endl;}Point::Point(Point&a){X=a.X;Y=a.Y;cout<<"调用Point复制函数"<<endl;}Point::~Point(){cout<<"Delete Point"<<X<<" "<<Y<<endl;}double Point::Distance(Point&a){return sqrt((X-a.X)*(X-a.X)+(Y-a.Y)*(Y-a.Y));}Line::Line(Point&a1,Point&a2,Cow&a3):a(a1),b(a2),cw(a3){cout<<"调用Line的构造函数"<<endl;}Line::Line(Line&s):a(s.a),b(s.b),cw(s.cw){cout<<"调用Line的复制构造函数"<<endl;}Line::~Line(){cout<<"Delete Line"<<endl;}double Line::Display(Line&a){a.a.Display();a.b.Display();cout<<"Color="<<a.cw.Color<<","<<"Width="<<a.cw.Width<<endl;double x=a.a.Getx()-a.b.Getx();double y=a.a.Gety()-a.b.Gety();return sqrt(x*x+y*y);}void main(){Point a;Point b(5.2,11.8),c(38.9,85.6)a=c;cout<<"两点之距为:"<<a.Distance(b)<<endl;Cow cw={3,5};Line s(a,b,cw);Line s1(s);cout<<s1.Display(s1)<<endl;}使用模板的方法(1)//cpp11.h文件#if !defined(cpp11_H)#define cpp11_H#include <iostream.h>#include <math.h>//声明Point类template <class T>class Point{T X,Y;public:Point(T=0,T=0);Point(Point&);void Display(){cout<<"X="<<X<<",Y="<<Y<<endl;}T Distance(Point&);~Point();T Getx(){return X;}T Gety(){return Y;}};//声明结构struct Cow{int Color;int Width;};//实现Point类template <class T>Point<T>::Point(T a,T b):X(a),Y(b){cout<<"调用Point的构造函数"<<endl;} template <class T>Point<T>::Point(Point&a){X=a.X;Y=a.Y;cout<<"调用Point的复制构造函数"<<endl;}template <class T>T Point<T>::Distance(Point&a){return sqrt((X-a.X)*(X-a.X)+(Y-a.Y)*(Y-a.Y));}template <class T>Point<T>::~Point(){cout<<"Delete Point"<<X<<" "<<Y<<endl;}//声明Line类template <class T>class LinePoint<T> a,b;Cow cw;public:Line(Point<T>&,Point<T>&,Cow&);Line(Line&);T Display(Line&);~Line();};//实现Line类template <class T>Line<T>::Line(Point<T>&a1,Point<T>&a2,Cow&a3):a(a1),b(a2),cw(a3) {cout<<"调用Line的构造函数"<<endl;}template <class T>Line<T>::Line(Line<T>&s):a(s.a),b(s.b),cw(s.cw){cout<<"调用Line的复制构造函数"<<endl;}template <class T>Line<T>::~Line(){cout<<"Delete Line"<<endl;}template <class T>T Line<T>::Display(Line&a){a.a.Display();a.b.Display();T x=a.a.Getx()-a.b.Getx();T y=a.a.Gety()-a.b.Gety();return sqrt(x*x+y*y);}#endif(2)//cpp11.cpp文件#include "cpp13.h"void main(){Point<double> a;Point<double> b(5.2,11.8),c(38.9,85.6)a=c;cout<<"两点之距为:"<<a.Distance(b)<<endl;Cow cw={3,5};Line<double>s(a,b,cw);Line<double> s1(s);cout<<s1.Display(s1)<<endl;使用继承的方法(1)cpp13.h文件#if !defined(PP13_H)#define CPP13_H#include <iostream.h>#include <math.h>//using namespace std;class Point{double X,Y;public:Point(double=0,double=0);Point(Point&);void Display(){cout<<"X="<<X<<",Y="<<Y<<endl;} double Distance(Point&);~Point(){cout<<"Delete Point"<<X<<" "<<Y<<endl;} double Getx(){return X;}double Gety(){return Y;}};struct Cow{int Color;int Width;};class Line:public Point{double X2,Y2;Cow cw;public:Line(double,double,double,double,Cow&);Line(Line&);double Display(Line&);~Line(){cout<<"Delete Line"<<endl;}};#endif(2)cpp13.cpp文件#include"cpp13.h"Point::Point(double a,double b):X(a),Y(b){cout<<"调用Point的构造函数"<<endl;}Point::Point(Point&a){X=a.X;Y=a.Y;cout<<"调用Point的复制构造函数"<<endl;} inline double Point::Distance(Point&a){return sqrt((X-a.X)*(X-a.X)+(Y-a.Y)*(Y-a.Y));Line::Line(double a1,double a2,double a3,double a4,Cow&c):Point(a1,a2),X2(a3), Y2(a4),cw(c){cout<<"调用Line的构造函数"<<endl;}Line::Line(Line&s):Point(s),X2(s.X2),Y2(s.Y2),cw(s.cw){cout<<"调用Line的复制构造函数"<<endl;}double Line::Display(Line&a){Point::Display();cout<<"X2="<<X2<<","<<"Y2="<<Y2<<endl;cout<<"Color="<<a.cw.Color<<","<<"Width="<<a.cw.Width<<endl;double x=X2-Getx();double y=Y2-Gety();return sqrt(x*x+y*y);}void main(){Point a;Point b(5.2,11.8),c(38.9,85.6);a=c;cout<<"两点之距为:"<<a.Distance(b)<<endl;Cow cw={3,5};Line s(5.2,11.8,38.9,85.6),cw);Line s1(s);cout<<s1.Display(s1)<<endl;cout<<"基类对象的属性"<<endl;a.Display();a=s;cout<<"派生类的对象赋给基类对象"<<endl;a.Display();cout<<"派生类的对象赋给基类的指针:"<<endl;Point &d=s1;d.Display();}使用模板继承的方法(1)cpp14.h文件#if !defined(CPP14_H)#define CPP14_H#include <iostream.h>#include <math.h>template <class T>class Point{T X,Y;public:Point(T=0,T=0);Point(Point&);void Display(){cout<<"X="<<X<<",Y="<<Y<<endl;}double Distance(Point&);T Getx(){return X;}T Gety(){return Y;}~Point(){cout<<"Delete Point"<<X<<" "<<Y<<endl;}};template <class T>Point<T>::Point(T a,T b):X(a),Y(b){cout<<"调用Point的构造函数"<<endl;}template <class T>Point<T>::Point(Point&a){X=a.X;Y=a.Y;cout<<"调用Point的复制构造函数"<<endl;}template <class T>double Point<T>::Distance(Point&a){return sqrt((X-a.X)*(X-a.X)+(Y-a.Y)*(Y-a.Y));}struct Cow{int Color;int Width;};template <class T>class Line:public Point<T>{T X2,Y2;Cow cw;public:Line(T,T,T,T,Cow&);Line(Line&);double Display(Line&);~Line(){cout<<"Delete Line"<<endl;}};template <class T>Line<T>::Line(T a1,T a2,T a3,T a4,Cow&c):Point<T>(a1,a2),X2(a2),Y2(a4),cw(c) {cout<<"调用Line的构造函数"<<endl;}template <class T>Line<T>::Line(Line&s):Point<T>(s),X2(s.X2),Y2(s.Y2),cw(s.cw) {cout<<"调用Line的复制构造函数"<<endl;}template <class T>double Line<T>::Display(Line&a){Point<T>::Display();cout<<"X2="<<X2<<","<<"Y2="<<Y2<<endl;cout<<"Color="<<a.cw.Color<<","<<"Width="<<a.cw.Width<<endl; double x=X2-Getx();double y=Y2-Gety();return sqrt(x*x+y*y);}#endif(2)cpp14.cpp文件#include "cpp14.h"void main(){Point<double>a;Point<double> b(7.8,9.8),c(34.5,67.8);a=c;cout<<"两点之距为:"<<a.Distance(b)<<endl;Cow cw={3,5};Line<double>s(7.8,9.8,34.5,67.8,cw);Line<double>s1(s);cout<<s1.Display(s1)<<endl;cout<<"基类的对象属性:"<<endl;a.Display();a=s;cout<<"派生类对象赋给基类对象:"<<endl;a.Display();cout<<"派生类指针赋给基类指针:"<<endl;Point<double>*p=&s1;p->Display();cout<<"基类对象引用派生类对象:"<<endl;Point<double> &d=s1;d.Display();}4.运行结果及分析(1)用包含的方法构成的新类(2)使用模板的方法构成的新类(3)通过继承的方法构成的新类(4)通过模板继承的方法构成的新类程序完成了实验要求。