Zolak94
Zolak94

Reputation: 126

Inheritance class and constructor

I tried to create program that will have derived class Kafic where it will have same info as Lokal but I could add function sale(); where I would have strings with some info about sales in that coffee shop, I am having problem with inheritance and when I compile this I don't have errors but I get core dumped when I try to run it.

main.cpp

#include <iostream>
#include <string>
#include "place.h"
#include "coffeeshop.h"

using namespace std;

void choosePlace(int *choice) {
  cout << "Choose type of place" << endl;
  cout << "1. Coffee Shop" << endl;
  cout << "2. Pub" << endl;
  cout << "3. Club" << endl;
  cout << "4. Disco" << endl;
  cout << "0 for exit" << endl;
  cin >> *choice;
}

void chooseCoffeeShop(int *choice) {
  cout << "Choose Coffee Shop" << endl;
  cout << "1. Renesansa" << endl;
  cout << "2. Bridge" << endl;
  cout << "3. Ultra Caffe" << endl;
  cout << "0 for exit" << endl;
  cin >> *choice;
}


int main() {
  CoffeeShop coffee1("Coffee Shop", "Renesansa", "Town Squar");
  Place coffee2("Coffee Shop", "Bridge", "Under the main bridge");
  int choice;
  choosePlace(&choice);
  switch(choice) {
    case 1:
      chooseCoffeeShop(&choice);
      switch(choice) {
        case 1:
          cout << coffee1.getTypeOfCoffeeShop() << " " << coffee1.getNameOfCoffeeShop() << endl;
          cout << coffee1.getAdressOfCoffeeShop() << endl;
          //cout << coffee1.sale("test") << endl;
        break;
        case 2:
          cout << coffee2.getType() << " " << coffee2.getName() << endl;
          cout << coffee2.getAdress() << endl;
    break;
    case 3:

    break;
    case 0:
      cout << "Thanks" << endl;
    return 0;
    default:
      cout << "Wrong choice" << endl;
    return 0;
  }
break;
case 2:

break;
case 3:

break;
case 4:

break;
case 0:
  cout << "Thanks, goodbye" << endl;
return 0;
default:
  cout << "wrong choice" << endl;
return 0;
}
}

place.cpp

#include "place.h"

using namespace std;

Place::Place(string a, string b, string c) {
  type = a;
  name = b;
  adress = c;
}

string Place::getType() {
  return type;
}

string Place::getName() {
  return Name;
}

string Place::getAdress() {
  return adress;
}

place.h

#ifndef PLACE_H
#define PLACE_H
#include <string>

using namespace std;

class Place {
  protected:
    string type;
    string name;
    string adress;
  public:
    Place(string, string, string);
    string getType();
    string getName();
    string getAdress();
};
#endif

coffeeshop.h

#ifndef COFFEESHOP_H
#define COFFEESHOP_H
#include <string>
#include "place.h"

using namespace std;

class CoffeeShop: protected Place {
  public:
    CoffeeShop(string, string, string);
    string getTypeOfCoffeeShop();
    string getNameOfCoffeeShop();
    string getAdressOfCoffeeShop();
    //void sale(string a);
};
#endif

coffeeshop.cpp

#include "coffeeshop.h"

using namespace std;


CoffeeShop::CoffeeShop(string a1, string b1, string c1) : Place(type, name, adress) {
  type = a1;
  name = b1;
  adress = c1;
}

string CoffeeShop::getTypeOfCoffeeShop() {
  return type;
}

string CoffeeShop::getNameOfCoffeeShop() {
  return name;
}

string CoffeeShop::getAdressOfCoffeeShop() {
  return adress;
}

Upvotes: 0

Views: 68

Answers (1)

doctorlove
doctorlove

Reputation: 19252

There may be more than this going wrong, however in

Kafic::Kafic(string a1, string b1, string c1) : Lokal(vrsta, ime, adresa) {
  vrsta = a1;
  ime = b1;
  adresa = c1;
}

we have Kafic inheriting (with protected) from Lokal and the member variables declared in the base calls:

string vrsta;
string ime;
string adresa;

So, you are sending the uninitialise variables to Loka1 which will try to read them to copy (UB/BOOM!/...) and then overwriting them. Do this instead:

Kafic::Kafic(string a1, string b1, string c1) : Lokal(a1, b1, c1) {
}

It's also not good form to put a using namespace in your headers.

Upvotes: 1

Related Questions