继承类和构造函数

时间:2017-02-06 12:50:49

标签: c++ class inheritance

我尝试创建将派生类Kafic的程序,它将具有与Lokal相同的信息,但我可以添加函数sale();在那里我会找到关于该咖啡店销售的一些信息的字符串,我有继承问题,当我编译它时我没有错误但是当我尝试运行它时我会被核心转储。

的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;
}

1 个答案:

答案 0 :(得分:1)

但是在

中可能存在更多错误
Kafic::Kafic(string a1, string b1, string c1) : Lokal(vrsta, ime, adresa) {
  vrsta = a1;
  ime = b1;
  adresa = c1;
}

我们KaficLokal继承(带有受保护)和基本调用中声明的成员变量:

string vrsta;
string ime;
string adresa;

所以,你要将未初始化的变量发送给Loka1,它会尝试读取它们来复制(UB / BOOM!/ ...),然后覆盖它们。这样做:

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

在标题中添加using namespace也不是一种好方法。