473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Scope of object variable in classes

I'm fundamentally confused about this. Can someone please explain it to me clearly?

Say I have two classes A and B. I want to create an object from Class B in class A, and I want to be able to access and work with this object in all of the functions in class A. Do I create the object in the public/private data section of Class B? Do I declare the object in class B's constructor? What exactly do both of those things do? Does it effect the variables scope if I allocate it dynamically with 'new?' I don't want to use 'new' if I don't have to.
May 17 '07 #1
2 1300
Here The Basic thing is Once you create an Object of Class B, Inside class A.
That object becomes a Data member of Class A and thus; just by creating
an Object of Class A, you can utilise the features of both the classes.

This is called Composition ( One of the Famous Pattern among
the Three which are Aggregation, Association and Composition )

I'm fundamentally confused about this. Can someone please explain it to me clearly?

Say I have two classes A and B. I want to create an object from Class B in class A, and I want to be able to access and work with this object in all of the functions in class A. Do I create the object in the public/private data section of Class B? Do I declare the object in class B's constructor? What exactly do both of those things do? Does it effect the variables scope if I allocate it dynamically with 'new?' I don't want to use 'new' if I don't have to.
May 17 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You have not identified the complete relationship between Class A and Class B.

If the object of Class B is to change independently from the object of Class A, then you have an Association and Class A should contain a handle or reference to the Class B object.

If the Class B object is really part of the Class A object, then you have Composition and the Class B object becomes a private member variable of Class A.

If the Class B methods are to be part of the Class A interface, then you publicly derive Class A from Class B provided Class A IS-A kind of Class B. Otherwise, you have Composition and and object of Class B is a private data member in Class A and you write wrapper methods in Class A for the Class B methods. Here, and alternative to the Class A wrapper functions is to have Class A inherit privately from Class B.
May 17 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
7
by: surfrat_ | last post by:
Hi, My project has about 4 source files (xxx.cs) and I am having a problem with scope between the files. If I put the code all within one class everything works OK. Can you please point me to...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Nitro | last post by:
Hello, today I wrote this piece of code and I am wondering why it does not work the way I expect it to work. Here's the code: y = 0 def func(): y += 3 func()
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.