Re: Object Initialization in C++

From: Josephine Schafer (no_spam_josschafer_at_hotmail.com)
Date: 10/03/03


Date: Fri, 3 Oct 2003 20:40:34 +0530


"Venkat" <junnuthula@yahoo.com> wrote in message
news:86545ff.0310030644.4791142c@posting.google.com...
> 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.??
>
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



Relevant Pages

  • Re: Vehicular Ramp Access
    ... Current situation is tht I can still get my vehicle on ... communications center and the gate must be opened for me. ... declare the number of passengers would be a simple next step and they could ...
    (rec.aviation.piloting)
  • Re: New to class why am I having sooo much trouble accessing privates
    ... > declare the function in public as ... void test::FormatARAYMember function of class test. ... house is a dead thing. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: creating thread in C++
    ... and you can't declare static member this way. ... OS/2 for instance uses a different calling convention for class members which cannot be changed. ... The work around is to use a friend instead of a static class member function. ... But I am unsure if a static function declaration within an extern "C" block is guaranteed. ...
    (comp.programming.threads)
  • Re: what static fuction mean in c#
    ... >> to acces but in c# i have to but it. ... in both languages you have to declare a member function as static if you want to call that ... > you need an instance to call a nonstatic member function. ... The difference is that in C++ you can declare functions outside of any class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with __cdecl
    ... > If i declare the function as ... you can't pass a member function where a non-member ... these callback mechanisms often provide a means to pass "user ... but the first "void*" parameter may be ...
    (comp.lang.cpp)