Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,628 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,047 people online right now. Registration is fast and FREE... Join Now!




Inheritance

 
Reply to this topicStart new topic

Inheritance, Sales Reading Material program

henry
post 28 Mar, 2006 - 08:13 AM
Post #1


D.I.C Head

**
Joined: 3 Jan, 2006
Posts: 96


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 28 Mar, 2006 - 01:22 PM
Post #2


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


where exactly are you getting an error?

derived classes can only access members that are public or protected, they cannot access private members.
User is offlineProfile CardPM

Go to the top of the page

henry
post 28 Mar, 2006 - 07:45 PM
Post #3


D.I.C Head

**
Joined: 3 Jan, 2006
Posts: 96


My Contributions


It don't have error but the calculation is WRONG!!! crazy.gif
CODE

void ReadingMaterial::totalcalc()
{
        totalprice=totalbookprice+totalMagprice+totalNewprice;
        cout<<totalbookprice<<endl; //HERE GOT PROBLEM!!!! Output: -9.25596e+061
        cout<<totalMagprice<<endl; //HERE GOT PROBLEM Output: -9.25596e+061

     cout<<totalNewprice<<endl;//HERE GOT PROBLEM Output: -9.25596e+061

}
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";
  // OUTPUT for totalprice is RM-2776788940479534900000000000000000000000000000000000000000000000.00
}


Where is my mistake?
Help.........
This is my first time apply inheritance.

This post has been edited by Dark_Nexus: 29 Mar, 2006 - 09:15 AM
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 29 Mar, 2006 - 01:22 PM
Post #4


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


Make sure you've inialized all variables before using them.
User is offlineProfile CardPM

Go to the top of the page

henry
post 31 Mar, 2006 - 08:19 PM
Post #5


D.I.C Head

**
Joined: 3 Jan, 2006
Posts: 96


My Contributions


I already initialize it but it still not work......
Help me........I want to cry sleepy.gif
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 1 Apr, 2006 - 04:46 PM
Post #6


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,970



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


As was posted before, but i'll be more specific:
Your Book class, has box as a private variable, this value will not be inherited to the sub or child classes, thus when you try to use the value contained inside:
totalbookprice=box*price;
your child class knows that box exists so there is no error, but it cannot use the value contained within it as it is private, you must make this variable protected or public, and then it will be inherited with the same attributes.
User is offlineProfile CardPM

Go to the top of the page

cipherence
post 1 Apr, 2006 - 06:54 PM
Post #7


D.I.C Regular

Group Icon
Joined: 1 Apr, 2006
Posts: 260



Thanked 1 times

Dream Kudos: 650
My Contributions


I got it to work fine.
add:

system("PAUSE");

Between the last 2 lines
and then, where your main() statment is
just make that line:

main()

although the end price messes up ph34r.gif
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 1 Apr, 2006 - 08:51 PM
Post #8


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


main has to have a return type. You cannot have a function without a return type.

the standard is
CODE

int main()
{
//code
}

// OR

int main ( int argc, char *argv[] )
{
//code
}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 1 Apr, 2006 - 11:57 PM
Post #9


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


QUOTE(cipherence @ 1 Apr, 2006 - 08:46 PM)
I got it to work fine.
add:

system("PAUSE");

Although your solution works on specific operating systems, it would be a good idea to mention such a solution without mentioning that it is platform dependant. The pause.exe does not exist on all platforms...therefore the code given could easily result in a runtime error. It would be better to go with some sort of STL supported input.
User is offlineProfile CardPM

Go to the top of the page

henry
post 6 Apr, 2006 - 12:36 AM
Post #10


D.I.C Head

**
Joined: 3 Jan, 2006
Posts: 96


My Contributions


Thank you for your reply.
But I cannot understand what you all wrote.


Where actually the system("PAUSE") should be put?

If can, can you edit my program so the calculation not go wrong...... smile.gif

Please.............
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:36AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month