Why my child class cannot access parent class data cause my calculation have rubbish value?
CODE
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
class Book
{
private:
int box;
double price;
public:
Book(int =0, double =15.00);
void setdata(int, double);
void calcBook();
void print();
protected:
double totalbookprice;
};
Book::Book(int b, double p)
{
box=b;
price=p;
}
void Book::setdata(int b, double p)
{
box=b;
price=p;
}
void Book::calcBook()
{
totalbookprice=box*price;
}
void Book::print()
{
cout<<box<<" box(es) of Book priced RM"<<setprecision(2)<<fixed<<price<<endl;
calcBook();
}
class Magazine
{
private:
int bundle;
double price;
public:
Magazine(int =0, double =5.00);
void setdata(int, double);
void calcMagazine();
void print();
protected:
double totalMagprice;
};
Magazine::Magazine(int b, double p)
{
bundle=b;
price=p;
}
void Magazine::setdata(int b, double p)
{
bundle=b;
price=p;
}
void Magazine::calcMagazine()
{
totalMagprice=bundle*price;
}
void Magazine::print()
{
cout<<bundle<<" bundle(s) of Magazine priced RM"<<setprecision(2)<<fixed<<price<<endl;
calcMagazine();
}
class Newspaper
{
private:
int piece;
double price;
public:
Newspaper(int =0, double =1.00);
void setdata(int,double);
void calcNewspaper();
void print();
protected:
double totalNewprice;
};
Newspaper::Newspaper(int b, double p)
{
piece=b;
price=p;
}
void Newspaper::setdata(int b, double p)
{
piece=b;
price=p;
}
void Newspaper::calcNewspaper()
{
totalNewprice=piece*price;
}
void Newspaper::print()
{
cout<<piece<<" piece(s) of Newspaper priced RM"<<setprecision(2)<<fixed<<price<<endl;
calcNewspaper();
}
class ReadingMaterial:public Book,public Magazine, public Newspaper
{
private:
double totalprice;
public:
ReadingMaterial(double =0);
void setdata(int,double,int,double,int,double);
void totalcalc();
void print();
};
ReadingMaterial::ReadingMaterial(double a)
{
totalprice=a;
}
void ReadingMaterial::setdata(int bb, double pb, int bm, double pm, int bn, double pn)
{
Book::setdata(bb,pb);
Magazine::setdata(bm,pm);
Newspaper::setdata(bn,pn);
ReadingMaterial::totalcalc();
}
void ReadingMaterial::totalcalc()
{
totalprice=totalbookprice+totalMagprice+totalNewprice;
cout<<totalbookprice<<endl; //HERE GOT PROBLEM!!!!
cout<<totalMagprice<<endl; //HERE GOT PROBLEM
cout<<totalNewprice<<endl;//HERE GOT PROBLEM
}
void ReadingMaterial::print()
{
cout<<"You have buy:\n";
Book::print();
Magazine::print();
Newspaper::print();
// Book::calcBook();
// Magazine::calcMagazine();
// Newspaper::calcNewspaper();
cout<<"Total you have spent are: RM"<<fixed<<setprecision(2)<<totalprice<<endl
<<"Thank you\n";
}
void main()
{
ReadingMaterial obj;
int b=0;
int m=0;
int n=0;
int choice;
char decision;
do{
cout<<"What do you want to buy?\n"
<<" 1.Book\t\tRM15.00\n"
<<" 2.Magazine\tRM5.00\n"
<<" 3.Newspaper\tRM1.00\n";
cin>>choice;
if(choice==1)
{
cout<<"How many book you want to buy?\n";
cin>>b;
}
else if(choice==2)
{
cout<<"How many Magazine you want to buy?\n";
cin>>m;
}
else if(choice==3)
{
cout<<"How many piece Newspaper you want to buy?\n";
cin>>n;
}
else
cout<<"You enter invalid choice!";
cout<<"Do you want to continue? <y> or <n>:";
cin>>decision;
}while(decision!='n');
obj.setdata(b,15,m,5,n,1);
obj.print();
}
This post has been edited by Dark_Nexus: 29 Mar, 2006 - 09:15 AM