d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Homework
Add Reply New Topic New Poll
Member
Posts: 21
Joined: Mar 28 2013
Gold: 0.00
Apr 11 2013 12:43pm
Basically HMS DOWEIRDSTUFF is calling CargoShip's print() method, everything else is calling Ship's, and I want Drawner and Drowner to call CargoShip's,
I know I'm probably missing just a lil' bit but I cant get it rly

#include <iostream>
#include "Ship.h"
#include "CruiseShip.h"
#include "CargoShip.h"

Ship *shpArray;
int size=3;
int main(){
//
shpArray=new Ship[size];
*shpArray= Ship("HMS Drowner", 1);
*(shpArray+1)= CargoShip("HMS Drawner", 1,12);
*(shpArray+2)=CargoShip("HMS Drewner", 1,123);
CargoShip cs("HMS DOWEIRDSTUFF",1,2);
cs.print();
for(int i=0;i<size;i++){
shpArray[i].print();
}
system("PAUSE");
return 0;
}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 11 2013 06:20pm
there is defiantly not enough information here to help you at all. post all your code.
Member
Posts: 3,663
Joined: Feb 15 2006
Gold: 3,425.00
Apr 11 2013 11:10pm
You're print() method in Ship must be virtual.

virtual void print()

You should read about static and dynamic binding.
Member
Posts: 21
Joined: Mar 28 2013
Gold: 0.00
Apr 12 2013 12:33am
it is. that's the problem,
CargoShip cs("HMS DOWEIRDSTUFF",1,2);
cs.print();
this bit works.
for(int i=0;i<size;i++){
shpArray[i].print();
}
this bit doesn't

Ship.h:
Code

#pragma once
#include <string>
class Ship
{
private:
std::string name;
int year;

public:
Ship(void);
Ship(std::string, int);
~Ship(void);

std::string getName() const;
int getYear() const;

void setName(std::string);
void setYear(int);

virtual void print() const;
};

Cargoship.h:
Code

#pragma once
#include "Ship.h"
class CargoShip : public Ship
{
private:
int tonnage;
public:
CargoShip(void);
CargoShip(std::string,int,int);
~CargoShip(void);

int getTonnage() const;
void setTonnage(int);

void print() const;
};



Ship.cpp:
Code

#include "Ship.h"
#include <iostream>
using namespace std;

Ship::Ship(void)
{
name="";
year=0;
}

Ship::Ship(string arg1, int arg2)
{
name=arg1;
year=arg2;
}

Ship::~Ship(void)
{
}

string Ship::getName() const{
 return name;
}
int Ship::getYear() const{
 return year;
}
void Ship::setName(string arg){
 name=arg;
}
void Ship::setYear(int arg){
 year=arg;
}

void Ship::print() const{
 cout<<"The ship "<<name<<" was built in "<<year<<endl;
}


CargoShip.cpp:
Code

#include "CargoShip.h"
#include <iostream>
using namespace std;

CargoShip::CargoShip(void):Ship()
{
//Ship();
tonnage=0;
}

CargoShip::CargoShip(string arg1,int arg2,int arg3):Ship(arg1,arg2)
{
//Ship(arg1,arg2);
tonnage=arg3;
}


CargoShip::~CargoShip(void)
{
}

int CargoShip::getTonnage() const{
return tonnage;
}

void CargoShip::setTonnage(int arg){
tonnage=arg;
}

void CargoShip::print() const{
cout<<"The ship "<<getName()<<" has a "<<tonnage<<" tones of cargo capacity"<<endl;
}



Member
Posts: 21
Joined: Mar 28 2013
Gold: 0.00
Apr 14 2013 11:53am
bump
Member
Posts: 72
Joined: Aug 16 2003
Gold: 685.27
Apr 15 2013 11:36am
Ship **shpArray;
int size=3;
shpArray=new Ship*[size];
Ship drowner = Ship("HMS Drowner", 1);
CargoShip drawner = CargoShip("HMS Drawner", 1,12);
CargoShip drewner = CargoShip("HMS Drewner", 1,123);
*shpArray= &drowner;
*(shpArray+1)= &drawner;
*(shpArray+2)= &drewner;
for(int i=0;i<size;i++){
shpArray[i]->print();
}
delete[] shpArray;
system("PAUSE");
return 0;
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 18 2013 03:41pm
xarai ver 2
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 18 2013 03:46pm
multis cant figure out which account theyre logged in as lul
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll