Object Initialization in C++

V

Venkat

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
 
T

tom_usenet

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..??

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

What about Chevy, etc.? Do they all derive from Auto?
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;

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;
}
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.??

Using polymorphism. Auto should look like this:

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

And the others should override Description.

Tom
 
J

Josephine Schafer

Venkat said:
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
 
D

David Rubin

Venkat said:
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..??
[snip]
switch(number)
{
case 1:
{
Chevy o;
}

What is the scope of o in the above example?

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

/david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top