lorcan
asked on
Problem: Class B should take values from class A
Hi there...
I've got a Class 'A' with some functions and values and a class 'B' which should use class 'A', like this:
class B { private: A *someAs;};
The problem is how do I get strings from an object of type 'A' into an object of type 'B'?
Can I use A's functions? And if that's possible, how?
I've got a Class 'A' with some functions and values and a class 'B' which should use class 'A', like this:
class B { private: A *someAs;};
The problem is how do I get strings from an object of type 'A' into an object of type 'B'?
Can I use A's functions? And if that's possible, how?
yes u can A's functions just like u use any other function. remember to put reference to A class.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can also use...
pA[index].getAny();
if you declare pA like...
pA = new A[100];
but for this you need to give A a public default constructor.
pA[index].getAny();
if you declare pA like...
pA = new A[100];
but for this you need to give A a public default constructor.
Hi lorcan,
Whenever u create a class, u can either have data-members/functions as public, protected OR private.
Now, U can access public data-members/functions outside the class using a variable/pointer of that particular class.
You cannot access private members/functions from outside the class. i.e. only functions within the class can access other private functions/data-members of that class.
Thus, in your case, you are declaring a pointer to class A in class B.
Thus, u can access ONLY PUBLIC data-members / functions using this pointer. Not to mention, that data-membsrs are normally NOT MADE PUBLIC. Only functions should be normally made PUBLIC. You can always have PUBLIC member functions thru which u can access the data members
You will have to use the deference operator -> as shown in other posts if u use a pointer OR the dot '.' operator if u declare an object of type A in B.
As shown in above posts, u will have to allocate memory to the pointer of type A in constructor
e.g. A *p = new A ;
HTH
Amit
Whenever u create a class, u can either have data-members/functions as public, protected OR private.
Now, U can access public data-members/functions outside the class using a variable/pointer of that particular class.
You cannot access private members/functions from outside the class. i.e. only functions within the class can access other private functions/data-members of that class.
Thus, in your case, you are declaring a pointer to class A in class B.
Thus, u can access ONLY PUBLIC data-members / functions using this pointer. Not to mention, that data-membsrs are normally NOT MADE PUBLIC. Only functions should be normally made PUBLIC. You can always have PUBLIC member functions thru which u can access the data members
You will have to use the deference operator -> as shown in other posts if u use a pointer OR the dot '.' operator if u declare an object of type A in B.
As shown in above posts, u will have to allocate memory to the pointer of type A in constructor
e.g. A *p = new A ;
HTH
Amit
ASKER
Thank you a lot but that's what i've got so far:
class A
{
private:
char *name;
int number;
double price;
public:
A(char *nameE,int numberE,double priceE);
A();
~A();
A(A &a);
A & operator=(A &a);
};
class B
{
private:
A *b; //my array of type A
int menge, *anzahl;
public:
B(A &a);
B();
~B();
};
A::A()
{
name = new char[1];
number = 0;
price = 0.0;
name[0] = '\0';
}
and now I want to use:
1. a default constructor to create b of type A
2. a constructor to get the values from a into b[0] and b[1] and so on...
3. copy b into c of Type B
class A
{
private:
char *name;
int number;
double price;
public:
A(char *nameE,int numberE,double priceE);
A();
~A();
A(A &a);
A & operator=(A &a);
};
class B
{
private:
A *b; //my array of type A
int menge, *anzahl;
public:
B(A &a);
B();
~B();
};
A::A()
{
name = new char[1];
number = 0;
price = 0.0;
name[0] = '\0';
}
and now I want to use:
1. a default constructor to create b of type A
2. a constructor to get the values from a into b[0] and b[1] and so on...
3. copy b into c of Type B
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.