#include<iostream>
#include<string>
using namespace std;

class A{
private:
string id;
string work;  // same as class B
int salary;
public:
void getid(string _id){
    id=_id;
}
string returnid(){
    return id;
}
void getwork(string _work){
    work= _work;
}
string returnwork(){
    return work;
}
/*
if i need to use the data of objects of class B, how can i do this?
void calculation(){
    salary= the price of the same work in class B
    let say. all work
    salary = b[0].returnprice()+b[1].returnprice()+b[2].returnprice()+b[3].returnprice();

    so how can i make it possible?
    */
};

class B{
private:
string work;
string price;
void getwork(string _work){
    work= _work;
    }
string returnwork(){
    return work;
}
void getprice(int _price){
    price= _price;
}
int returnprice(){
    return price;
}
};

A a[50];
B b[50];
string work[]={"001","002","003","004"};
int price[]={5000,6000,7000,8000};

void initializeB(){    // make 4 objects of class B
for(int i=0; i<4; i++){
    b[i].getwork(work[i]);
    b[i].getprice(price[i]);
    }
}

int main(){
initializeB();
return 0;
}

2 things I noticed, .

If returnprice is returning an int then price should be an int not a string.

Class B has no public: modifier, therefore nothing is visible outide the class.

And if i were you, i will bring in the class object inside the main. Then the initialize fucntion a template so that you can use that for both class.

template<class T, int size>
void init(T, size){
// Then you can initialize all you class

}

okay sorry for the typing mistake.
It's "public" not "private" in class B.

And i am not quite familiar with template class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.