根据uml类图写c++类定义 【精编】_第1页
根据uml类图写c++类定义 【精编】_第2页
根据uml类图写c++类定义 【精编】_第3页
根据uml类图写c++类定义 【精编】_第4页
根据uml类图写c++类定义 【精编】_第5页
已阅读5页,还剩63页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Review(复习),Contents,1 概念2 简答3 选择 填空 改错4 根据UML类图写C类定义 根据C代码画UML类图5 编程,1 概念,对象,消息,方法,类,继承,封装,聚集,多态性,抽象类友元函数,构造函数,析构函数,函数名重载,虚函数,纯虚函数,异常,2 简答,简述C程序中类的一般结构。在C程序中,类成员的访问权符有哪几种?公有派生和私有派生有什么区别?用成员函数和友元函数重载运算符有什么区别? 当进行多重继承时,如果在派生类中使用不同父类的同名成员,如何避免二义性?C主要使用哪两个流类进行预定义类型的输入和输出,重载了哪两个运算符?UML类图中,如何表示聚集和泛化关系?请列举出5种UML模型图。,3 根据UML类图写C类定义,例:UML类图,类定义,#include class Figure protected: float x,y; public: void Set(); virtual void ShowArea();class Triangle:public Figure public: void ShowArea();,class Square: public Figure public: void ShowArea();class Circle: public Figure public: void ShowArea(); ;,4 根据C代码画UML类图,例:源程序#include class X protected:int a; public:void make(int i)a = i; ;class Y protected:int a; public:void make(int i)a = i; ; class Z: public X, public Y public:int make()return X:a*Y:a; ;,UML类图,1. 定义一个Shape基类,在此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。,#include 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 Rectangle : public Shapepublic: Rectangle(float len, float width): itsLength(len), itsWidth(width) ; Rectangle () ); float GetArea() return itsLength * itsWidth; float GetLength() return itsLength; ) float GetWidth() return itsWidth; private: float itsWidth; float itsLength;,class Square : public Rectangle public . Square (float len); Square () ;Square: Square (float len): Rectangle (len,len) ,void main() Shape * sp; sp=new Circle (5); cout GetArea () GetArea() GetArea () endl; delete sp;,输出,The area of the Circle is 78. 5The area of the Rectangle is 24The area of the Square is 25,2.定义一个Document类,包含成员变量name。从Document派生出Book类,增加PageCount变量。,#include #include class Documentpublic: Document ( ) ; Document(char * name); char * Name; / Document name. void PrintNameOf(); /Print name.;Document: Document (char * name) Name =new charstrlen(name) + 1; strcpy(Name, name);,void Document : PrintNameOf() cout Name endl;class Book : public Documentpublic: Book(char * name, long pagecount); void PrintNameOf(); private: long PageCount; Book:Book(char *name,long pagecount):Document(name) PageCountpagecount; ,void Book:PrintNameOf() coutName Of book:; Document:PrintNameOf(); void main() Document a(Document1); Book b(Bookl,100); b.PrintNameOf(); 输出:Name of book: Book1,3. 定义计数器Counter类,对其重载运算符“+”。,typedef unsigned short USHORT; #include Class Counter public: Counter(); Counter(USHORT initialValue); Counter() USHORT GetItsVal() const return itsVal; Void SetItsVal(USHORT x)itsValx; Counter operator+(const Counter&); private: USHORT itsVal; ;,Counter:Counter(USHORT initialValue):itsVal(initialValue) Counter:Counter():itsVal(0) Counter Counter:operator+(coust Counter return 0; ,4.定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义speak()成员函数,基类中定义为虚函数。定义一个Dog类的对象,调用Speak函数,观察运行结果。,#include class Mammal public: Mammal():itsAge(1)coutMammal constructor.n;) Mammal()cout“Mammal destructor.n; virtual void Speak()const coutMammal speak! n; protected: itsAge; ; class Dog:public Mammal public: Dog()coutDog Constructor.n; Dog()cout“Dog destructor.n; voidSpeak()const coutSpeak(); return 0; 输出:Mammal constructor.Dog constructor.Woof !Dog destructor.Mammal destructor.,5.定义一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者都有 GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。,#include class Shape public: Shape() Shape() virtual float GetArea()0; virtual float GetPerim()0; ;,class Circle:public Shape public: Circle(f1oat radius):itsRadius(radius) CIrcle() float GetArea()return 314 *itsRadius *itsRadius; float GetPerim()return 6.28 *itsRadius; private: float itsRadius; ;,class Rectangle:public Shape public: Rectangle(float len,float width):itsLength(len),itsWidth(Width); Rectangle(); virtualfloat GetArea()return itsLength *itsWidth; float GetPerim()return 2 *itsLength+2 *itsWidth; ) virtual float GetLength()return itsLength; virtual float GetWidth()return itWidth; private: float itsWidth; float itsLength; ;,void main() Shape *sp; spnew Circle(5); coutGetArea()GetPerim()GetArea()GerPerim()endl; delere sp; 输出:The area of the Circle is 78.5The perimeter of the Circle is 31.4The area of the Rectangle is 24The perimeter of the Rectangle is 20,6.编写程序:定义类Person、Student和Teacher,分析3个类之间的关系。编写main()函数测试类定义。,UML类图:,源程序,/ People.h: interface for the People class.enum Sexesmale,female;class People public:virtual void ShowInfo();void Set(char * aName, Sexes aSex, int anAge);People(char *aName, Sexes aSex, int anAge);People();virtual People();protected:char Name20;Sexes Sex;int Age;,/ People.cpp: implementation of the People class.#include People.h#include #include People:People()People:People()People:People(char *aName, Sexes aSex, int anAge)strcpy(Name,aName);Sex=aSex;Age=anAge;,void People:Set(char *aName, Sexes aSex, int anAge)strcpy(Name,aName);Sex=aSex;Age=anAge;void People:ShowInfo()coutName: Name Sex: ;if(Sex) coutfemale;else coutmale;cout Age: Age;,/ Student.h: interface for the Student class.#include People.hclass Student : public People public:virtual void ShowInfo();Student(char *aName, Sexes aSex, int anAge, char *aNum, char *aCls);Student();virtual Student();private:char Num7;char ClassNo20;,/ Student.cpp: implementation of the Student class.#include Student.h#include #include Student:Student()Student:Student()Student:Student(char *aName, Sexes aSex, int anAge, char *aNum, char *aCls):People(aName,aSex,anAge)strcpy(Num,aNum);strcpy(ClassNo,aCls);void Student:ShowInfo()People:ShowInfo();cout Class: ClassNo Number: Num ;,/ Teacher.h: interface for the Teacher class.#include People.hclass Teacher : public People public:virtual void ShowInfo();Teacher(char *aName, Sexes aSex, int anAge, char *aPrin, char *aDept);Teacher();virtual Teacher();private:char Department40;char Principalship30;,/ Teacher.cpp: implementation of the Teacher class.#include Teacher.h#include #include Teacher:Teacher()Teacher:Teacher()Teacher:Teacher(char *aName, Sexes aSex, int anAge, char *aPrin, char *aDept):People(aName,aSex,anAge)strcpy(Principalship,aPrin);strcpy(Department,aDept); void Teacher:ShowInfo()People:ShowInfo();cout Department: Department Principalship: Principalship ;,#include Student.h#include Teacher.h#include int main()People p1(Zhang Li,female,20);Student s1(Wang Yong,male,21,987654,Computer9820);Teacher t1(Liu Hong,female,50,Master,Computer);p1.ShowInfo();coutendl;s1.ShowInfo();coutendl;t1.ShowInfo();coutendl;return 0;,7. 工资管理程序设计,某公司财务部需要开发一个计算雇员工资的程序。该公司有3类雇员,工人的工资为每小时工资额乘当月工作时数再加上工龄工资;销售员工资为每小时工资额乘当月工作时数加上销售额提成再加上工龄工资,其中销售额提成等于该销售员当月售出商品金额的1;管理人员工资为基本工资再加上工龄工资。工龄工资就是雇员在该公司工作的工龄每增加1年,月工资就增加35元。请用面向对象方法分析、设计这个程序,并用C语言写出完整的程序。,参考答案,UML类图:,源程序,/ Employee.h: interface for the Employee class.class Employee public:Employee();Employee(int anAge);virtual Employee();virtual float salary()=0;protected:int work_age;,/ Employee.cpp: implementation of the Employee class.#include Employee.hEmployee:Employee() work_age=0;Employee:Employee(int anAge)work_age=anAge;Employee:Employee(),/ Worker.h: interface for the Worker class./#include Employee.hclass Worker : public Employee public:Worker();Worker(float,int,int);virtual Worker();virtual float salary();private:float hour_salary;int work_hour;,/ Worker.cpp: implementation of the Worker class.#include Worker.hWorker:Worker() hour_salary=0;work_hour=0;Worker:Worker(float hr_sa, int wk_hr,int age):Employee(age)hour_salary=hr_sa;work_hour=wk_hr;,Worker:Worker()float Worker:salary()return hour_salary*work_hour+work_age*35;,/ SalesMan.h: interface for the SalesMan class.#include Employee.hclass SalesMan : public Employee public:SalesMan();SalesMan(float, int, float,int);virtual SalesMan();virtual float salary();private:float hour_salary;int work_hour;float sale_amount;,/ SalesMan.cpp: implementation of the SalesMan class.#include SalesMan.hSalesMan:SalesMan() hour_salary=0;work_hour=0;sale_amount=0;SalesMan:SalesMan(float hr_sa, int wk_hr, float s_amount,int age):Employee(age)hour_salary=hr_sa;work_hour=wk_hr;sale_amount=s_amount;a,SalesMan:SalesMan()float SalesMan:salary()return hour_salary*work_hour+sale_amount*0.01+work_age*35;,/ Manager.h: interface for the Manager class.#include Employee.hclass Manager : public Employee public:Manager();Manager(float,int);virtual Manager();virtual float salary();private:float base_salary;,/ Manager.cpp: implementation of the Manager class.#include Manager.hManager:Manager() base_salary=0;Manager:Manager(float amount, int age):Employee(age)base_salary=amount;Manager:Manager()float Manager:salary()return base_salary+work_age*35;,控制台程序,#include #include Employee.h#include Manager.h#include SalesMan.h#include Worker.hint main()SalesMan s1(15, 100, 25000,10);Worker w1(10, 150, 20);Manager m1(1000, 5);coutSales man s1s salary is: s1.salary()endl;coutWorker w1s salary is: w1.salary()endl;coutManager m1s salary is: m1.salary()endl;return 0;,Windows 程序,Salary.exe,8. 继承派生类,已知类定义:如上已定义了2个类fruit和tree,请派生出一些既是fruit又是tree的类,如apple;并从这些类再派生出一些类,如apple-pear(苹果梨)。如:class apple: public fruit, public tree;每个派生类至少有一个成员函数显示自己的类名,以及自己是从哪些基类派生出来的。如:apple类应显示:(apple: fruit, tree) apple-pear类应显示:(apple-pear: (apple: fruit, tree), (pear: fruit, tree),class fruit public: virtual char *identify()return “fruit”; ; class tree public: virtual char *identify()return “tree”; ;,参考答案,/ fruit.h: interface for the fruit class.class fruit public: virtual char *identify()return fruit; ;class tree public: virtual char *identify()return tree; ;class apple:public fruit, public treeprivate:char ident30;public:virtual char *identify();,class pear:public fruit, public treeprivate:char ident30;public:virtual char *identify();class apple_pear:public apple, public pearprivate:char ident80;public:virtual char *identify();,/ fruit.cpp: implementation of the fruit class.#include fruit.h#include #include char *apple:identify()sprintf(ident,(apple:%s,%s),fruit:identify(),tree:identify();return ident;char *pear:identify()sprintf(ident,(pear:%s,%s),fruit:identify(),tree:identify();return ident;,char *apple_pear:identify()sprintf(ident,(apple_pear:%s,%s),apple:identify(),pear:identify();return ident;int main()apple apple1;pear pear1;fruit fruit1;tree tree1;apple_pear ap1;coutfruit1s identification is: fruit1.identify()endl; couttree1s identification is: tree1.identify()endl; coutapple1s identification is: apple1.identify()endl; coutpear1s identification is: pear1.identify()endl;coutap1s identification is: ap1.identify()endl;return 0;,9. 抽象类,要求计算正方体、球和圆柱 3个几何体的表面积和体积。可以抽象出一个公共的基类Base,把它作为抽象类,在该类内定义求表面积和体积的纯虚函数(抽象类本身是没有表面积和体积可言的)。抽象类中可以定义一个数据成员length,它可作为球的半径、正方体的边长或圆柱体底面圆的半径。由这个抽象类派生出描述球、正方体和圆柱的3个具体类,在这 3个类中都有计算表面积和体积的函数的自己版本。用C语言定义上述类等级。,参考答案,UML类图:,源程序,/ Base.h: interface for the Base class.class Base public:Base(double aDouble);Base();virtual Base();virtual double Square()=0;virtual double Cubage()=0;protected:double length;,/ Base.cpp: implementation of the Base class.#include Base.hBase:Base() length=0;Base:Base()Base:Base(double aDouble) length=aDouble;,/ ball.h: interface for the ball class.#include Base.hclass ball : public Base public:virtual double Cubage();virtual double Square();ball(double len);ball();virtual ball();,/ ball.cpp: implementation of the ball class.#include ball.hball:ball()ball:ball()ball:ball(double len):Base(len)double ball:Square() return 4*3.14159*length*length;double

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论