Aug 27, 2013 at 8:08pm UTC
need help >>> After adding item ,can't see it when choose view
i'm using inheritance how to create one object from the main class and use it to call a function from any derived class??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#include<iostream>
#include<string>
using namespace std;
struct items
{
string name;
float price;
int amount;
float total=0;
};
class Store
{
protected :
string name;
int amount;
double price,total=0;
items item[100];
int num_items=0;
public :
void set_name(){cout<<"Enter the product you want to add: " ; cin>>name;}
void set_price(){cout<<"\nEnter the price of the unit: " ; cin>>price;}
void set_amount(){cout<<"\nEnter the amount u want: " ; cin>>amount;}
void set_total(){total=(price*amount);}
string get_name(){return name;}
double get_price(){return price;}
int get_amount(){return amount;}
double get_total(){return total;}
};
class Add:protected Store
{
public :
void add()
{
Store::get_name();
int ck=0;
int indx=0;
for (; indx<=num_items; indx++)
{
if (ck>0)
goto end;
if (name == item[indx].name )
{
item[indx].total+= (amount*price);
item[indx].amount += amount;
item[indx].price = price;
ck++;
}
}
indx--;
num_items++;
item[indx].total = (amount*price);
item[indx].amount = amount;
item[indx].price = price;
item[indx].name=name;
end:
ck=0;
}
};
class View:protected Store
{
public :
void display()
{
cout<<"\n=========================================================\n" ;
cout <<"r\titem\t price\t\t quantity\ttotal\n" ;
cout<<"=========================================================\n" ;
for (int indx=0; indx<num_items; indx++)
{
cout<<indx+1<<"\t" <<item[indx].name<<"\t" <<" price : " <<item[indx].price<<"\t amount : " <<item[indx].amount<<"\ttotal :" <<item[indx].total<<endl;
}
cout<<"=========================================================\n\n\n" ;
}
};
int main()
{
Store s1;
Add a1;
View v1;
up:
int x;
{
cout<<"=========================================================\n" ;
cout<<"Enter choice :\n" ;
cout<<"=========================================================\n" ;
cout <<"1- Add item \n" ;
cout <<"2- View item \n" ;
cout <<"5- Exit \n" ;
cout<<"=========================================================\n" ;
cout<<"your choice : " ;
}
cin>>x;
switch (x)
{
case (1):
s1.set_name();
s1.set_price();
s1.set_amount();
a1.add();
goto up;
break ;
case (2):
v1.display();
goto up;
break ;
case (5):
break ;
default :
{
cout <<"error re_enter the choice \n" ;
goto up;
}
}
cin.get();
return 0;
}
Last edited on Aug 27, 2013 at 8:11pm UTC
Aug 27, 2013 at 8:52pm UTC
s1 ,
a1 and
v1 are not the same object. You've created them as three separate objects, on the stack:
1 2 3
Store s1;
Add a1;
View v1;
So setting/changing the states of
s1 and
a1 with
1 2 3 4
s1.set_name();
s1.set_price();
s1.set_amount();
a1.add();
does nothing at all to
v1 . The value of
v1.num_items remains at its initial value. And, since you haven't initialised it anywhere (e.g. in a constructor), that number is undefined. If you're not seeing any output from
v1.display() , it's likely that your compiler is initialising it to 0, although it's a very bad habit to rely on that.
Also, there's absolutely no excuse for using goto in this situation. goto is extremely dangerous, and should be avoided wherever possible. And in this case, it's very, very possible to avoid it.
Last edited on Aug 27, 2013 at 8:54pm UTC
Aug 27, 2013 at 9:02pm UTC
ok so how can i use just one main object
Store s1;
and use it to call the members of derived classes
like that i think!!
Add::s1.add();
i'm not sure
Aug 27, 2013 at 9:06pm UTC
You have to create an object of whichever derived type you want - either an Add or a View, depending on which sort of object you want.
Edit: Your class design seems really weird to me. Inheritance is supposed to model an "is-a" relationship. Have you really thought about what your classes represent? What is an "Add"? What is a "View"? What is a "Store"? How is a "View" a specialised "Store"? How as an "Add" a specialised "Store"?
Last edited on Aug 27, 2013 at 9:20pm UTC