Re: Object Initialization in C++
From: Josephine Schafer (no_spam_josschafer_at_hotmail.com)
Date: 10/03/03
- Next message: Michiel Salters: "Re: Deleting Pointers from STL Vector"
- Previous message: tom_usenet: "Re: Object Initialization in C++"
- In reply to: Venkat: "Object Initialization in C++"
- Next in thread: red floyd: "Re: Object Initialization in C++"
- Reply: red floyd: "Re: Object Initialization in C++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Michiel Salters: "Re: Deleting Pointers from STL Vector"
- Previous message: tom_usenet: "Re: Object Initialization in C++"
- In reply to: Venkat: "Object Initialization in C++"
- Next in thread: red floyd: "Re: Object Initialization in C++"
- Reply: red floyd: "Re: Object Initialization in C++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|