实验4

实验任务1

问题1

描述1更准确,因为GradeCalc在语义上并不是一个vector数组,但它包含一个int型的vector数组,用来储存成绩数据。

问题2

1.局部变量grades用来储存输入的原始数据,grades_是复制产生副本数据,生命周期由GradeCalc对象决定。
2.因为grade_是类的私有成员,无法通过外部函数直接修改,只能使用接口set_grades()进行修改。

问题3

1.语法上是合法的,但成绩管理规则上并不合理。
2.不能。组合版本输入时需使用set_grades()函数输入,且在输入时会进行判断,若输入大于100会无法输入

问题4

调用者会获得绕过接口直接修改grades_的能力。
当前写法通过返回const变量,既保留了读的权限,使UI 可以打印成绩,又剥夺了写的权限,使外部不能随意修改成绩。

问题5

不合理,因为FileGradeCalc仅仅是为GradeCalc添加了一种新的输入方式,而不是其特化。
可以在不修改GradeCalc的情况下复用现有类。
因为代码仅改变了grades变量的输入,grades_的输入不会受影响,仅需设计console_ui::import_grades()便可以实现。
新增console_ui::import_grades()函数后,只有输入方式发生改变,成绩的来源对象未变,成绩的校验、保存和统计逻辑也并未改变。

实验任务2

问题1

1.分别执行的是Circle类,Triangle类,Triangle类声明。
不知道是什么图形地址。
2.输出均变为draw a shape...。
virtual决定了函数是静态绑定还是根据派生类动态绑定。

问题2

原方案要新增一个Ellipse的类,并在make_shape() 里加一个Ellipse的分支。
方案A要新增一个Ellipse的类,Canvas 的成员、add、paint、析构全都要动,添加有关Ellipse的信息。
方案B要新增枚举、add映射、paint的switch中的信息
原方案的paint()最稳定,新增图形时无需修改。方案AB在新增图形时均需修改paint()函数。

问题3

Shape.h中
enum class ShapeType {circle, triangle, rectangle};处添加ellipse
在此文件中添加Ellipse类的声明
Shape.cpp中
添加Ellipse类的实现
ShapeType str_to_ShapeType(const std::string& s) 中添加
if (t == "Ellipse")
return ShapeType::Ellipse;
Shape* make_shape(const std::string& type)中添加
case ShapeType::Ellipse: return new Ellipse;
无需改动Canvas.h 、Canvas.cpp 、demo2.cpp
因为这三个文件只依赖于shape的接口,并不依赖具体的图形形状

问题4

这是优点,体现了面向对象程序设计的面向接口编程原则
1.只需要知道公开接口和运用流程就行,不需要知道实现流程的具体细节和具体图形类
2.原本职责在工厂,修改后在应用层。
原设计应用层不依赖具体图形类,修改后对具体图形类强依赖

问题5

因为要调用不同类的析构函数
因为~Shape() 是虚函数,所以会根据shape的具体类型,调用对应的析构函数
去掉virtual后,删除数据时仅会调用~Shape(),导致派生类对象占用的资源无法释放
不安全,在每个析构函数中添加cout << "~Shape()\n"; 的类似代码,便可以发现析构函数是否正常调用。

实验任务3

场景:玩具工厂对于玩具的生产与简介。
抽象基类toy,它的派生类为doll,car,plane,light。
采用继承,因为toy和doll等派生类满足is-a关系(即doll是一种玩具)

toy.h

#pragma once
#include<string>
using namespace std;
enum class toytype{doll,car,plane,light
};
class toy{
public:
virtual void show()=0;
virtual void make()=0;
virtual ~toy()=default;
};
class doll:public toy{
public:
void show() override;
void make() override;
private:
string id="doll";
string ability="play";
};
class car:public toy{
public:
void show() override;
void make() override;
private:
string id="car";
string ability="drive";
};
class plane:public toy{
public:
void show() override;
void make() override;
private:
string id="plane";
string ability="fly";
};
class light:public toy{
public:
void show() override;
void make() override;
private:
string id="light";
string ability="shining";
};
toy* make_toy(const string& type);
toytype str_to_toytype(const string& s);

toy.cpp

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include "toy.h"
using namespace std;
void doll::show() {
cout<<"名称:"<<id<<endl;
cout<<"功能:"<<ability<<endl;
}
void doll::make(){
cout<<"doll has been made!\n";
}
void car::show() {
cout<<"名称:"<<id<<endl;
cout<<"功能:"<<ability<<endl;
}
void car::make(){
cout<<"car has been made!\n";
}
void plane::show() {
cout<<"名称:"<<id<<endl;
cout<<"功能:"<<ability<<endl;
}
void plane::make(){
cout<<"plane has been made!\n";
}
void light::show() {
cout<<"名称:"<<id<<endl;
cout<<"功能:"<<ability<<endl;
}
void light::make(){
cout<<"light has been made!\n";
}
toytype str_to_toytype(const string& s) {
string t = s;
transform(s.begin(), s.end(), t.begin(),
[](unsigned char c) {
return tolower(c);
});
if (t == "doll")
return toytype::doll;
if (t == "car")
return toytype::car;
if (t == "plane")
return toytype::plane;
if (t == "light")
return toytype::light;
return toytype::doll;
}
toy* make_toy(const string& type){
switch (str_to_toytype(type)){
case toytype::doll:return new doll;
case toytype:🚗return new car;
case toytype::plane:return new plane;
case toytype::light:return new light;
}
}

toyfactoty.h

#pragma once
#include <string>
#include <vector>
#include "toy.h"
using namespace std;
class toyfactory{
public:
void add(const std::string& type);
void allshow() const;
void makeall() const;
~toyfactory();
private:
std::vector<toy*> toys;
};

toyfactoty.cpp

#include "toyfactory.h"
#include
using namespace std;
void toyfactory::add(const string& type) {
toy* g = make_toy(type);
if (g)
toys.push_back(g);
}
void toyfactory::allshow()const{
for(toy* g:toys)
g->show();
}
void toyfactory::makeall() const{
cout<<"Start manufacturing...\n";
for(toy* g:toys)
g->make();
}
toyfactory::~toyfactory() {
for (toy* g : toys)
delete g;
}

test.cpp

#include
#include"toyfactory.h"
using namespace std;
void test() {
toyfactory toyfactorys;
toyfactorys.add("doll");
toyfactorys.add("car");
toyfactorys.add("plane");
toyfactorys.add("light");
toyfactorys.allshow();
toyfactorys.makeall();
}
int main() {
test();
}

运行结果

image

posted @ 2026-12-06 16:20  星祈sajou  阅读(0)  评论(0)    收藏  举报