【问题标题】:can someone please explain what is wrong in this c++ code and provide a solution有人可以解释一下这个 c++ 代码有什么问题并提供解决方案吗
【发布时间】:2020-12-30 03:18:50
【问题描述】:

我已经声明了一个类 Products 和另一个类 CD,该类 CD 继承了类 Products。 现在我已经声明了一个构造函数来更新它的值。我收到一个错误

#include <iostream>
#include <string>

class Products
{
private:
  std::string name;
  std::string type;
  double price;
public:
  virtual std::string getname();
  virtual double getprice();
  virtual void show();
  std::string gettype()
  {
    return type;
  }
};

class CD: public Products
{
private:
  std::string artist;
  std::string studio;
public:

  CD(std::string sname,double sprice,std::string sartist,std::string sstudio)
  {
    this->type = "CD";
    this->name = sname ;
    this->price = sprice;
    this->artist = sartist;
    this->studio = sstudio;
  }
  
  void show()
  {
    std::cout<<"\nName of the CD:\t"<<this->name;
    std::cout<<"\nArtist of the CD:\t"<<this->artist;
    std::cout<<"\nStudio of the CD:\t"<<this->studio;
    std::cout<<"\nPrice of the cd:\t"<<this->price;
  }
  
};


int main()
{
CD obj("Oceans",49,"somesinger","somestudio");
}

错误:

在构造函数中'CD::CD(std::string, double, std::string)';

'std::string Products::type' 在这个上下文中是私有的

this->type="CD";

'std::string Products::name' 在此上下文中是私有的

this->name=sname;

'double Products::price' 在此上下文中是私有的

this->price= sprice;

基本上它不会对 CD 类的私有数据成员给出错误,而只是对从 Products 类继承的数据成员给出错误

【问题讨论】:

  • 例如“类型是私有类成员”的哪一部分你不清楚?
  • CD 无权访问其基类的私有成员。这就是private 的意义所在。
  • type 只是一个数据成员@Sam Varshavchik
  • 也许你的意思是 protected 就像“派生类可以完全访问这些字段”而不是 private 意思是“离开我的草坪你们这些笨孩子!”
  • 打开你的课本,阅读privateprotectedpublic之间的区别。

标签: c++ class inheritance


【解决方案1】:
#include <iostream>
#include <string>

class Products
{
    private:
      std::string m_name;
      std::string m_type;
      double m_price;
public: 
  // No need for your setters/getters to be virtual 
  // if the derived class won't override anything or not
  const std::string& getType() const { return m_type; }
  const std::string& getName() const { return m_name; } 
  double getPrice() const { return m_price; } 


  void setType(const std::string& new_type) { m_type = new_type; }
  void setName(const std::string& new_name) { m_name = new_name; }
  void setPrice(double new_price) { m_price = new_price; }
  
  // Force derived class to override function
  virtual void show() = 0;
};

class CD: public Products
{
private:
  std::string artist;
  std::string studio;
public:

  CD(std::string sname,double sprice,std::string sartist,std::string sstudio)
  {
    this->setType("CD");
    this->setName(sname) ;
    this->setPrice(sprice);
    this->artist = sartist;
    this->studio = sstudio;
  }
  
  void show()
  {
    std::cout<<"\nName of the CD:\t"<<this->getName();
    std::cout<<"\nArtist of the CD:\t"<<this->artist;
    std::cout<<"\nStudio of the CD:\t"<<this->studio;
    std::cout<<"\nPrice of the cd:\t"<<this->getPrice();
  }
  
};


int main()
{
    CD obj("Oceans",49,"somesinger","somestudio");
    obj.show();
}

我希望您了解这里的一些变化。首先删除 virtual 关键字。在您的情况下,setter/getter 不需要是虚拟的,因为它们没有被覆盖或不需要基于当前示例。其次,setter/getter 被设置为相应地访问私有成员。我们现在在 CD 类中使用这些函数。此外,我们将函数 show() 更改为纯虚拟函数,注意最后的 = 0。我添加了一条评论,说这会强制派生类覆盖该函数。最后,你的 main 没有做任何事情,所以我添加了一个 obj.show() 来实际打印一些东西。

【讨论】:

  • 这些被添加为虚拟,因为这不是整个程序。这只是整个库存管理程序的一个部分,将整个项目放在她身上似乎没有意义,所以我只写了导致错误的部分。不过感谢您的回答。
【解决方案2】:

在这个解决方案中,我为Products添加了一个构造函数,CD的构造函数调用它来初始化Products私有的成员。

我删除了getNamegetPrice 上的virtual,因为这些功能不会改变其他产品。

show 仍然是虚拟的,我将其拆分为 Products 中的一部分和 CD 中的一部分,因此它们各自显示各自的字段。这会根据变量所在的位置分隔打印,例如,从Products 派生的另一个类不必重新实现名称和价格的打印。

#include <iostream>
#include <string>

class Products
{
private:
  std::string name;
  std::string type;
  double price;
public:
  std::string getname(); // Does not need to be virtual, as it's not overriden
  double getprice(); // Also does not need to be virtual
  virtual void show() const {
    std::cout<<"\nName of the " << type << ":\t"<<this->name;
    std::cout<<"\nPrice of the " << type << ":\t"<<this->price;
  };
  Products (const std::string &stype, double sprice, const std::string &sname)
  : name (sname), type (stype), price (sprice) {
  }
  std::string gettype() const
  {
    return type;
  }
};

class CD: public Products
{
private:
  std::string artist;
  std::string studio;
public:

  CD(const std::string &sname,double sprice, const std::string &sartist, const std::string &sstudio)
  : Products ("CD", sprice, sname)
  {
    artist = sartist;
    studio = sstudio;
  }
  
  void show() const override
  {
    Products::show(); // Call parent show() to show the basics
    std::cout<<"\nArtist of the " << gettype() << ":\t"<<this->artist;
    std::cout<<"\nStudio of the " << gettype() << ":\t"<<this->studio;
  }
  
};

int main()
{
    Products shoe ("Shoe", 3.49, "Nike runner");
    shoe.show();
    
    CD obj("Oceans",49,"somesinger","somestudio");
    obj.show();

}

【讨论】:

    猜你喜欢
    • 2020-05-02
    • 1970-01-01
    • 2021-01-17
    • 2021-10-15
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多