Object Initialization in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Venkat

    Object Initialization in C++

    Hello All.

    I am having a trouble creating a Object in Case Label, and calling the
    Member Function outside the Switch. Can some one please help me..??

    My code looks like

    int main()
    {
    int number;
    cout <<"1. Chevy"<<endl;
    cout << "2. Toyota"<<endl;
    cout << "enter a number"<<endl;
    cin >> number;
    switch(number)
    {
    case 1:
    {
    Chevy o;
    }
    break;
    case 2:
    {
    Toyota o;
    }
    break;
    default:
    {
    Auto o;
    }
    break;
    }
    o.Description() ;
    return 0;
    }


    In this case I want to call the Description Member Function depending
    on the Object.

    But the Compiler does not know about "o". How can I implement this.??


    Thanks in advance
    -Venkat
  • tom_usenet

    #2
    Re: Object Initialization in C++

    On 3 Oct 2003 07:44:30 -0700, junnuthula@yaho o.com (Venkat) wrote:
    [color=blue]
    >Hello All.
    >
    >I am having a trouble creating a Object in Case Label, and calling the
    >Member Function outside the Switch. Can some one please help me..??[/color]

    Once an automatic object has gone out of scope, it no longer exists,
    so you can't possibly call member functions of it.
    [color=blue]
    >
    >My code looks like[/color]

    What about Chevy, etc.? Do they all derive from Auto?
    [color=blue]
    >
    >int main()
    >{
    > int number;
    > cout <<"1. Chevy"<<endl;
    > cout << "2. Toyota"<<endl;
    > cout << "enter a number"<<endl;
    > cin >> number;
    > switch(number)
    > {
    > case 1:
    > {
    > Chevy o;
    > }
    > break;
    > case 2:
    > {
    > Toyota o;
    > }
    > break;
    > default:
    > {
    > Auto o;
    > }
    > break;
    > }
    > o.Description() ;
    > return 0;[/color]

    Instead:

    int number;
    cout <<"1. Chevy"<<endl;
    cout << "2. Toyota"<<endl;
    cout << "enter a number"<<endl;
    cin >> number;
    Auto* o = 0;
    switch(number)
    {
    case 1:
    {
    o = new Chevy;
    }
    break;
    case 2:
    {
    o = new Toyota;
    }
    break;
    default:
    {
    o = new Auto;
    }
    break;
    }
    o->Description( );
    delete o;
    return 0;
    }
    [color=blue]
    >In this case I want to call the Description Member Function depending
    >on the Object.
    >
    >But the Compiler does not know about "o". How can I implement this.??[/color]

    Using polymorphism. Auto should look like this:

    class Auto
    {
    public:
    virtual ~Auto();
    virtual void Description();
    };

    And the others should override Description.

    Tom

    Comment

    • Josephine Schafer

      #3
      Re: Object Initialization in C++


      "Venkat" <junnuthula@yah oo.com> wrote in message
      news:86545ff.03 10030644.479114 2c@posting.goog le.com...[color=blue]
      > Hello All.
      >
      > I am having a trouble creating a Object in Case Label, and calling the
      > Member Function outside the Switch. Can some one please help me..??
      >
      > My code looks like
      >
      > int main()
      > {
      > int number;
      > cout <<"1. Chevy"<<endl;
      > cout << "2. Toyota"<<endl;
      > cout << "enter a number"<<endl;
      > cin >> number;
      > switch(number)
      > {
      > case 1:
      > {
      > Chevy o;
      > }
      > break;
      > case 2:
      > {
      > Toyota o;
      > }
      > break;
      > default:
      > {
      > Auto o;
      > }
      > break;
      > }
      > o.Description() ;
      > return 0;
      > }
      >
      >
      > In this case I want to call the Description Member Function depending
      > on the Object.
      >
      > But the Compiler does not know about "o". How can I implement this.??
      >[/color]
      A simple way would be to declare a base class Vehicle.
      Derive Chevy, Toyota and Auto from it.
      Declare Description to be a virtual function in Vehicle.
      Then declare Vehicle *p = NULL;
      .....
      {
      case 1:
      p = new Chevy;
      case 2:
      //
      case 3:
      // so on ..
      }
      // then call description
      p ->Description ();
      delete p;

      Don't forget to declare the destructor in Vehicle class to be virtual.

      HTH,
      J.Schafer



      Comment

      • David Rubin

        #4
        Re: Object Initialization in C++

        Venkat wrote:[color=blue]
        >
        > Hello All.
        >
        > I am having a trouble creating a Object in Case Label, and calling the
        > Member Function outside the Switch. Can some one please help me..??[/color]

        [snip][color=blue]
        > switch(number)
        > {
        > case 1:
        > {
        > Chevy o;
        > }[/color]

        What is the scope of o in the above example?

        In any case, a more correct solution has been given.

        /david

        --
        Andre, a simple peasant, had only one thing on his mind as he crept
        along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
        -- unknown

        Comment

        • red floyd

          #5
          Re: Object Initialization in C++

          Josephine Schafer wrote:[color=blue]
          > "Venkat" <junnuthula@yah oo.com> wrote in message
          > news:86545ff.03 10030644.479114 2c@posting.goog le.com...
          >[color=green]
          >>Hello All.[/color][/color]
          [switch statement from hell redacted][color=blue][color=green]
          >>[/color][/color]
          [simple and lucid explanation and solution redacted][color=blue]
          >
          > Don't forget to declare the destructor in Vehicle class to be virtual.
          >[/color]
          He should also look into the Factory pattern.

          Comment

          Working...