C++Primer Plus 14章 const char* 类的值不能转换为char *问题

 
#pragma once #ifndef WORKER0_H_ #define WORKER0_H_ #include <string> class Worker // an abstract base class { private: std::string fullname; long id; public: Worker() : fullname("no one"), id(0L) {} Worker(const std::string & s, long n) : fullname(s), id(n) {} virtual ~Worker() = 0; // pure virtual destructor virtual void Set(); virtual void Show() const; }; class Waiter : public Worker { private: int panache; public: Waiter() : Worker(), panache(0) {} Waiter(const std::string & s, long n, int p = 0) : Worker(s, n), panache(p) {} Waiter(const Worker & wk, int p = 0) : Worker(wk), panache(p) {} void Set(); void Show() const; }; class Singer : public Worker { protected: enum { other, alto, contralto, soprano, bass, baritone, tenor }; enum { VTypes = 7 }; private: static const char *pv[VTypes]; int voice; public: Singer() : Worker(), voice(other) {} Singer(const std::string & s, long n, int v = other) : Worker(s, n), voice(v) {} Singer(const Worker & wk, int v = other) : Worker(wk), voice(v) {} void Set(); void Show() const; }; #endif // WORKER0_H_ #include "worker0.h" #include <iostream> using std::cout; using std::cin; using std::endl; // Worker methods // must implement virtual destructor, even if pure Worker::~Worker() {} void Worker::Set() { cout << "Enter worker's name: "; getline(cin, fullname); cout << "Enter worker's ID: "; cin >> id; while (cin.get() != '\n') continue; } void Worker::Show() const { cout << "Name: " << fullname << "\n"; cout << "Employee ID: " << id << "\n"; } // Waiter methods void Waiter::Set() { Worker::Set(); cout << "Enter waiter's panache rating: "; cin >> panache; while (cin.get() != '\n') continue; } void Waiter::Show() const { cout << "Category: waiter\n"; Worker::Show(); cout << "Panache rating: " << panache << "\n"; } // Singer methods //char *arr[4] = { "hello", "world", "shannxi", "xian" }; //arr就是我定义的一个指针数组,它有四个元素,每个元素是一个char *类型的指针, //这些指针存放着其对应字符串的首地址。 const char * Singer::pv[VTypes]={ "other", "alto", "contralto", "soprano", "bass", "baritone", "tenor" };
//在c中字符文本是数组char,在c++是数组const char,参看官网的处理 void Singer::Set() { Worker::Set(); cout << "Enter number for singer's vocal range:\n"; int i; for (i = 0; i < VTypes; i++) { cout << i << ": " << pv[i] << " "; if (i % 4 == 3) cout << endl; } if (i % 4 != 0) cout << endl; while (cin >> voice && (voice < 0 || voice >= VTypes)) cout << "Please enter a value >= 0 and < " << VTypes << endl; while (cin.get() != '\n') continue; } void Singer::Show() const { cout << "Category: singer\n"; Worker::Show(); cout << "Vocal range: " << pv[voice] << endl; } #include <iostream> #include "worker0.h" const int LIM = 4; int main() { Waiter bob("Bob Apple", 314L, 5); Singer bev("Beverly Hills", 522L, 3); Waiter w_temp; Singer s_temp; Worker * pw[LIM] = { &bob, &bev, &w_temp, &s_temp }; int i; for (i = 2; i < LIM; i++) pw[i]->Set(); for (i = 0; i < LIM; i++) { pw[i]->Show(); std::cout << std::endl; } system("pause"); return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值