-6

ok, lets say I have this:

class A:
{
Public:
    int dd;
}

Ok, then I have this class:

class B:
{
Public:
    void sss(){ ff=dd; }
    int ff;
}

Ok, the problem is that class B doesn't know what dd is. How can I make class B know it?

eeerahul
  • 1,629
  • 4
  • 27
  • 38
Ramilol
  • 3,394
  • 11
  • 52
  • 84
  • 8
    Are you sure you are understanding classes ? – Alexandre C. Nov 10 '10 at 21:09
  • 6
    Like I've said, and am going to say for the last time, [get a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read it. You don't know basic C++, and you need to. -1's from me from now on, unless your question is about understanding things in the book. – GManNickG Nov 10 '10 at 21:13
  • This is not valid C++. I suggest you get a book on C++, read it, type in and compile all the examples, and make an effort to understand them. – Merlyn Morgan-Graham Nov 10 '10 at 21:15
  • i got the book but it uses big words that i don't understand – Ramilol Nov 10 '10 at 21:15
  • @GMan: Have you said this to @Ramiz before? I've looked through some of @Ramiz's recent history and not seen you say this, maybe I just missed it. I agree that @Ramiz doesn't seem to have done their due diligence in at least trying to learn the language on their own and is asking bad questions. If this is a repeat offender then they might be a candidate for a timed suspension. – John Dibling Nov 10 '10 at 21:29
  • ok from now and on ill search look up in my book for the answer if i couldn't find it ill ask – Ramilol Nov 10 '10 at 21:36
  • @John: Yes. One from [yesterday](http://stackoverflow.com/questions/4129261/c-breaks-on-class-function), for example. But there are others spread out. @Ramiz: We want to help, but these books exist for a reason. We expect a certain amount of effort from you to learn the basics before you try to do anything with the language. – GManNickG Nov 10 '10 at 22:14
  • @Ramiz: I suggest that before you post anything else on SO you thoroughly read and understand everything in "Accelerated C++". If you do this, you will probably not have to ask many questions at all. This book represents more or less the level of C++ understanding SO posters are expected to have. (IMO) – John Dibling Nov 10 '10 at 22:34

6 Answers6

4

It's public: and not Public:.

Because dd is part of A, you need an instance of A to access dd.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
1

The two classes aren't related in any way; since B does not have access to any A objects it can't take the value of its members.

If you were to pass an A object to the B constructor, either by value, reference, or pointer, you could access its dd member since you made it public.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

You need to have an instance of A, and pass it to the sss method in order to access this data:

void sss(A a) { ff = a.dd; }

If you want to have only one copy of dd, rather than a single copy per instance of A, then you'll have to make dd static:

class A
{
public:
    static int dd;
};

class B
{
public:
    void sss() { ff = A::dd; }
    int ff;
};
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
0

Maybe by passing in an A instance?

class B:
{
Public:
    void sss(const A& a){ ff=a.dd; }
    int ff;
}
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
0

Your code does not look like C++ but I will try to answer your question.

There would be several possibilities to do what you want. I just pick one which is easy to understand.

Do someting like this:

class B
{
public:
    void sss() { ff = a.dd; }
    int ff;

private:
    A a;
};

However if you do not tell me what you really want to achieve with that class i.e. which responsibilites the class has this will not help you much.

frast
  • 2,700
  • 1
  • 25
  • 34
0

The code that you present, ...

class A:
{
Public:
    int dd;
}

class B:
{
Public:
    void sss(){ ff=dd; }
    int ff;
}

... is not valid C++.

You can find that out easily enough by trying to compile it. Then the compiler will complain about a lot of irrelevant things, but its first message will be related to the extraneous colon after A. When you remove that and compile again, its first message will be related to Public, as opposed to correct public (lowercase). So on.

Syntacically correct code (that will still produce a compilation error, but not about the basic syntax):

class A
{
public:
    int dd;
};

class B
{
public:
    void sss(){ ff=dd; }
    int ff;
};

Imagine that you create five instances of class A, named a1, a2, a3, a4, and a5. Each such instance has a member dd, so you can refer to these five members as a1.dd, a2.dd, a3.dd, a4.dd, and a5.dd.

Now you create a B instance, naming it 'b'. And you call b.sss(). And that member function effectively does b.ff=dd. Or it would, had it been meaningful and accepted by the compiler. But it can't, for which of a1.dd, a2.dd, a3.dd, a4.dd, and a5.dd is being referred to?

So, since this is a very basic concept in C++, you need a good C++ textbook, such as Bjarne's latest, and start from the beginning.

Alternatively, since C++ is a very complicated programming language, it's probably even better to start with some simpler language, like Java, or even simpler, like JavaScript or Python. I recommend Python. It's very different from C++ in the details, but both languages are "conventional" programming languages. Using Python you'll learn about decision constructs and functions and things, including classes versus instances.

Cheers & hth.,

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331