CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2008
    Posts
    26

    Instantiable Class

    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.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Instantiable Class

    An instantiable class is simply a class from which objects of that type can be instantiated. Any class with a public constructor is instantiable:

    Code:
    // 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
    {
    
    }

  3. #3
    Join Date
    Oct 2008
    Posts
    26

    Re: Instantiable Class

    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.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Instantiable Class

    I'm not sure what you mean. "Instantiable" simply means that you can do this:

    Code:
    MyInstClass var = new MyInstClass();

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Instantiable Class

    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 )
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  6. #6
    Join Date
    Oct 2008
    Posts
    26

    Re: Instantiable Class

    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Instantiable Class

    Quote Originally Posted by johnboy14 View Post
    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.

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Instantiable Class

    Quote Originally Posted by johnboy14 View Post
    .....
    Code:
    InterestPaid x = newInterestPaid();
    ..
    Is this Instantiate Classes.
    Yes thats exactly what you need to do x is the instance of your class.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  9. #9
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Instantiable Class

    I think an example is in order.

    Let's look at System::String. Yep, this is a class.

    When you do

    Code:
    string myString = "hello";
    the compiler turns this into

    Code:
    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured