473,386 Members | 1,630 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,386 software developers and data experts.

about inheritance and array

Hello!

Assume we have the following classes and interfaces.
We have a class called Inventory that is associated to a class called
Product.

We have some classes that is derived from this Product class and from two
interfaces called Wearable and Rentable.
These classes has the following names Book, Soda, Shoe, Coffee, BubbleGum
and Underwear.

Below I have some code and there is a row that I want to ask about.
It's this row "Product[] inventory = read_all_products();" that I have a
question about
Normally when we have an array we do Product[] inventory = new
Product[50]; // allocating memory for the array
But here you assign the return value from method "read_all_products();".
I can't understand the construction assigning the return value from this
read_all_products();"?
What type does this read_all_products(); actually returns?
I just can't understand how it's possible to load the array without
allocation memory by using the new operator.?

If this instead had the following construction I would understand it.
Product[] inventory = new Product[50];
read_all_products();

All this is about explaining this row construction. "Product[] inventory
= read_all_products();"

namespace Inventory
{
public class ProductHandler
{
public static void Main()
{
Product[] inventory = read_all_products(); //load inventory
foreach (Product p in inventory) //Check all products in array
{
if (p is Rentable) //It it rentable?
assign_to_rental_department(p); //Allocate it

if (p is Wearable) //Is it wearable?
{
Wearable w = (Wearable) p; //Yes, cast it so
w.PutOn(); // we can put
it on
}
}
}
}
}

//Tony
Nov 20 '05 #1
4 2945

"Tony Johansson" wrote...
What type does this read_all_products(); actually returns?


It returns a reference to one object, in this case a reference to an
instanmce of an array object, i.e. it doesn't return the array itself.

An array of this type doesn't even consist of instances of objects
themselves, but of *references* to instances of objects.

So...

The array, and the instances themselves, will (hopefully) already exist
someplace when the method returns the reference to the array.

// Bjorn A
Nov 20 '05 #2
Product[] read_all_products()
{
Product[] localInventory = new Product[50]; // allocating memory for
the array
//
// : build Inventory
//
return localInventory;
}

However, you probably don't want to lock yourself in to exactly 50 products,
so
Product[] read_all_products()
{
ArrayList tmpInventory = new ArrayList();
//
// : build Inventory
//
Product[] localInventory = new Product[tmpInventory.Count];
tmpInventory.CopyTo(localInventory);
return localInventory;
}

Tony Johansson wrote:
Hello!

Assume we have the following classes and interfaces.
We have a class called Inventory that is associated to a class called
Product.

We have some classes that is derived from this Product class and from
two interfaces called Wearable and Rentable.
These classes has the following names Book, Soda, Shoe, Coffee,
BubbleGum and Underwear.

Below I have some code and there is a row that I want to ask about.
It's this row "Product[] inventory = read_all_products();" that I
have a question about
Normally when we have an array we do Product[] inventory = new
Product[50]; // allocating memory for the array
But here you assign the return value from method
"read_all_products();". I can't understand the construction assigning the
return value from
this read_all_products();"?
What type does this read_all_products(); actually returns?
I just can't understand how it's possible to load the array without
allocation memory by using the new operator.?

If this instead had the following construction I would understand it.
Product[] inventory = new Product[50];
read_all_products();

All this is about explaining this row construction. "Product[] inventory
= read_all_products();"

namespace Inventory
{
public class ProductHandler
{
public static void Main()
{
Product[] inventory = read_all_products(); //load
inventory foreach (Product p in inventory) //Check all
products in array {
if (p is Rentable) //It it rentable?
assign_to_rental_department(p); //Allocate it

if (p is Wearable) //Is it wearable?
{
Wearable w = (Wearable) p; //Yes, cast it so
w.PutOn(); // we can
put it on
}
}
}
}
}

//Tony


--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 20 '05 #3

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:n6*********************@newsc.telia.net...
<snip>
Below I have some code and there is a row that I want to ask about.
It's this row "Product[] inventory = read_all_products();" that I have a question about
Normally when we have an array we do Product[] inventory = new Product[50]; // allocating memory
for the array
But here you assign the return value from method "read_all_products();".
I can't understand the construction assigning the return value from this read_all_products();"?


Inside read_all_products() there will be the equivalent of new. It will allocate the storage for the
array and it's elements and then return a reference to the array.

It most likely Creates an ArrayList to hold the Products that it reads. It the converts the
ArrayList to an Array and returns a reference to that array.

Bill
Nov 20 '05 #4

"James Curran" <Ja*********@mvps.org> wrote in message
news:OJ****************@tk2msftngp13.phx.gbl...
<snip>
However, you probably don't want to lock yourself in to exactly 50 products, so
Product[] read_all_products()
{
ArrayList tmpInventory = new ArrayList();
//
// : build Inventory
//
Product[] localInventory = new Product[tmpInventory.Count];
tmpInventory.CopyTo(localInventory);
return localInventory;
}

Or instead of Product[] localInventory = new Product[tmpInventory.Count];
tmpInventory.CopyTo(localInventory);


How about
Product[] localInventory = (Product[])(tmpInventory.ToArray(Product));

The call to "new Product[]" is hidden inside of ToArray()

Bill

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Peter Jakobi | last post by:
Hello, i am looking for a tool, which dumps inheritance information of c++ binaries. Does such a tool exist?? Can I retrieve interitance from the output of objdump or something else? Does...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
2
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass'...
8
by: ^MisterJingo^ | last post by:
Hi all, I have a question regarding inheritance. I'll use the following code for an example (its been stripped down to the minimum): // code start using System; class Animal {
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
25
by: raylopez99 | last post by:
First in an occasional series. I'm somewhat experienced in C++, and am using .NET Visual Studio 2005 as the IDE. I'm learning C#. What I don't like about C#, compared to C++ (all flavors): ...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.