Y
Y
yuharu2015-10-04 16:20:22
OOP
yuharu, 2015-10-04 16:20:22

Solving an OOP problem (C++)?

Task:
Write a program to create a database for a fabric store that also sells yarn. Create a class that stores the titles and the price. From this class, 2 more classes are inherited: the first contains information about the manufacturer of the fabric, and the second contains information about the composition of the yarn and meters per skein. Each of the three classes must have a getdata method through which you can get data from the user from the keyboard and a method that allows you to display data on the screen.
My code. How to get data from the getdata function of the parent class in the derived class for the getdata() function?

class shop
{
protected:
  int n1, n2, *price1, *price2;
  char **cloth, **yarn;
public:
  ~shop()
  {
    delete [] price1;
    delete [] price2;
    for (int i=0; i<n1; i++)
      delete [] cloth[i];
    for (int i=0; i<n2; i++)
      delete [] yarn[i];
  }
    void getdata() 
  {
    cout<<"Input the number of kinds of cloth: ";
    cin>>n1;
    price1=new int[n1];
    cloth=new char*[n1];
    for (int i=0; i<n1; i++)
      cloth[i]=new char[20];
    cout<<"Input name of cloth and price of it:\n";
    for (int i=0; i<n1; i++)
      cin>>cloth[i]>>price1[i];
    cout<<"Input the number of kinds of yarn: ";
    cin>>n2;
    price2=new int[n2];
    yarn=new char*[n2];
    for (int i=0; i<n2; i++)
      yarn[i]=new char[20];
    cout<<"Input name of yarn and price of it:\n";
    for (int i=0; i<n2; i++)
      cin>>yarn[i]>>price2[i];
  }
void outdata()
  {
    cout<<"In stock shop has the following products at a following price:\n";
    for (int i=0; i<n1; i++)
      cout<<cloth[i]<<" "<<price1[i]<<endl;
    for (int i=0; i<n2; i++)
      cout<<yarn[i]<<" "<<price2[i]<<endl;
  }
};

class shop_cloth:public shop
{
  char **fabricator;
public:
  ~shop_cloth()
  {
    for (int i=0; i<n1; i++)
        delete [] fabricator[i];
  }
  void getdata()
  {
    fabricator=new char*[n1];
    for (int i=0; i<n1; i++)
      fabricator[i]=new char[20];
    cout<<"Input the fabricators of cloth:\n";
    for (int i=0; i<n1; i++)
    {
      cout<<cloth[i]<<" ";
      cin>>fabricator[i];
    }
  }
  void outdata()
  {
    cout<<"The fabricators of cloth:\n";
    for (int i=0; i<n1; i++)
      cout<<cloth[i]<<" "<<fabricator[i];
  }
};

class shop_yarn:public shop
{
  char **consist;
  int *length;
  public:
    ~shop_yarn()
    {
      delete [] length;
      for (int i=0; i<n2; i++)
          delete [] consist[i];
    }
    void getdata()
    {
        consist=new char*[n2];
      for (int i=0; i<n2; i++)
          consist[i]=new char[20];
      length=new int[n2];
        cout<<"Input the consist and the length of yarn:\n";
        for (int i=0; i<n2; i++)
        {
          cout<<yarn[i]<<" ";
        cin>>consist[i]>>length[i];
        }
    }
    void outdata()
    {
        cout<<"The consist ant the length of yarn:\n";
        for (int i=0; i<n1; i++)
          cout<<yarn[i]<<" "<<consist[i]<<" "<<length[i];
    }
};

void main()
{
  shop ob;
  ob.getdata();
  shop_cloth ob1;
  shop_yarn ob2;
  ob1.getdata();
  ob2.getdata();
  cout<<endl;
  system("pause");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Moseychuk, 2015-10-04
@fshp

shop::getdata()inside the descendant method.

T
Tlito, 2015-10-04
@tlito

your program looks broken
and it crashes on the line:
with the message:
segmentation failed: core dumped...
and also in C++ the main function cannot be void, and must not only be an int, but also contain a return 0 instruction;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question