473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I instantiate an object of a derived class using the parent class?

110 100+
I'm trying to instantiate an object of an inherited class "Chimp" from the parent class "Animal". The Animal class has a constructor that allows for an int legcount, string skintype, string talk, and a string eats. I have several objects instantiated as:
Expand|Select|Wrap|Line Numbers
  1. Squirrel squeeky = new Squirrel(FOUR_LEGS, "String1", "String2", "String3");
  2. Chicken cluck = new Chicken(TWO_LEGS, "String1", "String2", "String3");
  3.  
I am trying to instantiate my Chimp as an Animal though and have something like:
Expand|Select|Wrap|Line Numbers
  1. Chimp bubbles = new Animal(FOUR_LEGS, "String1", "String2", "String3");
  2.  
But that is wrong. It returns an error saying it can't implicitly convert Animal to Chimp. When I set it as:
Expand|Select|Wrap|Line Numbers
  1. Animal bubbles = new Chimp(FOUR_LEGS, "String1", "String2", "String3");
  2.  
The instantiation works but my Chimp.Unique() Method doesn't work (a method within the chimp class unique to the chimp class).

Can someone please explain if both of those are wrong or how I do it using one or the other but can still reference the unique methods in the class?

Thanks in advance.
Oct 7 '10 #1
4 2340
Sfreak
64
When you instantiate a class, all parents are instantiate together. To access an attribute of a parent use the BASE statement to get access of it.
Oct 7 '10 #2
Fuzz13
110 100+
I understand that but I'm being asked to instantiate it in the specific way I'm having trouble doing. Can you help with that?
Oct 7 '10 #3
GaryTexmo
1,501 Expert 1GB
I think we'd need to see your inheritance structure... can you post a class diagram possibly? If Chimp inherits from Animal, you should be able to assign a new Animal to a Chimp object.

A class diagram or some sample code so I can see what you're doing would help.
Oct 7 '10 #4
Fuzz13
110 100+
Thank you for your replies. What I ended up needing was:
Expand|Select|Wrap|Line Numbers
  1. Animal bubbles = new Chimp(FOUR_LEGS, "String1", "String2", "String3");
  2.  
The problem it was giving me that I didn't understand was that it broke the connection with my method

bubbles.dosomethingunique();

As it turns out since I instantiate it as a member of the parent class, it had the attributes of that class and not of the Chimp class like it would have had if I instantiated it as:
Expand|Select|Wrap|Line Numbers
  1. Chimp bubbles = new Chimp(FOUR_LEGS, "String1", "String2", "String3");
  2.  
Anyways that error was giving me serious confusion until it was explained to me. Thank you for your assistance though guys.
Oct 8 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: machine | last post by:
I'm trying to develop a VB.NET web app that incorporates the Citrix WI3.0 components. The problem is that there is a class named "TemplateParser" in the "com.citrix.nfuse" namespace, but there is...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
6
by: jalkadir | last post by:
Let's say that I have this class: class Parent{ private: char* str; public: const char* getStr(){return str;} }; And then I create a child class class Child{ private: std::string str;...
6
by: marco_segurini | last post by:
Hi, the following sample code shows a compiler error I get trying to build some old code with the last CL compiler (vers 13.10.3077): //----- begin #include <iostream> namespace ns {
1
by: - vhannak | last post by:
I have a class (sharedClass) that is instantiated (not derived) by two other classes (guiClassA and guiClassB). The sharedClass needs to be able to call a method that is defined in its parent...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
15
by: Anthony Greene | last post by:
This is probably a very introductory object-oriented question, but it has been nagging me for years, and since I've never been able to find the right answer, I've had to work around it with...
3
by: jimhce | last post by:
Hi, How can a child class passes its non-static member function to the parent class? class A { public: typedef void *(*object_t)(void *); A(object_t f) : func(f) {} virtual ~A(){}...
5
by: devgupta01 | last post by:
One.java ------------ public class One{ int x; char y; One(){x=0;y='a';} public void disp(){System.out.println("x ="+x+", y ="+y);} }
7
by: Tarscher | last post by:
Hi all, I have classes: Unit, Transporter, Bus and Car Transporter inherits from Unit and Bus and Car inherit from Transporter. I want to be able to do Bus bus = new Bus() if (bus is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.