2017-05-19 13 views
-6

이 아래에있는 내 코드 :이 코드를 실행하면작문의 복사 생성자는 어떻게 작성합니까?

class Pulley{ 
private: 
    int noOfTeeth; 

public: 
    Pulley(); 
    Pulley(int teeth); 
    ~Pulley(); 

    void show(); 
}; 

Pulley::Pulley() 
{ 
    cout << "Pulley constructor called!"; 
    noOfTeeth = 0; 
} 

Pulley::Pulley(int teeth) 
{ 
    cout << "Pulley constructor called!"; 
    noOfTeeth = teeth; 
} 

void Pulley::show() 
{ 
    cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth; 
} 

Pulley::~Pulley() 
{ 

} 

class GearBox{ 
    private: 
    char *transmission; 
    Pulley p; 

    public: 
    GearBox(); 
    GearBox(char *trans, int pTeeth); 
    ~GearBox(); 

    void show(); 
}; 

GearBox::GearBox(): p() 
{ 
    cout << "Gearbox constructor called!"; 
    transmission = NULL; 
} 

GearBox::GearBox(char *trans, int pTeeth): p(pTeeth) 
{ 
    cout << "Gearbox constructor called!"; 
    if(trans != NULL) 
    { 
     transmission = new char[strlen(trans)+1]; 
     strcpy(transmission, trans); 
    } 
    else 
    { 
     transmission = NULL; 
    } 
} 

void GearBox::show() 
{ 
    cout << "\n\nTransmission of vehicle: " << transmission; 
    p.show(); 
} 

GearBox::~GearBox() 
{ 

} 

class Vehicle{ 
private: 
    char *model; 
    char *color; 
    GearBox g; 

public: 
    Vehicle(); 
    Vehicle(char *mod, char *col, char *gr); 
    Vehicle(const Vehicle &vh); 
    ~Vehicle(); 

    void show(); 
}; 

Vehicle::Vehicle(): g() 
{ 
    cout << "Vehicle constructor called!"; 
    model = NULL; 
    color = NULL; 
} 

Vehicle::Vehicle(char *mod, char *col, char *gr): g(gr) 
{ 
    cout << "Vehicle constructor called!"; 
    if(mod != NULL) 
    { 
     model = new char[strlen(mod)+1]; 
     strcpy(model, mod); 
    } 
    else 
    { 
     model = NULL; 
    } 

    if(col != NULL) 
    { 
     color = new char[strlen(col)+1]; 
     strcpy(color, col); 
    } 
    else 
    { 
     color = NULL; 
    } 
} 

void Vehicle::show() 
{ 
    cout << "\n\nModel of Vehicle: " << model; 
    cout << "\n\nColor of Vehicle: " << color; 
    g.show(); 
} 

int main() 
{ 
    Pulley p(20); 
    GearBox g("Manual", p); 
    Vehicle V("Honda", "Black", g); 
    V.show(); 
    system("PAUSE"); 
} 

지금, 나는 오류의 제비를 얻을, 나는 그 해결 방안을 무엇인지 모른다. 내가 이해 한 한 가지 오류는 Copy Constructor의 것이므로 아무도 내가 transmission, model and color 문자에 대한 포인터 용 복사 생성자를 쓸 수 있다는 것을 설명 할 수있다. 또한 다른 오류를 해결하는 방법을 알려주십시오. 고마워.

+6

, 다음 번에 복잡성을 약간 추가합니다. 이 많은 코드에 많은 오류가있는 것은 그것이 발생했을 때 해결하지 않았기 때문입니다. ** 작동하지 않는 코드에는 절대로 추가하지 마십시오. ** 오류가 발생하는 동안 최대한이 코드를 단순화하는 것이 좋습니다. 버그가 분명 해지면 더 간단한 예제를 게시 할 수 있습니다. – Beta

+0

문자열을 저장할 때 std :: string을 사용하십시오. 소멸자를 쓰거나 연산자를 복사하지 마십시오. 델리게이트 :: 문자열, std :: vector, std :: unique_ptr 등 –

+0

_ 이제이 코드를 실행하면 많은 오류가 발생합니다. 그 코드와 그 해결 방법을 알지 못합니다 ._ From 이 문은 컴파일 오류가있는 경우 명확하지 않거나 (만약 그렇다면 오류가 무엇입니까?) 런타임에 코드가 실패했습니다 (그렇다면 디버거로 코드를 실행하여 실패한 이유를 찾으셨습니까?). –

답변

-4

는 다음과 같이하십시오 : 당신이 코드를 작성할 때, 완벽하게 작동 작고 간단한 무언가 시작

#include <string> 
#include <iostream> 

class Pulley{ 
private: 
    int noOfTeeth; 

public: 
    Pulley(int teeth = 0); 

    void show(); 
}; 

Pulley::Pulley(int teeth) 
: noOfTeeth(teeth) 
{ 
    std::cout << "Pulley constructor called!"; 
} 

void Pulley::show() 
{ 
    std::cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth; 
} 

class GearBox{ 
    private: 
    std::string transmission; 
    Pulley p; 

    public: 
    GearBox(std::string trans = std::string(), Pulley p = Pulley()); 

    void show(); 
}; 

GearBox::GearBox(std::string trans, Pulley p) 
: transmission(std::move(trans)) 
, p(std::move(p)) 
{ 
    std::cout << "Gearbox constructor called!"; 
} 

void GearBox::show() 
{ 
    std::cout << "\n\nTransmission of vehicle: " << transmission; 
    p.show(); 
} 

class Vehicle{ 
private: 
    std::string model; 
    std::string color; 
    GearBox g; 

public: 
    Vehicle(); 
    Vehicle(std::string mod = "", std::string col = "", GearBox gr = GearBox()); 

    void show(); 
}; 

Vehicle::Vehicle(std::string mod,std::string col, GearBox gr) 
: model(std::move(mod)) 
, color(std::move(col)) 
, g(std::move(gr)) 
{ 
    std::cout << "Vehicle constructor called!"; 
} 

void Vehicle::show() 
{ 
    std::cout << "\n\nModel of Vehicle: " << model; 
    std::cout << "\n\nColor of Vehicle: " << color; 
    g.show(); 
} 

int main() 
{ 
    Pulley p(20); 
    GearBox g("Manual", p); 
    Vehicle V("Honda", "Black", g); 
    V.show(); 
// system("PAUSE"); 
} 
+2

나는이 대답이 불완전하다고 생각할 것이다. 그것은 어떤 변화가 있었는지 나타내지도 않고 그 뒤에있는 이유를 설명하지도 않습니다. –

+1

나에게도 숙제를 해줄 수 있습니까? –

+0

덧글을 올리면 게시 됨 –