Logical Error

Hi guys,I'm new to C++, I have written a code which includes inheritance and I have to use the "Arguments Constructor(child class)" to pass certain values to the parent class. Bad luck, Its not getting any better, the constructor won't pass the values. The code is down here, can anyone plz tell me whats wrong here???


#include<iostream>
#include<string>
#include"conio.h"

using namespace std;

class Package
{
protected:
string sname; //variables with s in the start are sender's
string rname; //attributes
string saddress; //variables with r in the start are receiver's
string raddress; //attributes
string scity;
string rcity;
string sstate;
string rstate;
int szip;
int rzip;
double weight_o;
double cost_p_o;
public:
Package()
{
sname='\0';
rname='\0';
saddress='\0';
raddress='\0';
scity='\0';
rcity='\0';
sstate='\0';
rstate='\0';
szip=0;
rzip=0;
weight_o=0;
cost_p_o=0;
}
Package(string sn,string rn,string sa,string ra,string sc,string rc,string ss,string rs,int sz,int rz,double w,double co)
{
sname=sn;
rname=rn;
saddress=sa;
raddress=ra;
scity=sc;
rcity=rc;
sstate=ss;
rstate=rs;
szip=sz;
rzip=rz;
weight_o=w;
cost_p_o=co;
}
virtual double calculateCost()
{
cout<<"Cost: ";
return(weight_o*cost_p_o);
}
};
class TwoDayPackage:public Package
{
protected:
double flat_fee;
public:
TwoDayPackage()
{
Package();
flat_fee=0;
}
TwoDayPackage(string sn,string rn,string sa,string ra,string sc,string rc,string ss,string rs,int sz,int rz,double w,double co,double f)
{
Package(sn,rn,sa,ra,sc,rc,ss,rs,sz,rz,w,co);
flat_fee=f;
}
double calculateCost()
{
cout<<"Cost of Two Day Package: ";
return((weight_o*cost_p_o)+flat_fee);
}
};
class OvernightPackage:public Package
{
protected:
double fee_p_o;
public:
OvernightPackage()
{
Package();
fee_p_o=0;
}
OvernightPackage(string sn,string rn,string sa,string ra,string sc,string rc,string ss,string rs,int sz,int rz,double w,double co,double f)
{
Package(sn,rn,sa,ra,sc,rc,ss,rs,sz,rz,w,co);
fee_p_o=f;
}
double calculateCost()
{
cout<<"Cost of Overnight Package: ";
return((cost_p_o+fee_p_o)*(weight_o));
}
};
int main()
{
TwoDayPackage t("Kamran","Kamran","D Town","E Town","Karachi","Lahore","Sindh","Punjab",45000,45000,120,50,300);
OvernightPackage o("Kamran","Kamran","D Town","E Town","Karachi","Lahore","Sindh","Punjab",45000,45000,120,50,300);
double a;
a=t.calculateCost();
cout<<a<<endl;
a=o.calculateCost();
cout<<a<<endl;

getch();

return 0;
}
Last edited on
Last edited on
well,my question is, I want to use parent argument constructor in the child argument constructor, thats all what I did up there, what your links show,thats same as I did, I wana use argument constructor, and in my rpogram,,, the compiler does not show any error, the problem is that the program doesnt actually provides the correct output, it a logical error, plz if u point it out??
Last edited on
You did not. Pay attention to details.

You wrote
B() { A(); }

Should be
B() : A() {}
in the book,Object Oriented Programming in C++ by Robert Lafore, it was written that
B() { A(); }
would work well but its not recommended due to complex reasons so I thought that it might work here too.. didnt know that this program was so complex that it'll need complex details :3 ;)

and, thanks very much keskiverto, ur info solved my problem, :)

( though, in today's exam, I did this mistake, lol :D )
Topic archived. No new replies allowed.