Click to See Complete Forum and Search --> : Instantiable Class
johnboy14
February 6th, 2009, 01:28 PM
Can someone in english explain abit about instantiable classes, I know its a class that can be used by other programs but I just can't get my head around working with its attributes and how to really implement an instantiable class, is there any good examples of an instantiable class.
BigEd781
February 6th, 2009, 02:31 PM
An instantiable class is simply a class from which objects of that type can be instantiated. Any class with a public constructor is instantiable:
// can instantiate an instance of this class
class Foo
{
public Foo() { }
}
// this one is not
class NoInst
{
protected NoInst() { }
}
// and neither is this one because it is static
static class StatClass
{
}
johnboy14
February 6th, 2009, 06:23 PM
Do Instantiable Classes actually perform anything or is it the startup class that builds the object with information from the Instantiable class. Is it just used to deliver Information to a class.
BigEd781
February 6th, 2009, 06:38 PM
I'm not sure what you mean. "Instantiable" simply means that you can do this:
MyInstClass var = new MyInstClass();
JonnyPoet
February 8th, 2009, 06:49 PM
For getting the full picture about an instatiable class look at the opposit like abstract classes are. So look for the keyword abstract and you may get a picture of when you may be able to instantiate or when you are able to derive. A static class doesn't need to be instantiated by your code as it is static and you can use its static methods without instantiating it. An abstact class connot be instantiated but you can derive from it and the derived classes normally arn't abstract and can be instantiated.
Get the following picture: (simplified ) A class is a template for an object. To create an object from a class you instantiate this class.
So the class you will write is the pattern for designing an object. At runtime this objects are created by instantiating the class. So if you have a class for example named Person and you have 10 persons ten you need to instantiate 10 objects of the class person and fill each one with the data from one of your persons.
The result will be 10 different objects all of type 'Person' but each contains different data. Such easy.
BTW I really wold suggest you to read a good beginners book, which explains that basic things. For example one of the 'Head First' series or the '..For Dummyies...' series about OOP ( object orientated programming ) :D
johnboy14
February 9th, 2009, 01:03 PM
say I create a class to calculate interest payments,
If I want to use that class in a start up class I have to instantiate that class, how do I do that?, would you declare it like a variable, lets say your Class name is "InterestPaid", would you make an instant of that like
InterestPaid x = newInterestPaid();
Is this Instantiate Classes.
BigEd781
February 9th, 2009, 02:05 PM
say I create a class to calculate interest payments,
If I want to use that class in a start up class I have to instantiate that class, how do I do that?, would you declare it like a variable, lets say your Class name is "InterestPaid", would you make an instant of that like
InterestPaid x = newInterestPaid();
Is this Instantiate Classes.
Yes. It is a variable. The class is simply a template that you can create objects from.
JonnyPoet
February 9th, 2009, 02:51 PM
.....InterestPaid x = newInterestPaid();..
Is this Instantiate Classes.Yes thats exactly what you need to do x is the instance of your class.
darwen
February 9th, 2009, 04:52 PM
I think an example is in order.
Let's look at System::String. Yep, this is a class.
When you do
string myString = "hello";
the compiler turns this into
string myString = new string("hello");
I.e. it creates a string and passes "hello" into its constructor.
Now - when you ask where are instances of classes created and if they're created in the main method what do you think when we consider string ?
Does it make sense to create all strings which are used by the app in the main function of the app ?
No ! It doesn't ! They're created as needed, and so are all other instances of classes.
They are then passed into functions as parameters (where needed) and held in member variables of other classes etc etc.
Get the picture ?
Darwen.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.