2017-02-06 206 views
0

我试图创建将派生类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; 
} 
+2

Stack Overflow中的通讯是英文的。这也适用于代码。请提供使用英文符号名称的[mcve]。确保它确实很小。发布后,仍然有很多不相关的代码。 – IInspectable

回答

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; 

所以,您要发送的uninitialise变量Loka1这将试着去阅读他们复制( UB/BOOM!/ ...),然后覆盖它们。做到这一点,而不是:

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

这也是不好的形式把一个using namespace在你的头。