C++ 类继承&;模板类别C++;
我正在尝试编写一个程序,允许用户将物品添加到购物车中并将其删除。作业是使用讲师已经提供的Bag课程。ShoppingCart类将从Bag类派生。我正在努力继承和编译它 我对Bag.h文件(由教授提供)末尾的C++ 类继承&;模板类别C++;,c++,templates,inheritance,e-commerce,shopping-cart,C++,Templates,Inheritance,E Commerce,Shopping Cart,我正在尝试编写一个程序,允许用户将物品添加到购物车中并将其删除。作业是使用讲师已经提供的Bag课程。ShoppingCart类将从Bag类派生。我正在努力继承和编译它 我对Bag.h文件(由教授提供)末尾的#include“Bag.cpp”感到困惑。当我添加#include“ShoppingCart.cpp”等内容时,它会给我不同的错误。但在这种情况下,我得到以下错误。如果我添加这些包含,我会得到重新定义错误 我还对PuTTy上的编译过程应该包含哪些文件感到困惑 我知道这是一个很长的问题,但如果
#include“Bag.cpp”
感到困惑。当我添加#include“ShoppingCart.cpp”
等内容时,它会给我不同的错误。但在这种情况下,我得到以下错误。如果我添加这些包含,我会得到重新定义错误
我还对PuTTy上的编译过程应该包含哪些文件感到困惑
我知道这是一个很长的问题,但如果有人能给我一些答案,我会很高兴的。在main.cpp中,我没有尝试调用所有函数,基本上我还没有完成main。多谢各位
另外,任务要求我的文件作为头文件/实施文件分开
g++ -o main main.cpp Item.cpp
Undefined first referenced
symbol in file
_ZN12ShoppingCartI4ItemEC1Ev /var/tmp//cc52nA1n.o
_ZN12ShoppingCartI4ItemE3addES0_ /var/tmp//cc52nA1n.o
_Zeq4ItemS_ /var/tmp//cc52nA1n.o
_ZN12ShoppingCartI4ItemE13getTotalPriceEv /var/tmp//cc52nA1n.o
ld: fatal: symbol referencing errors. No output written to main
BagInterface.h
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE
#include <vector>
using namespace std;
template<class ItemType>
class BagInterface
{
public:
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
virtual int getCurrentSize() const = 0;
/** Sees whether this bag is empty.
@return True if the bag is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this bag.
@post If successful, newEntry is stored in the bag and
the count of items in the bag has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(const ItemType& newEntry) = 0;
/** Removes one occurrence of a given entry from this bag,
if possible.
@post If successful, anEntry has been removed from the bag
and the count of items in the bag has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(const ItemType& anEntry) = 0;
/** Removes all entries from this bag.
@post Bag contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Counts the number of times a given entry appears in bag.
@param anEntry The entry to be counted.
@return The number of times anEntry appears in the bag. */
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if bag contains anEntry, or false otherwise. */
virtual bool contains(const ItemType& anEntry) const = 0;
/** Empties and then fills a given vector with all entries that
are in this bag.
@return A vector containing all the entries in the bag. */
virtual vector<ItemType> toVector() const = 0;
}; // end BagInterface
#ifndef#u BAG#u接口
#定义\u包\u接口
#包括
使用名称空间std;
模板
类接口
{
公众:
/**获取此文件包中的当前条目数。
@返回包中当前条目的整数*/
虚拟int getCurrentSize()常量=0;
/**看看这个包是不是空的。
@如果行李是空的,则返回True,否则返回false*/
虚拟布尔isEmpty()常量=0;
/**将新条目添加到此包。
@post如果成功,则新条目将存储在袋子中,并且
袋子里的物品数量增加了1件。
@param newEntry要作为新条目添加的对象。
@如果添加成功,则返回True,否则返回false*/
虚拟bool add(const ItemType&newEntry)=0;
/**从该行李中删除给定条目的一次出现,
如果可能的话。
@post如果成功,则已从包中删除入口
袋子里的物品数量减少了1件。
@param a输入要删除的条目。
@如果删除成功,则返回True,否则返回false*/
虚拟bool remove(const ItemType&anEntry)=0;
/**删除此包中的所有条目。
@邮袋不包含任何物品,物品计数为0*/
虚空清除()=0;
/**统计给定条目在行李中出现的次数。
@参数输入要计数的条目。
@返回一个入口在行李中出现的次数*/
虚拟int getFrequencyOf(const ItemType&anEntry)const=0;
/**测试此包是否包含给定条目。
@param a输入要查找的条目。
@如果行李包含入口,则返回True,否则返回false*/
虚拟bool包含(const ItemType&anEntry)const=0;
/**清空给定向量,然后用所有
你在这个袋子里。
@返回包含包中所有条目的向量*/
虚拟向量toVector()常量=0;
}; // 终端接口
袋子
\ifndef\u包
#定义包
#包括“BagInterface.h”
模板
类包:公共包接口
{
私人:
静态常数int默认值\u袋大小=10;
ItemType items[默认的_袋子_大小];//袋子物品数组
int itemCount;//行李物品的当前计数
int maxItems;//行李的最大容量
//返回数组项中元素的索引
//包含给定的目标或-1(如果数组不包含)
//目标。
int getIndexOf(const ItemType&target)const;
公众:
袋子();
int getCurrentSize()常量;
bool isEmpty()常量;
bool add(const ItemType和newEntry);
bool-remove(const-ItemType和anEntry);
无效清除();
bool包含(const ItemType&anEntry)const;
int getFrequencyOf(const ItemType&anEntry)const;
向量toVector()常量;
}; // 尾袋
#包括“Bag.cpp”
#恩迪夫
Bag.cpp
#include "Bag.h"
#include <cstddef>
template<class ItemType>
Bag<ItemType>::Bag() : itemCount(0), maxItems(DEFAULT_BAG_SIZE)
{
} // end default constructor
template<class ItemType>
int Bag<ItemType>::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize
template<class ItemType>
bool Bag<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template<class ItemType>
bool Bag<ItemType>::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
template<class ItemType>
bool Bag<ItemType>::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if
return canRemoveItem;
} // end remove
template<class ItemType>
void Bag<ItemType>::clear()
{
itemCount = 0;
} // end clear
template<class ItemType>
int Bag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int searchIndex = 0;
while (searchIndex < itemCount)
{
if (items[searchIndex] == anEntry)
{
frequency++;
} // end if
searchIndex++;
} // end while
return frequency;
} // end getFrequencyOf
template<class ItemType>
bool Bag<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
/* ALTERNATE 1
template<class ItemType>
bool Bag<ItemType>::contains(const ItemType& anEntry) const
{
return getFrequencyOf(anEntry) > 0;
} // end contains
*/
/* ALTERNATE 2
template<class ItemType>
bool Bag<ItemType>::contains(const ItemType& anEntry) const
{
bool found = false;
for (int i = 0; !found && (i < itemCount); i++)
{
if (anEntry == items[i])
{
found = true;
} // end if
} // end for
return found;
} // end contains
*/
template<class ItemType>
vector<ItemType> Bag<ItemType>::toVector() const
{
vector<ItemType> bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private
template<class ItemType>
int Bag<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchIndex = 0;
// if the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount))
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while
return result;
} // end getIndexOf
#包括“Bag.h”
#包括
模板
Bag::Bag():itemCount(0),maxItems(默认袋大小)
{
}//结束默认构造函数
模板
int Bag::getCurrentSize()常量
{
返回项目计数;
}//结束getCurrentSize
模板
bool Bag::isEmpty()常量
{
返回itemCount==0;
}//结束是空的
模板
bool Bag::add(const ItemType和newEntry)
{
bool hasRoomToAdd=(itemCount-1);
如果(canRemoveItem)
{
项目计数--;
items[locatedIndex]=items[itemCount];
}//如果结束,则结束
返回canRemoveItem;
}//结束删除
模板
空包::清除()
{
itemCount=0;
}//结束清除
模板
int Bag::getFrequencyOf(const ItemType&anEntry)const
{
整数频率=0;
int searchIndex=0;
while(searchIndex-1;
}//结束包含
/*备选方案1
模板
bool Bag::包含(const ItemType和anEntry)const
{
返回getFrequencyOf(anEntry)>0;
}//结束包含
*/
/*备选方案2
模板
bool Bag::包含(const ItemType和anEntry)const
{
bool-found=false;
对于(int i=0;!found&&(i#include "Bag.h"
#include <cstddef>
template<class ItemType>
Bag<ItemType>::Bag() : itemCount(0), maxItems(DEFAULT_BAG_SIZE)
{
} // end default constructor
template<class ItemType>
int Bag<ItemType>::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize
template<class ItemType>
bool Bag<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template<class ItemType>
bool Bag<ItemType>::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
template<class ItemType>
bool Bag<ItemType>::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if
return canRemoveItem;
} // end remove
template<class ItemType>
void Bag<ItemType>::clear()
{
itemCount = 0;
} // end clear
template<class ItemType>
int Bag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int searchIndex = 0;
while (searchIndex < itemCount)
{
if (items[searchIndex] == anEntry)
{
frequency++;
} // end if
searchIndex++;
} // end while
return frequency;
} // end getFrequencyOf
template<class ItemType>
bool Bag<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
/* ALTERNATE 1
template<class ItemType>
bool Bag<ItemType>::contains(const ItemType& anEntry) const
{
return getFrequencyOf(anEntry) > 0;
} // end contains
*/
/* ALTERNATE 2
template<class ItemType>
bool Bag<ItemType>::contains(const ItemType& anEntry) const
{
bool found = false;
for (int i = 0; !found && (i < itemCount); i++)
{
if (anEntry == items[i])
{
found = true;
} // end if
} // end for
return found;
} // end contains
*/
template<class ItemType>
vector<ItemType> Bag<ItemType>::toVector() const
{
vector<ItemType> bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private
template<class ItemType>
int Bag<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchIndex = 0;
// if the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount))
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while
return result;
} // end getIndexOf
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
#include "Bag.h"
#include "Item.h"
#include <iostream>
#include <iomanip>
using namespace std;
template <class ItemType>
class ShoppingCart : public Bag<ItemType> {
private:
double totalPrice;
public:
ShoppingCart();
double getTotalPrice();
bool add(Item);
bool remove(Item);
};
#endif //SHOPPINGCART_H
#include "ShoppingCart.h"
using namespace std;
// Default Constructor
template <class ItemType>
ShoppingCart<ItemType>::ShoppingCart() {
totalPrice = 0;
}
template <class ItemType>
bool ShoppingCart<ItemType>::add(Item newItem) {
bool added = Bag<ItemType>::add(newItem);
totalPrice = totalPrice + (newItem.getQuantity() * newItem.getPrice());
return added;
}
template <class ItemType>
bool ShoppingCart<ItemType>::remove(Item anItem) {
bool removed = Bag<ItemType>::remove(anItem);
totalPrice = totalPrice - (anItem.getQuantity() * anItem.getPrice());
return removed;
}
template <class ItemType>
double ShoppingCart<ItemType>::getTotalPrice() {
return totalPrice;
}
#ifndef ITEM_H
#define ITEM_H
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Item {
private:
string name;
double price;
int quantity;
public:
Item();
Item(string n, double p, int q);
// Setters
void setName(string s);
void setPrice(double p);
void setQuantity(int q);
// Getters
string getName();
double getPrice();
int getQuantity();
friend istream& operator >>(istream&, Item&);
};
bool operator ==(Item i1, Item i2);
Item operator <<(ostream& os, Item& source);
#endif //ITEM_H
#include "Item.h"
#include <string>
using namespace std;
Item::Item() {
}
Item::Item(string n, double p, int q) {
name = n;
price = p;
quantity = q;
}
// Setters
void Item::setName(string n) {
name = n;
}
void Item::setPrice(double p) {
price = p;
}
void Item::setQuantity(int q) {
quantity = q;
}
// Getters
string Item::getName() {
return name;
}
double Item::getPrice() {
return price;
}
int Item::getQuantity() {
return quantity;
}
// Definition of the friend function
istream& operator >>(istream& ins, Item& target)
{
ins >> target.name >> target.price >> target.quantity;
return ins;
}
// Definition of non-member functions
// << & == operator overloading
bool operator ==(Item& i1, Item& i2) {
return (i1.getName()==i2.getName() && i1.getPrice()==i2.getPrice()
&& i1.getQuantity()==i2.getQuantity());
}
Item operator <<(ostream& os, Item& source) {
os << source.getName() << " " << source.getPrice() << " " <<source.getQuantity() << endl;
}
#include "ShoppingCart.h"
#include "Item.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
cout << "Welcome to XXX SHOPPING CENTER" << endl;
Item items[10];
ShoppingCart<Item> cart;
cout << "Enter the item you selected as the following order:\nname unitPrice quantity"
<< "\n(Name can not contain any space. Otherwise errors happen!)" << endl;
cin >> items[0];
cart.add(items[0]);
cout << "The shopping cart contains: " << endl;
cout << items[0];
cout << "The total price of the order is " << cart.getTotalPrice() << endl;
return 0;
}
bool operator ==(Item i1, Item i2);
bool operator ==(const Item& i1, const Item& i2);
// Definition of non-member functions
// << & == operator overloading
bool operator ==(Item& i1, Item& i2) {
return (i1.getName()==i2.getName() && i1.getPrice()==i2.getPrice()
&& i1.getQuantity()==i2.getQuantity());
}
// Definition of non-member functions
// << & == operator overloading
bool operator ==(const Item& i1, const Item& i2) {
return (i1.getName()==i2.getName() && i1.getPrice()==i2.getPrice()
&& i1.getQuantity()==i2.getQuantity());
}
// Getters
string getName();
double getPrice();
int getQuantity();
// Getters
const string& getName() const;
//string getName();
double getPrice() const;
int getQuantity() const;
// Getters
string Item::getName() {
return name;
}
double Item::getPrice() {
return price;
}
int Item::getQuantity() {
return quantity;
}
// Getters
const string& Item::getName() const {
return name;
}
double Item::getPrice() const {
return price;
}
int Item::getQuantity() const {
return quantity;
}
//Item operator <<(ostream& os, Item& source);
std::ostream &operator <<(ostream& os, Item& source);
//Item operator <<(ostream& os, Item& source) {
// os << source.getName() << " " << source.getPrice() << " " <<source.getQuantity() << endl;
//}
std::ostream &operator <<(ostream& os, Item& source) {
return os << source.getName() << " " << source.getPrice() << " " <<source.getQuantity() << endl;
}
gcc version 4.6.3
Welcome to XXX SHOPPING CENTER
Enter the item you selected as the following order:
name unitPrice quantity
(Name can not contain any space. Otherwise errors happen!)
ooo 2 3
The shopping cart contains:
ooo 2 3
The total price of the order is 6