473,513 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 19 '05 #1
4 3159
On 3 Oct 2003 07:44:30 -0700, ju********@yahoo.com (Venkat) wrote:
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
Jul 19 '05 #2

"Venkat" <ju********@yahoo.com> wrote in message
news:86*************************@posting.google.co m...
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

Jul 19 '05 #3
Venkat wrote:

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

--
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
Jul 19 '05 #4
Josephine Schafer wrote:
"Venkat" <ju********@yahoo.com> wrote in message
news:86*************************@posting.google.co m...
Hello All. [switch statement from hell redacted]
[simple and lucid explanation and solution redacted]
Don't forget to declare the destructor in Vehicle class to be virtual.

He should also look into the Factory pattern.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

106
5472
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
8
1760
by: nrhayyal | last post by:
hi all, is it a good idea to assign object to pointers without initialising it to NULL? suppose if i have a class TEST class ELEMENT { private: int a; string s1;
8
1812
by: Mark Neilson | last post by:
1. What is the best way to make a single instance of my top level class (DLL) internally available to all other members of the assembly? The top level object is where all other access is made in...
7
1655
by: xllx.relient.xllx | last post by:
Q1: What happens when you apply parenthesis to the class type name? Is the constructor for the class called and returns an object?, or what? in main...: MyClass(); // end
4
1546
by: sks | last post by:
hi , i Have a code snippet as follows class ABC { int &r; ABC(int a=0): r(a) {} }; int main() {
13
2045
by: Frederick Gotham | last post by:
I have just been reading 8.5 in the Standard, and am trying to make sense of the different kinds of initialisations. Up until now, I thought of an object as either NOT being initialised (i.e....
8
399
by: sarathy | last post by:
Hi, I read the following points in K&R "Section A8.7 Initialization". Seems like i have a problem with them. * All expressions in the initialization of constant object/array must be constant...
4
3693
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
13
2308
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
5
399
by: kiryazev | last post by:
Hello. Given the code below does C++ Standard guarantee that the function my_init() will be called before main()? struct A { A() { my_init(); } };
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7162
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7527
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3223
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.