![]() |
|
#1
|
|||
|
|||
Inheritance problemHi, for my program, I've created a class named CMaterial which is a base for different materials. The base class only contains a number ID, a name and a type. From that class, I derived two other classes (CMAT1 and CMATS1) which both have their own member variables.
Now, what I'd like to do, is to affect a material to another class CTube. I added a member variable of type CMaterial to the class CTube thinking that I could store the material of any kind (CMAT1 or CMATS1) but it seems that it only stores the members of CMaterial and not the derived one. I read a few tutorials and every example showed me how a derived class can acces members from the base class. I need to do the inverse, can it be done?? Another problem I have (I think it's the same) is that I'd like to output CMAT1 and CMATS1 data into a text file using operator<< overload from my CMaterial class so I could output either format. Here's the class definitions: CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
|
I think you are a little misguided, CaptainB. If you have a base-class A, that is inherited by classes B and C, then it is natural for B and C to have access to anything in base-class A. This link exists due to the inheritance of the base-class by B and C. However, if you instantiate class A alone, it will have no access whatsoever to the functions locked inside B and C. There is no way to overcome this. Let's look at this from a genetic perspective.
My father had great metabolism, but couldn't talk his way out of a paper pag when it came to an argument. When I was born, I naturally inherited his great metabolism, but I also inherited my mom's ability to speak confidently in a confrontation. Unfortunately for my father, the parent cannot access the skills that the child is born with unless it was the parent that gave those skills. If my dad couldn't stand up in a debate when I was born, he still couldn't after I was born, even though I was able to take his great metabolism. Class inheritance works the same way. If a base-class is inherited by a derived class, the derived class has access to any of those inherited functions, but the base-class alone cannot see any part of the class that were derived from it, nor is there a way to tell if any classes at all have been derived from it. So, if you would like the parent to have a skill in common with the child, then the parent's got to be born with it. // Upon looking at your code, it is my suggestion that you come up with more meaningful identifiers. __________________
-Aaron |
|
#3
|
|||
|
|||
|
I finally found out how to do what I wanted but I'm not sure if it's the more efficient way to do it. I've added virtual functions in my base class that, in my derived classes, return material properties. It works fine but if I have 10 different materials type with 10 variables each, I would need to add 100 virtual functions in my base class. Instead of all that, I could have created only one class with the 100 members, I would get the same result.
I'm sure there's a way to do what I want, I mean it's quite simple. A class representing a mechanical part with a member representing the part's material. Material can be of many types. If you have any suggestions, I'm all ears... CaptnB |
Recent GIDBlog
Match IP in CIDR by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Css design problem | JUNK KED | Web Design Forum | 4 | 25-Jan-2005 17:45 |
| File Serialize's problem | stanely | MS Visual C++ / MFC Forum | 2 | 08-Dec-2004 09:54 |
| Tee chart problem | Arun | C++ Forum | 0 | 01-Sep-2004 23:23 |
| Another FX 5600 problem (but with details that might shed light on this) | BobDaDuck | Computer Hardware Forum | 2 | 16-Apr-2004 07:53 |
| unwanted scrollbar problem | kelly001 | Web Design Forum | 3 | 24-Oct-2003 10:44 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The