简体   繁体   中英

C++ abstract base class

I am new to C++ and come from Java, I wanted to ask how exactly do I make the base class abstract and to make the classes that inherit it to have the same data types and constructor plus their own ones? I looked at various different methods but I cannot quite seem to be able to find it and also see stuff about destructors so that the class cannot be initialized. Can you give me some tips on how to improve it? How do I make the Book class, so that it inherits the constructor of the Item and its methods?

Item class:

#ifndef _ITEM_H_
#define _ITEM_H_

class Item
{
 private:
  static int item_id = 0;
  string name;
  string description;
  double price;
  int stock;
  int sales;
  
 pulic:
  virtual ~Item (string name, string description, double price, int stock)
{  
  this->name = name;
  this->description = description;
  this->price = price;
  this->stock = stock;
  this->sales = 0;
  this->item_id += 1;
}
  
  virtual ~Item (string name, string description, double price)
{
  this->name = name;
  this->description = description;
  this->price = price;
  this->stock = 0;
  this->sales = 0;
  this->item_id += 1;
}
  
  double getPrice()
  {
    return this->price;
  }
  
  string getName()
  {
    return this->name;
  }

  string getDescription()
  {
    return this->description;
  }

  int getSales()
  {
    return this->sales;
  }

  int getStock()
  {
    return this->stock;
  }

  int getItemID()
  {
    return this->item_id;
  }
};
#endif 

Book class that inherits Item:

#include "item.h"

class Book : public Item
{
 private:
  string author;
  string genre;

 public:
  Book(string author, string genre)
    {
      this->author = author;
      this->genre = genre;
    }
}

I am new to C++ and come from Java, I wanted to ask how exactly do I make the base class abstract and to make the classes that inherit it to have the same data types and constructor plus their own ones? I looked at various different methods but I cannot quite seem to be able to find it and also see stuff about destructors so that the class cannot be initialized. Can you give me some tips on how to improve it? How do I make the Book class, so that it inherits the constructor of the Item and its methods?

Item class:

#ifndef _ITEM_H_
#define _ITEM_H_

class Item
{
 private:
  static int item_id = 0;
  string name;
  string description;
  double price;
  int stock;
  int sales;
  
 pulic:
  virtual ~Item (string name, string description, double price, int stock)
{  
  this->name = name;
  this->description = description;
  this->price = price;
  this->stock = stock;
  this->sales = 0;
  this->item_id += 1;
}
  
  virtual ~Item (string name, string description, double price)
{
  this->name = name;
  this->description = description;
  this->price = price;
  this->stock = 0;
  this->sales = 0;
  this->item_id += 1;
}
  
  double getPrice()
  {
    return this->price;
  }
  
  string getName()
  {
    return this->name;
  }

  string getDescription()
  {
    return this->description;
  }

  int getSales()
  {
    return this->sales;
  }

  int getStock()
  {
    return this->stock;
  }

  int getItemID()
  {
    return this->item_id;
  }
};
#endif 

Book class that inherits Item:

#include "item.h"

class Book : public Item
{
 private:
  string author;
  string genre;

 public:
  Book(string author, string genre)
    {
      this->author = author;
      this->genre = genre;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM