Sinflar do‘stlari
Bir sinf ikkinchi sinfning umumiy usullaridan foydalanishi mumkin. Masalan: Bir sinf obyektlari massivlari bilan ishlovchi funksiyalarni ikkinchi sinf oshkor statik usullari sifatida ta’riflash mumkin.
#include
using namespace std;
class goods
{
string name;
long price;
float percent;
public:
goods() {};
goods(string name,long price, float percent)
{
goods::name= name;
goods::price= price;
goods::percent= percent;
}
void print()
{
cout << name <
cout << price;
cout << percent;
}
};
class array_goods
{
public:
static void all_input(struct goods a[], int n)
{
string name;
long price;
float percent;
for(int i=0; i
{
cin >> name >> price >> percent;
a[i] = goods(name, price, percent);
}
};
static void all_print(struct goods a[], int n)
{
for(int i=0; i
a[i].print();
}
};
int main()
{
goods a[]= {goods("smit", 34, 0.5), goods("bobbi", 45, 0.7), goods("pit", 56, 0.8)};
array_goods::all_print(a, 3);
array_goods::all_input(a, 3);
array_goods::all_print(a, 3);
return 0;
}
Do'stlaringiz bilan baham: |