c++ - 继承类和构造函数

标签 c++ class inheritance

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

主要.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;
}
}

放置.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;
}

放置.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

咖啡店.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

咖啡店.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;
}

最佳答案

问题可能不止于此,但是在

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

我们有 KaficLokal 继承( protected )和在基调用中声明的成员变量:

string vrsta;
string ime;
string adresa;

因此,您将未初始化的变量发送到 Loka1,它会尝试读取它们以进行复制 (UB/BOOM!/...),然后覆盖它们。改为这样做:

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

在标题中放置 using namespace 也不是好的形式。

关于c++ - 继承类和构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42068251/

相关文章:

c++ - 带有类参数的 std::thread 初始化导致类对象被多次复制

python - 是否可以动态继承仅在 python 运行时才知道的类?

java - 抽象类或接口(interface)中的 public static final 字段

C++设计——网络数据包和序列化

c++ - 访问模板化类的非模板基类的静态数据

c++ - ffmpeg/opencv 帧类型分类后如何进行边缘检测

c++ - ((Thermostat*)this)->Thermostat::_dht' 没有类类型

python - 为战斗模拟器生成随机数

c++ - 添加不同的对象到 std::list

c++ - 后递减运算符和逻辑运算符之间的交互