R
R
Retr0Hacker2022-04-13 22:10:53
OOP
Retr0Hacker, 2022-04-13 22:10:53

How to create an array of pointers to base class objects, to which to assign addresses of derived class objects?

inheritance task: "Create a base class PRINTED PRODUCTION (name, number of pages are specified). Create derived classes BOOK (data on the author, page size, number of copies are specified) and MAGAZINE (page size, number of copies, frequency of publication are specified). For the entered data on printed products, calculate paper costs (in sq. m.) - for each example and total, sort these examples in descending order of costs. To check, use an array of pointers to base class objects, to which assign the address of objects of derived classes."

Question:
How to implement the input and output of all the data I need.

Well, that is, here is the output of the code in main():
62571fa22ca7b022514605.png
According to my idea, after each entered digit, I must first enter from the computer and then display all the data I need using the virtual function print (). Here's how to make the input and then the output of these data?

#include <iostream>
#include <string>

using namespace std;

class DRUK
{
protected:
  string m_name;
  int m_pages;
public:
  DRUK(const string& name = "", double _pages = 0) : m_pages(_pages), m_name(name) {}
  const string& getName() const
  {
    return m_name;
  }
  int getPages() const
  {
    return m_pages;
  }

  virtual void print()const
  {
    cout << "Название: " << m_name << "\n";
    cout << "Кол-во стораниц: " << m_pages << "\n";
  }
};

class BOOK : public DRUK
{
private:
  string m_author;
  int m_sizeofPages;
  int m_countofPrimir;
public:
  BOOK(const string& name = "", double _pages = 0,
    const string& author = "", double size = 0, double primir = 0)
    : DRUK(name, _pages), m_author(author), m_sizeofPages(size), m_countofPrimir(primir)
  {

  };
  const string& getAuthor() const{return m_author;}
  int getSize() const{return m_sizeofPages;}
  int getCount() const{return m_countofPrimir;}

  virtual void print()const override
  {
    DRUK::print();
    cout << "Автор: " << m_author << "\n" <<
        "Розмер страниц: " << m_sizeofPages << "\n" <<
        "Кол-во экземпляров: " << m_countofPrimir << "\n";
  }
};

class JURNAL : public DRUK
{
private:
  int m_SizeOfPages;
  int m_CountOfPrimir;
  int m_period;
public:
  JURNAL(const string& name = "", double _pages = 0,
    double _size = 0, double _primir = 0, double _period = 0)
    : DRUK(name, _pages), m_SizeOfPages(_size), m_CountOfPrimir(_primir), m_period(_period)
  {

  };
  
  int getSizeOfPages() const { return m_SizeOfPages; }
  int getCountOfPrimir() const { return m_CountOfPrimir; }
  int getPeriod() const { return m_period; }

  virtual void print()const override
  {
    DRUK::print();
    cout << "Розмер страниц: " << m_SizeOfPages << "\n" <<
        "Кол-во экземпляров: " << m_CountOfPrimir << "\n" <<
        "Период вихода: " << m_period << "\n";
  }
};

const ostream& operator<< (ostream& out, const DRUK& druk)
{
  druk.print();
  return out;
}


int main() {

  system("chcp 1251");
  int count;
  cout << "Введите кол-во печатной продукции: ";
  cin >> count;
  DRUK** totalDRUK = new DRUK*[count];

  for (int i = 0; i < count; ++i) {
    std::cout << "Item number " << i + 1 << " is a (1. BOOK/ 2. JURNAL): ";
    char choise;
    std::cin >> choise;
    switch (choise)
    {
    case '1': totalDRUK[i] = new BOOK;
      break;
    case '2': totalDRUK[i] = new JURNAL;
      break;
    default:
      --i;
      cout << "Invalid choise!" << endl;
      break;
    }
  }
  return 0;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question