help for inheritance problem

D

David

Hi all,

I am new in c++. I met such a problem:

class base{

protected:
int a[10];
int b;
public:
.....
}

if I have two child classes,
class child1:public base
{
}
class child2:public base
{
}

I want to do:
(1) change the values of a[] in child1, for example set a[0]=1;
(2) change the values of a[] in child 2, for example set a[1]=2 and I
can get the value a[0]

how I can do? Thanks a lot
 
V

Victor Bazarov

David said:
I am new in c++. I met such a problem:

class base{

protected:
int a[10];
int b;
public:
.....
}

if I have two child classes,
class child1:public base
{
}
class child2:public base
{
}

I want to do:
(1) change the values of a[] in child1, for example set a[0]=1;
(2) change the values of a[] in child 2, for example set a[1]=2 and I
can get the value a[0]

how I can do? Thanks a lot

Just use the syntax you typed in right here. a[0]=1; a[1]=2; and
so on. Also read the FAQ 5.8.

V
 
N

Nick Keighley

David said:
I am new in c++. I met such a problem:

class base{

protected:
int a[10];
int b;
public:
.....
}

if I have two child classes,
class child1:public base
{
}
class child2:public base
{
}

I want to do:
(1) change the values of a[] in child1, for example set a[0]=1;
(2) change the values of a[] in child 2, for example set a[1]=2 and I
can get the value a[0]

how I can do? Thanks a lot

why not-

class child1:public base
{
public:
void update()
{ a[0] = 1; }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top