473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ineritance: Values of parent/base class

Hi guys

Posted what was probably a rather confusing post about this the other
day, so I am going to have another go and see if I can make more sense.
This sis purely a

I've got a base class called animal, and from within animal you can
access lots more classes such as feline, canine, reptile and
amphibian.....

To access canine, you are going to go through animal. Animal has lots
of public properties such as height and weight that we can access from
our aspx.

So first of all I want to do animal.configur e(iHeight, iWeight).....

Inside canine, I would like to be able to access the properties of the
animal object, so i can find out how big it is, how tall it is etc etc
- does that make sense?

I can do the canine canine = new canine(); bit so that canine is a
child of animal - but I can't get animal to access the properties of
animal.

Can you help?? Awaiting you is my eternal gratitude ;)

Many thanks

Darren

May 17 '06 #1
11 2871
*** SORRY! ***

In correction to the above, the next to last paragraph should read:

I can do the canine canine = new canine(); bit so that canine is a
child of animal - but I can't get CANINE to access the properties of
animal.

Thanks

Daz

May 17 '06 #2
Can you post some code ???

Do you dervie Canine from Animal ?

class Canine : Animal
{

}
Steph.
<Da************ **@gmail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
Hi guys

Posted what was probably a rather confusing post about this the other
day, so I am going to have another go and see if I can make more sense.
This sis purely a

I've got a base class called animal, and from within animal you can
access lots more classes such as feline, canine, reptile and
amphibian.....

To access canine, you are going to go through animal. Animal has lots
of public properties such as height and weight that we can access from
our aspx.

So first of all I want to do animal.configur e(iHeight, iWeight).....

Inside canine, I would like to be able to access the properties of the
animal object, so i can find out how big it is, how tall it is etc etc
- does that make sense?

I can do the canine canine = new canine(); bit so that canine is a
child of animal - but I can't get animal to access the properties of
animal.

Can you help?? Awaiting you is my eternal gratitude ;)

Many thanks

Darren

May 17 '06 #3
Hi,

<Da************ **@gmail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
Hi guys
I've got a base class called animal, and from within animal you can
access lots more classes such as feline, canine, reptile and
amphibian.....
You do not access them, you derive Animal in all those other classes
To access canine, you are going to go through animal.
What you mean with this?

Inside canine, I would like to be able to access the properties of the
animal object, so i can find out how big it is, how tall it is etc etc
- does that make sense? Yes.
You will be able, unless you declare them as private

I can do the canine canine = new canine(); bit so that canine is a
child of animal - but I can't get animal to access the properties of
animal. Can you help?? Awaiting you is my eternal gratitude ;)


Post a piece of code whith what the problem is and the error you are getting
May 17 '06 #4
As I said, post the code
<Da************ **@gmail.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
*** SORRY! ***

In correction to the above, the next to last paragraph should read:

I can do the canine canine = new canine(); bit so that canine is a
child of animal - but I can't get CANINE to access the properties of
animal.

Thanks

Daz

May 17 '06 #5
This is definitely the correct approach. Canine should be derived from
Animal. Its not a child of Animal, its a specialization of Animal.

So if...

public class Animal
{
public int weight;
public int height;
}

public class Canine : Animal
{
public int teeth;
}

Canine dog = new Canine();
Console.WriteLi ne(dog.weight);

// polymorphism in effect...
Animal animal = new Canine();
Canine dog2 = (Canine)animal;
"TheSteph" wrote:
Can you post some code ???

Do you dervie Canine from Animal ?

class Canine : Animal
{

}
Steph.
<Da************ **@gmail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
Hi guys

Posted what was probably a rather confusing post about this the other
day, so I am going to have another go and see if I can make more sense.
This sis purely a

I've got a base class called animal, and from within animal you can
access lots more classes such as feline, canine, reptile and
amphibian.....

To access canine, you are going to go through animal. Animal has lots
of public properties such as height and weight that we can access from
our aspx.

So first of all I want to do animal.configur e(iHeight, iWeight).....

Inside canine, I would like to be able to access the properties of the
animal object, so i can find out how big it is, how tall it is etc etc
- does that make sense?

I can do the canine canine = new canine(); bit so that canine is a
child of animal - but I can't get animal to access the properties of
animal.

Can you help?? Awaiting you is my eternal gratitude ;)

Many thanks

Darren


May 17 '06 #6
Yes Steph

Basically, Canine belongs to Animal.

So, inside Canine, I want to be able to access properties of Animal.

For example:

class Animal
{
// Declare the child object (I presume this is correct)
Canine Canine = new Canine();

public string sName = "Rex";
}

Class Canine
{
Public String myFunc()
{
return Animal.Name + " is our returned value"
}

}

Thanks very much for your help.

Darren

May 18 '06 #7
Hi Ignacio

Thanks for trying to help!

I have posted a code snipped in reply to Stephs reply - please could
you take a look at it there?

Many many thanks.

Darren :)

May 18 '06 #8
What you are trying to do is presumably inheritance, not encapsulation; to
give a more complete example:

Marc

======

using System;

static class Program {
static void Main() {
Canine rex = new Canine();
rex.Name = "Rex";
Console.WriteLi ne("Rex speaking: " + rex.Speak()); // use the method
inherited from all animals
Console.WriteLi ne("Rex barking: " + rex.Bark()); // use the
canine-specific method
Animal animal = rex; // note that this does *NOT* create a new
object -
// we are still looking at the same rex, but just using the generic
animal API
Console.WriteLi ne("Animal speaking: " + animal.Speak()) ;
Console.ReadLin e();
}
}

abstract class Animal { // abstract: doesn't declare a concrete (creatable)
type - but only a base class
private string _name;
public string Name {get {return _name;} set {_name = value;}}
public abstract string Speak(); // force all Animal types to provide
this
}

class Canine : Animal {
public string Bark() { // this method is specific to Canine objects
return Name + " says \"Woof\"";
}
public override string Speak() {
return Bark();
}
}

May 18 '06 #9
Hi Marc, and to everyone else.

Well, after a good hour and a half of brushing up on OOP with Pro C#
2005 and the .NET 2.0 Platform (Apress) that I bought, I have got it.

What I want to do is set up my child class (canine) as Canine:Animal.

Then, I can access the protected properties of animal from within
canine by doing this.AnimalProp erty.

Now I know that, even I think I sounded stupid before ;)

Many thanks for all your help - it is appreciated :)

May 18 '06 #10

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

Similar topics

3
4799
by: RR | last post by:
I have two classes: class Base { function Insert() { // does stuff } }
2
3836
by: Mattias B | last post by:
Hello! I have s quick question regarding multiple ineritance that I hope that someone will have the time to look at. I have the following classes that I try to compile and use: class A { public: virtual void foo() = 0;
9
2078
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
4
4567
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as adding to the child collection, eg: parent.Children.Add(child); child.Parent = parent;
12
1979
by: Peter Cranz | last post by:
hello, I've got the following problem: I have a construct similar like this: namespace A { class X {
0
2829
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
3
3326
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this class hierarchy but only objects of some of these types can be a parent. Example: class CFruit : public CBase { }; class CBranch : public CBase { };
14
1579
by: Jo | last post by:
Hi, Is there a generic way to access the (virtual) functions in the base class of a class? Like in: if (!this->Foo(something)) return(((Base*)this)->Foo(something)); // for a normal member function if (!this->Foo(something)) return(this->Base::Foo(something)); // for
61
2958
by: Sanders Kaufman | last post by:
I'm wondering if I'm doing this right, as far as using another class object as a PHP class property. class my_baseclass { var $Database; var $ErrorMessage; var $TableName; var $RecordSet; function my_baseclass(){ $this->TableName = "";
0
8706
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9311
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7947
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6632
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5946
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4719
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2518
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.