如何创建派生指针类变量
how to create a derived pointer class variable?
目前正在做家庭作业,我应该创建一个容纳不同类型卡片的向量。所以我创建了一个卡类和 2 个派生类(ID 卡、银行卡)。每个类都有其独特的信息(名称等)。我不知道如何在矢量中创建ID和银行卡。此外,有人可以解释多态性以及如何在此代码中使用吗?我尝试使用&,但我不确定该符号的确切用途是什么。提前感谢任何帮助。
std::cout << "Card Type: " ;
std::cin >> card_type;
while (card_type)
{
if (card_type == 1)
{
std::cout << "Institution name: ";
getline (std::cin, institute_name);
std::cin.ignore();
std::cout << "Cardholder name: ";
getline (std::cin, card_name);
std::cin.ignore();
std::cout << "Expiration date mmddyyyy (0 if none): ";
std::cin >> expire_date;
std::cout << std::endl <<std::endl;
Card* regular = new Card(institute_name, card_name, expire_date);
cardbook.push_back (regular);
}
else if (card_type == 2)
{
std::cout << "Institution name: ";
getline (std::cin, institute_name);
std::cin.ignore();
std::cout << "Cardholder name: ";
getline (std::cin, card_name);
std::cin.ignore();
std::cout << "Expiration date mmddyyyy (0 if none): ";
std::cin >> expire_date;
std::cout << "ID number: ";
std::cin >> identify_num;
std::cout << "DOB mmddyyyy (0 if not listed)";
std::cin >> birthdate;
std::cout << std::endl << std::endl;
IDCard* identification = new IDCard(institute_name, card_name, expire_date, identify_num, birthdate);
cardbook.push_back (identification);
}
else if (card_type == 3)
{
std::cout << "Institution name: ";
getline (std::cin, institute_name);
std::cin.ignore();
std::cout << "Cardholder name: ";
getline (std::cin, card_name);
std::cin.ignore();
std::cout << "Expiration date mmddyyyy (0 if none): ";
std::cin >> expire_date;
std::cout << "Account number: ";
std::cin >> account_num;
std::cout << "Card Security Code: ";
std::cin >> secure_code;
std::cout << std::endl << std::endl;
BankCard* bank = new BankCard (institute_name, card_name, expire_date, account_num, secure_code);
cardbook.push_back (bank);
}
为此,创建包含不同类型的向量
可以创建类层次结构并在 vector
中存储指向基类型的指针。听起来您已经创建了层次结构,但您还没有显示您的代码,我无法进一步评论您的具体问题。
例
// This example creates a base type with 2 derived types and stores the
// derived types in a `vector` using a base type pointer.
//
// The polymorphism is observed when `Base::action` is called but
// `action` from the "real" type `Foo` and `Bar` is invoked.
#include <iostream>
#include <memory>
#include <vector>
class Base
{
public:
virtual ~Base() {}
virtual void action() const = 0;
};
class Foo : public Base
{
public:
void action() const override { std::cout << "Foo::actionn"; }
};
class Bar : public Base
{
public:
void action() const override { std::cout << "Bar::actionn"; }
};
int main()
{
// A `vector` of base type pointers
std::vector<std::unique_ptr<Base>> values;
// Add a `Foo` type to the `vector`
values.emplace_back(new Foo());
// Add a `Bar` type to the `vector`
values.emplace_back(new Bar());
// For each `v` (which is a `Base` pointer) in `values`
for(auto& v : values)
{
// Calling `action` through the `Base` pointer
v->action();
}
return 0;
}
输出
Foo::action
Bar::action
如果您仍然遇到问题,请提出一个新问题,如果您希望获得有用的帮助,这也应该是一个更好的问题。请务必阅读以下内容:
- https://stackoverflow.com/help/mcve
- https://stackoverflow.com/help/how-to-ask
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- 如何使用基类指针引用派生类成员
- 将派生指针分配给C 中的其他派生类型
- 如何通过派生指针调用基类中的模板成员函数
- 如何将带有恒定指针作为参数的模板的派生指针使用
- 无法返回包含派生指针的向量
- 如何创建派生指针类变量
- C++强制转换为派生指针或设计错误
- 无法从派生指针访问公共基成员
- C++发送派生指针作为参数
- 将专用基指针强制转换为专用于其他模板参数的派生指针("adding on"专用化)
- 在派生指针上显式调用基类析构函数时编译错误
- 安全派生指针值的示例
- 安全派生指针的整数表示形式
- c++多态克隆:如何从基指针获得派生指针
- 如果我将派生指针转换为基指针,这两个指针是否保证具有相同的值?
- 以任何方式将基指针强制转换为派生指针是否危险
- 将派生指针隐式强制转换为其相应基类的引用
- 当派生指针仅添加方法时,将基指针强制转换为派生指针的有效性
- c++转换指针到派生指针到基指针的例子