473,403 Members | 2,338 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,403 software developers and data experts.

Inheritance and base ???

Is this construction possible????

class MyClass1
{
....
}

class MyClass2 : MyClass1
{
....
}

..... somewhere in the code:

MyClass1 myClass1 = new MyClass1()
MyClass2 myClass2 = (MyClass2) myClass1;

Can I simply convert base class into inherited class???
Or how to pass initialized base class into new inherited_class ????

Thanks

Mike
Nov 15 '05 #1
3 1244

"ne**@mikendy.com" <ne**@endys.cz> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
Is this construction possible????

class MyClass1
{
...
}

class MyClass2 : MyClass1
{
...
}

.... somewhere in the code:

MyClass1 myClass1 = new MyClass1()
MyClass2 myClass2 = (MyClass2) myClass1;

Can I simply convert base class into inherited class???
Or how to pass initialized base class into new inherited_class ????

Thanks

Mike


No, this direction is not possible.
Say MyClass1 == Mammal and MyClass2 == Dog
then you can treat a Dog as a (more general) Mammal,
but you can't just treat any Mammal as a Dog

Hans Kesting
Nov 15 '05 #2

NO, you can not cast a class to an inherited class because the parent class
don't know the design of the inherited class. What you can do is this:

MyClass1 myClass1 = new MyClass2();
MyClass2 myClass2 = myClass1 as MyClass2;

Hope this helps.
"ne**@mikendy.com" <ne**@endys.cz> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
Is this construction possible????

class MyClass1
{
...
}

class MyClass2 : MyClass1
{
...
}

.... somewhere in the code:

MyClass1 myClass1 = new MyClass1()
MyClass2 myClass2 = (MyClass2) myClass1;

Can I simply convert base class into inherited class???
Or how to pass initialized base class into new inherited_class ????

Thanks

Mike

Nov 15 '05 #3
I am not quite sure what you want to do, but maybe this ?

class MyClass1
{
public string s_Text = "This is MyClass1";
...
}

class MyClass2 : MyClass1
{
MyClass1 mc_bla=null;
public MyClass2(MyClass1 bla)
{
mc_bla = bla;
// at this point s_Text is : This is MyClass1
s_Text = "This is MyClass2";
// now changed
}
public OnDoSomething()
{
MessageBox.Show("Class1("+mc_bla.s_Text+") ; Class2("+s_Text+")";
}

}

MyClass1 myClass1 = new MyClass1()
MyClass2 myClass2 = new MyClass2(myClass1);
myClass1.s_Text = "Changed Text in Class1";
myClass2.OnDoSomething();

Result should be : "Class1(Changed Text in Class1) ; Class2(This is
MyClass2)"

Mark Johnson, Berlin Germany
mj*****@mj10777.de

"ne**@mikendy.com" <ne**@endys.cz> schrieb im Newsbeitrag
news:uH**************@TK2MSFTNGP11.phx.gbl...
Is this construction possible????

class MyClass1
{
...
}

class MyClass2 : MyClass1
{
...
}

.... somewhere in the code:

MyClass1 myClass1 = new MyClass1()
MyClass2 myClass2 = (MyClass2) myClass1;

Can I simply convert base class into inherited class???
Or how to pass initialized base class into new inherited_class ????

Thanks

Mike

Nov 15 '05 #4

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

Similar topics

4
by: Dan Bullok | last post by:
I have a couple of classes: class Base: ... class Sub(Base): ... I want to subclass Base and Sub, i.e. define classes: class MyBase(Base):
0
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
7
by: Mahesh | last post by:
Hello, I am having a query about inheritance. I have a virtual base class that is derived by to other classes that are hirarchially at the same level. But I still need to ensure that only a...
5
by: ma740988 | last post by:
Prefer composition to inheritance (can't recall which text I stole that line from) is one of the fundamental tenets thats engrained in my mind. Having said that inheritance requires careful...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
1
by: relient | last post by:
Hi, I just started the chapter on "Inheritance" and I'm a bit confused so I have three questions: first, here's the code: using System; using System.Collections.Generic; using System.Text; ...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
12
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.