组合关系是包含关系的一种,包含关系是一种特殊的关联。表示类之间的关系是整体与部分的关系。判断两个类之间是否是组合关系要判断是否存在 has a / contains a 的关系。在面向对象程序设计中,可以对复杂对象进行分解、抽象,把一个复杂对象分解为简单对象的组合,由比较容易理解和实现的部件对象装配而成。在代码实现上,是以一个或多个某一类的对象作为另一个类的成员变量出现的方式进行实现的。类的成员变量可以是基本类型的变量,那么也可以是类的对象。组合关系的 UML 图例如下:
组合类即整体类或包含类,其构造函数的设计原则是:不仅要负责对本类中的基本类型成员数据赋初值,也要对对象成员初始化。当创建类的对象时,如果这个类具有内嵌对象成员,那么各个内嵌对象将首先被自动创建。在创建对象时既要对本类的基本类型数据成员进行初始化,又要对内嵌对象成员进行初始化。组合类构造函数定义的一般形式为:
类名::类名(对象成员所需的形参,本类成员形参) :内嵌对象 1(参数),内嵌对象 2(参数),…{ 本类初始化 }
在创建一个组合类的对象时,不仅它自身的构造函数的函数体将被执行,而且还将调用其内嵌对象的构造函数。这时构造函数调用的顺序如下:
(1)调用内嵌对象的构造函数,调用顺序按照内嵌对象在组合类的定义中出现的次序。
(2)执行本类构造函数的函数体。
合成复用原则,在软件复用时,要尽量先使用组合或聚合等关联关系来实现,其次才考虑使用继承关系来实现。合成复用原则是通过将已有的对象纳入新对象中,作为新对象的成员对象来实现的,新对象可以调用已有对象的功能,从而达到复用。
依赖关系类或对象之间的依赖描述了一个事物的变化可能会影响到使用它的另一个事物,反之不成立。两个类之间的语义连接关系:其中一个类是独立的,另一个类不是独立的,它依赖于独立的类,如果独立的类改变了,将影响依赖于它的类。依赖关系最为常见的一种形式就是:
A 类的某个方法中,使用了 B 类,那么就说 A 类依赖于 B 类,它们是依赖关系。
A 类的某个方法使用 B 类,可能是方法的参数是 B 类,也可能是在方法中获得了一个 B 类实例。
当要表明一个类使用另一个类作为它的成员函数参数时,就使用依赖关系。依赖关系代码实现的方式一般是一个类的成员函数中出现另一个类的对象指针来实现的。这里请复习一下参数传递方式,值传递与地址传递的区别,才能更好得理解依赖关系。依赖关系的 UML 图例如下:
图中的“类 A”是源,“类 B”是目标,表示“类 A”使用了“类 B”,或称“类 A”依赖“类 B”,即“类 A”的成员函数的参数出现了“类 B”的对象,对象指针或引用。可以用是否存在 use a 的关系来判断是否存在依赖关系
合理设计类与类的组合关系,即一个类要嵌套另一个类的对象作为成员变量,根据问题的需要可以是同一个类的多个对象(可以使用对象数组),也可以是不同类的多个不同对象。要求要对生成的对象的数据成员进行初始化、修改、获取、输出等基本操作,还应能完成其他一些合理的功能操作。让所设计的功能发挥作用。
题目描述对学生和图书进行抽象,抽象出应有的成员变量,和基本的初始化,修改等成员函数,学生可以发起借书的操作,这是一种依赖关系,在学生类中将设计相应的成员函数体现这一功能操作,合理即可,可以用输出借书是否成功的结果,有一定示意就可以。并生成对象完成借阅图书功能。
目的(1) 理解类之间的组合关系的逻辑解释,设计具有组合关系的两个类,包括类内部的成员变量,成员函数;
(2) 使用具有组合关系的类的对象,实现相应的功能;
(3) 理解类之间的依赖关系的逻辑解释,设计具有依赖关系的两个类,包括类内部的成员变量,成员函数;
(4) 使用具有依赖关系的类的对象,实现相应的功能。
#pragma once #include图书类 实现 CBOOK.cppusing namespace std; class CBOOK { private: string nBookname; string nPress;//出版社 string nIsbn;//图书Isbn号 string nPrice;//图书价格 string nPage;//图书页数 int njudge;//判断Ibsn码合法性 int temp;//求和后的值 public: void setBookInformation(string name, string press, string isbn, string price, string page); string ShowInformation(); int gettmp(); bool Judge(); };
#include "CBOOK.h" #include图书馆类 定义 CLibrary.h#include using namespace std; void CBOOK::setBookInformation(string name, string press, string isbn, string price, string page) { nBookname = name; nPress = press; nIsbn = isbn; nPrice = price; nPage = page; int ss[20] = { 0 }; int tmp = 0; for (int i = 0, j = 0; i < isbn.length(); i++) { if (isbn.substr(i, 1) >= "0" && isbn.substr(i, 1) <= "9") { ss[j] = atoi(isbn.substr(i, 1).c_str()); j++; } } for (int i = 0; i < 12; i++) { if (i % 2 == 0) { tmp += ss[i] * 1; } else { tmp += ss[i] * 3; } } temp = tmp; if (tmp % 10 == 0 && tmp % 10 == ss[12] || 10 - (tmp % 10) == ss[12]) njudge = 1; else { njudge = 0; } } string CBOOK::ShowInformation() { cout << "图书的名称是:" << nBookname << endl; cout << "出版社是:" << nPress << endl; cout << "图书的isbn码是:" << nIsbn << endl; cout << "图书价格是:" << nPrice << "元" << endl; cout << "图书有:" << nPage << "页" << endl; return string(); } int CBOOK::gettmp() { return temp; return 0; } bool CBOOK::Judge() { if (njudge == 1) { return true; } else { return false; } }
#pragma once #include图书馆类 实现 CLibrary.cpp#include "CStudent.h" #include "CBOOK.h" class CLibrary { private: string m_nLibraryName; int m_nBookAmount; CBOOK* m_nBook[255]; CStudent* m_nStu = new CStudent(); public: void SetLibraryName(string LibraryName); string GetLibraryName(); void SetAmount(int Amount); int GetNumber(); void SetBook(CBOOK* book,int i); CBOOK* GetBook(int i); void SetStudent(string Name, string Number,int i); CStudent* GetStudent(); };
#include "CLibrary.h" #include "CBOOK.h" #include "CStudent.h" #include学生类 定义 CStudent.hvoid CLibrary::SetLibraryName(string LibraryName) { this->m_nLibraryName = LibraryName; } string CLibrary::GetLibraryName() { return this->m_nLibraryName; return string(); } void CLibrary::SetAmount(int Amount) { this->m_nBookAmount = Amount; } int CLibrary::GetNumber() { return this->m_nBookAmount; return 0; } void CLibrary::SetBook(CBOOK* book,int i) { this->m_nBook[i] = book; } CBOOK* CLibrary::GetBook(int i) { return this->m_nBook[i]; return nullptr; } void CLibrary::SetStudent(string Name, string Number,int i) { this->m_nStu->SetStudentName(Name); this->m_nStu->SetStudentNumber(Number); this->m_nStu->SetBooki(i - 1); } CStudent* CLibrary::GetStudent() { return this->m_nStu; return nullptr; }
#pragma once #include学生类 实现 CStudent.husing namespace std; //#include "CBOOK.h" class CStudent { private: string m_nStudentName; string m_nStudentNumber; int m_nBooki; public: void SetStudentName(string Name); string GetStudentName(); void SetStudentNumber(string Number); string GetStudentNumber(); void SetBooki(int i); int GetBooki(); };
#include "CStudent.h" #include主函数 Library Management System.cppvoid CStudent::SetStudentName(string Name) { this->m_nStudentName = Name; } string CStudent::GetStudentName() { return this->m_nStudentName; return string(); } void CStudent::SetStudentNumber(string Number) { this->m_nStudentNumber = Number; } string CStudent::GetStudentNumber() { return this->m_nStudentNumber; return string(); } void CStudent::SetBooki(int i) { this->m_nBooki = i; } int CStudent::GetBooki() { return this->m_nBooki; return 0; }
// Library Management System.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//book.setBookInformation("C/C++程序设计教程——面向对象分册(第三版)", "中国工信出版集团,电子工业出版社", "978-7-121-33047-6", "47.00", "356");
#include
using namespace std;
#include "CLibrary.h"
#include "CBOOK.h"
#include "CStudent.h"
//设置图书信息
void SetInformation(CBOOK &book)
{
string Bookname;
string Press;//出版社
string Isbn;//图书Isbn号
string Price;//图书价格
string Page;//图书页数
cout << "请输入书名:"; cin >> Bookname;
cout << "请输入出版社:"; cin >> Press;
cout << "请输入Isbn号:"; cin >> Isbn;
cout << "请输入图书价格:"; cin >> Price;
cout << "请输入图书页数:"; cin >> Page;
book.setBookInformation(Bookname, Press, Isbn, Price, Page);
cout << endl;
}
//判断输入的书信息书否合法
CBOOK* JudgeIbsn(CBOOK& book) {
if (book.Judge()) {
return &book;
}
else
{
cout << "图书的Isbn码不合法!!请重新输入该书信息!!" << endl;
SetInformation(book);
JudgeIbsn(book);
}
}
//显示学生借书信息
void ShowBorrowInformation(CLibrary* Library) {
cout << "——————借书信息———————" << endl;
cout << "借书学生:" << Library->GetStudent()->GetStudentName() << endl;
cout << "学生学号:" << Library->GetStudent()->GetStudentNumber() << endl;
cout << "借的第:" << Library->GetStudent()->GetBooki()+1 << "本书" << endl;
Library->GetBook(Library->GetStudent()->GetBooki())->ShowInformation();
}
int main()
{
cout << "————————————————————图书管理及学生借书系统————————————————————" << endl;
cout << "请先依次设置图书馆的名字、其中书的数量、图书信息(包括:书名、出版社、Ibsn号、价格、页数)、" << endl << "学生姓名、学号、要借的第几本书" << endl;
string LibraryName;
int BookAmount;
string StudentName;
string StudentNumber;
int Booki;
CLibrary* Library = new CLibrary;
//设置图书馆信息
cout << "请输入图书馆名称:"; cin >> LibraryName;
Library->SetLibraryName(LibraryName);
cout << "请输入书本数量:"; cin >> BookAmount;
Library->SetAmount(BookAmount);
cout << endl;
//设置图书馆中图书信息
for (int i = 0; i < BookAmount; i++) {
CBOOK* book = new CBOOK();
cout << "请输入第" << i+1 << "本书信息:" << endl;
SetInformation(*book);
//JudgeIbsn(*book);
Library->SetBook(JudgeIbsn(*book), i);
}
//设置学生信息
cout << "请输入学生姓名:"; cin >> StudentName;
cout << "请输入学号:"; cin >> StudentNumber;
cout << "请输入要借的第几本书:"; cin >> Booki;
Library->SetStudent(StudentName, StudentNumber, Booki);
//Library->GetBook(Library->GetStudent()->GetBooki())->ShowInformation();
//显示学生的借书信息
cout << endl;
ShowBorrowInformation(Library);
system("pause");
return 0;
}
运行结果
说明
个人能力有限,仅供参考,共同学习!



