我如何使函数在派生类中工作?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何使函数在派生类中工作?相关的知识,希望对你有一定的参考价值。
这是我的代码。它具有一个名为PC的抽象类和一个名为HomePC,GamingPC和WorkStationPC的3个派生类。它必须执行4件事:1)创建任何类型的新pc,2)升级已经存在的pc,3)删除已经存在的pc并4)打印特定类型的所有pc。我设法使“新”和“打印”功能起作用。我不知道如何使“升级”和“删除”功能起作用。我想它们会相似。如果有人对我该怎么做有想法,请告诉我。
谢谢!
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class PC{
protected:
string operatingSystem;
int ramSlots;
int pcieSlots;
int cpu;
int totalRamSlots;
int gbPerRam;
int ssd;
int cost;
public:
virtual void Print() = 0;
virtual void Upgrade(string opt) = 0;
PC(string OS, int moboRam, int moboPcie, int numOfCores, int ramSlots, int ramGB, int ssdGB, int pcCost){
operatingSystem = OS;
ramSlots = moboRam;
pcieSlots = moboPcie;
cpu = numOfCores;
totalRamSlots = ramSlots;
gbPerRam = ramGB;
ssd = ssdGB;
cost = pcCost;
}
};
class HomePC: public PC{
private:
string model;
public:
HomePC(string OS, int moboRam, int moboPcie, int numOfCores, int ramSlot, int ramGB, int ssdGB, int pcCost, string mdl):PC(OS, moboRam, moboPcie, numOfCores, ramSlot, ramGB, ssdGB, pcCost){
model = mdl;
}
void set_totalRamSlots(int ramSlots){totalRamSlots = ramSlots;}
void set_gbPerRam(int ramGB){gbPerRam = ramGB;}
void set_cpu(int numOfCores){cpu = numOfCores;}
//delete spaces
void Print(){ cout << "Μodel: " << model << endl << "Operating System: MacOS" <<endl << "Motherboard: 2 RAM slot(s), 0 GPU slot(s)" << endl << "CPU: " << cpu << endl << "RAM: " << totalRamSlots << " stick(s), " << gbPerRam << " GigaBytes" << endl << "SSD: 256" << endl << "Cost: 800EUR" << endl;}
void Upgrade(string opt){ cout << opt << endl;}
//delete spaces
string get_model() const {return model;}
};
class GamingPC: public PC{
private:
string model;
int gpu;
public:
GamingPC(string OS, int moboRam, int moboPcie, int numOfCores, int ramSlot, int ramGB, int ssdGB, int pcCost, int numOfGpus, string mdl):PC(OS, moboRam, moboPcie, numOfCores, ramSlot, ramGB, ssdGB, pcCost){
model = mdl;
gpu = numOfGpus;
}
//delete spaces
void set_totalRamSlots(int ramSlots){totalRamSlots = ramSlots;}
void set_gbPerRam(int ramGB){gbPerRam = ramGB;}
void set_cpu(int numOfCores){cpu = numOfCores;}
void set_gpu(int numOfGpus){gpu = numOfGpus;}
//delete spaces
void Print(){ cout << "Μodel: " << model << endl << "Operating System: MacOS" << endl << "Motherboard: 4 RAM slot(s), 2 GPU slot(s)" << endl << "CPU: " << cpu << endl << "RAM: "<< totalRamSlots << " stick(s), " << gbPerRam << " GigaBytes" << endl << "SSD: 256" << endl << "GPU: " << gpu << endl << "Cost: 1300EUR" << endl;}
void Upgrade(string opt){ cout << opt << endl;}
//delete spaces
string get_model() const {return model;}
};
class WorkStationPC: public PC{
private:
string model;
int gpu;
bool afterburner;
public:
WorkStationPC(string OS, int moboRam, int moboPcie, int numOfCores, int ramSlot, int ramGB, int ssdGB, int pcCost, int numOfGpus, bool aburner, string mdl):PC(OS, moboRam, moboPcie, numOfCores, ramSlot, ramGB, ssdGB, pcCost){
model = mdl;
gpu = numOfGpus;
afterburner = aburner;
}
//delete spaces
void set_totalRamSlots(int ramSlots){totalRamSlots = ramSlots;}
void set_gbPerRam(int ramGB){gbPerRam = ramGB;}
void set_cpu(int numOfCores){cpu = numOfCores;}
void set_gpu(int numOfGpus){gpu = numOfGpus;}
void set_afterburner(int aburner){afterburner = aburner;}
//delete spaces
void Print(){ cout << "Μodel: " << model << endl << "Operating System: MacOS" << endl << "Motherboard: 8 RAM slot(s), 1 GPU slot(s)" << endl << "CPU: " << cpu << endl << "RAM: " << totalRamSlots << " stick(s), " << gbPerRam << " GigaBytes" << endl << "SSD: 256" << endl << "GPU: " << gpu << endl << "Afterburner: " << afterburner << endl << "Cost: 1600EUR" << endl;}
void Upgrade(string opt){ cout << opt << endl;}
//delete spaces
string get_model() const {return model;}
};
int main(){
vector<int> count_homepc;
vector<int> count_gamingpc;
vector<int> count_workstationpc;
int i, k = 0;
string input, opCode, key=" ", pcType, pcModel, data[5];
size_t pos = 0;
vector<PC*> pcList;
do{
cout << "---Enter an operation:" << endl;
getline(cin,input);
pos=input.find(key);
opCode=input.substr(0,pos);
input.erase(0,pos + 1);
if(opCode=="new"){
pos=input.find(key);
pcType=input.substr(0,pos);
input.erase(0,pos + 1);
pcModel=input;
if(pcType=="homepc"){
HomePC* myPC = new HomePC("MacOS", 2, 0, 2, 1, 4, 256, 800, pcModel);
pcList.push_back(myPC);
count_homepc.push_back(k);
}else if(pcType=="gamingpc"){
GamingPC* myPC = new GamingPC("Windows", 4, 2, 6, 2, 8, 1024, 1300, 1, pcModel);
pcList.push_back(myPC);
count_gamingpc.push_back(k);
}else if(pcType=="workstationpc"){
WorkStationPC* myPC = new WorkStationPC("Linux", 8, 1, 6, 4, 16, 2048, 1600, 0, 0, pcModel);
pcList.push_back(myPC);
count_workstationpc.push_back(k);
}else cout << "Wrong PC type, try again." << endl;
}else if(opCode=="upgrade"){
pos=input.find(key);
pcModel=input.substr(0,pos);
input.erase(0,pos + 1);
cout << input << endl;
}else if(opCode=="delete"){
cout << "WIP" << endl;
}else if(opCode=="print"){
cout << endl;
int point;
if(input=="homepc"){
for(i=0;i<count_homepc.size();i++){
point = count_homepc[i];
pcList[point]->Print();
cout << endl;
}
}else if(input=="gamingpc"){
for(i=0;i<count_gamingpc.size();i++){
point = count_gamingpc[i];
pcList[point]->Print();
cout << endl;
}
}else if(input=="workstationpc"){
for(i=0;i<count_workstationpc.size();i++){
point = count_workstationpc[i];
pcList[point]->Print();
cout << endl;
}
}
}else if(opCode!="q") cout << "Wrong operation, try again." << endl;
k++;
}while(opCode!="q"); //when someone inputs the string "q" the loop ends
return 0;
}
删除
似乎您将PC的名称保留在名为model
的成员中。首先要做的是将其从所有3个派生类中删除,并将其放入基础PC
类中。这就是它所属的地方。每台电脑必须有一个名称。您有4件事要做:
- 读取
PC
名称 - 在向量中搜索
- 实际上删除
PC
- 从向量中擦除
PC
指针
// "name" is the name of target "PC"
for(auto i = 0; i < pcList.size(); i++)
if(pcList[i]->model == name) {
delete pcList[i];
pcList.erase(pcList.begin() + i);
break;
}
U可以设置一个auto found = false
并在找到PC
时将其设置为true,因此您知道是否实际上有一个具有该名称的PC
升级
这有点复杂,因为您必须传递多个内容。实现此目的的一种方法是通过设置循环一次参与其中。每当您进入循环中时,您都可以选择要升级或退出的部分。看起来像:
std::cin >> pc; // Pos in vector
while(true) {
// 1: upgrade cpu
// 2: upgrade ram
// ...
// 0: finish
int option;
std::cin >> option;
if(option == cpu) { // "cpu" should be a constant. In this case is integer "1"
int cpu; // The new cpu goes here
std::cin >> cpu;
pcList[pc]->cpu = cpu;
// pcList[pc]->upgradeCpu(cpu); // If u wanna do it OOP. Same as the line before
// I see u have "set_cpu()" and more fcts for that. Use them
}
// Repeat here for every upgradable part of a pc
else if(option == exit)
break;
}
您可以基于为每台PC赋予的唯一ID进行删除或升级。这样,您可以拥有多台具有相同型号和名称的PC。此ID可以是当推送到该pc时为该pc指定的pcList向量的索引。当用户输入操作“ new”和一台PC时,您可以输出pcList.size(),它将以ID的形式提供给新PC。并且当用户想要升级或删除时,他必须指定他想要修改的PC的ID]
以上是关于我如何使函数在派生类中工作?的主要内容,如果未能解决你的问题,请参考以下文章
当我从用户获取数据并将其保存到 SQLite 数据库中时,我应该怎么做才能使列表视图在片段中工作
shared_from_this()如何在派生类中工作,该派生类继承自从enabled_shared_from_this继承的基类