现在它不打印Bridge类和子类RailwayBridge的所有变量,它只打印出2个变量,当我为Bridge输入第三个变量名的const print时,它什么都没打印出来!修改后的代码在这里:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
class Bridge
{
protected:
int height;
long payload;
string name;
public:
Bridge();
Bridge(int, long, string);
~Bridge() {
cout << "Message from the \"Bridge\" - destroyed!" << endl;
}
int GetHeight() const {
return height;
}
void SetHeight(int _height) {
height = _height;
}
long GetPayload() const {
return payload;
}
void SetPayload(long);
void SetName(string _name) {
name = _name;
}
std::string GetName() const {
return name;
}
void Print() const;
};
class RailwayBridge : public Bridge
{
private:
unsigned int SL;
public:
RailwayBridge():Bridge(), SL(0) {}
RailwayBridge(int, long, std::string, unsigned int);
virtual ~RailwayBridge() {
cout << endl << "Message from the \"RailwayBridge\" - destroyed!" << endl;
}
unsigned int GetSL() const {
return SL;
}
virtual void Print() const;
};
Bridge::Bridge() : height(0), payload(0), name("0")
{
}
Bridge::Bridge(int Pheight, long Ppayload, std::string Pname) : height(Pheight){
payload = Ppayload;
}
inline void Bridge::SetPayload(long _payload){
payload = _payload;
}
inline void Bridge::Print() const {
cout << "Height = " << height << ", Payload = " << payload << ", Name = " << name << endl;
}
RailwayBridge::RailwayBridge(int Pheight, long Ppayload, std::string Pname, unsigned int PSL) : Bridge(Pheight, Ppayload, Pname)
{
SL = PSL;
}
inline void RailwayBridge::Print() const {
Bridge::Print();
cout << ", SL = " << SL;
}
int main(void)
{
const int N = 3;
RailwayBridge *RWB1 = new RailwayBridge(12, 200, "RVans T", 0);
Bridge *RWB2 = new RailwayBridge(2,30, "Vansu Tilts", 3);
Bridge *Locations[N] = {
new Bridge(1,2, " akmens tilts "),
new RailwayBridge(3, 4, "Rail", 5),
new RailwayBridge(6, 7, "Rails", 8)
};
//clrscr();
cout << "lokaciju masivs: " << endl;
for(int i=0; i<N; i++) {
cout << (i+1) << ". ";
Locations[i]->Print();
cout << endl;
}
cout << endl << "Bridge lokacijas: " << endl;
RWB1->Print();
cout << endl << "Height = " << RWB1->GetHeight() << ".";
cout << endl << "SL = " << RWB1->GetSL() << "." << endl << endl;
for(int k=0; k<N; k++) {
delete Locations[k];
}
delete RWB1;
delete RWB2;
while (kbhit())
getch();
getch();
return 0;
}
答案 0 :(得分:1)
在您的RailwayBridge构造函数的定义中,您忽略了将参数(即我猜的桥的名称)传递给Bridge的构造函数。
您应该按照以下方式进行更改。
RailwayBridge::RailwayBridge(int Pheight, long Ppayload, string s, unsigned int PSL) : Bridge(Pheight, Ppayload, s) {
SL = PSL;
}
答案 1 :(得分:1)
代码清理+一些评论
#include <iostream.h>
#include <conio.h>
#include <cstring.h>
using namespace std;
//---------------------------------------------------------------------------
class Bridge
{
protected:
int height;
long payload;
string name;
public:
Bridge();
Bridge(int, long, string);
~Bridge() {
cout << "Message from the \"Bridge\" - destroyed!" << endl;
}
int GetHeight() const {
return height;
}
void SetHeight(int _height) {
height = _height;
}
long GetPayload() const {
return payload;
}
void SetPayload(long);
void SetName(string _name) {
name = _name;
}
string GetName() const {
return name;
}
void Print() const;
};
//---------------------------------------------------------------------------
Bridge::Bridge() : height(0), payload(0), name("0")
{
}
Bridge::Bridge(int Pheight, long Ppayload, string Pname) : height(Pheight) {
payload = Ppayload;
}
inline void Bridge::SetPayload(long _payload) {
payload = _payload;
}
inline void Bridge::Print() const {
cout << "Height = " << height << ", Payload = " << payload << endl;
}
//---------------------------------------------------------------------------
class RailwayBridge : public Bridge
{
private:
unsigned int SL;
public:
RailwayBridge():Bridge(), SL(0) {} // ?? Constructor call other constructor?
RailwayBridge(int, long, string, unsigned int);
virtual ~RailwayBridge() {
cout << endl << "Message from the \"RailwayBridge\" - destroyed!" << endl;
}
unsigned int GetSL() const {
return SL;
}
virtual void Print() const;
};
//---------------------------------------------------------------------------
// There no constructor: Bridge(Pheight, Ppayload) as Bridge(int, long)
RailwayBridge::RailwayBridge(int Pheight, long Ppayload, string, unsigned int PSL) : Bridge(Pheight, Ppayload) {
SL = PSL;
}
inline void RailwayBridge::Print() const {
Bridge::Print();
cout << ", SL = " << SL;
}
//---------------------------------------------------------------------------
void main(void)
{
const int N = 3;
RailwayBridge *RWB1 = new RailwayBridge(12);
Bridge *RWB2 = new RailwayBridge();
Bridge *Locations[N] = {
new Bridge(1,2, "akmens tilts"),
new RailwayBridge(),
new RailwayBridge(20)
};
clrscr();
cout << "Array of locations: " << endl;
for(int i=0; 1<N; i++) {
cout << (i+1) << ". ";
Locations[i]->Print();
cout << endl;
}
cout << endl << "Bridge loactions: " << endl;
RWB1->Print();
cout << endl << "Height = " << RWB1->GetHeight() << ".";
cout << endl << "SL = " << RWB1->GetSL() << "." << endl << endl;
for(int k=0; k<N; k++) {
delete Locations[k];
}
delete RWB1;
delete RWB2;
while (khbit())
getch();
getch();
}