473,387 Members | 1,388 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,387 software developers and data experts.

Inheritance Problems

Hey,

I'm kind of new to C++ and having some problems with inheritance. I've tried looking everywhere for some help, but I'm not too sure what to ask so please any input will be greatly appreciated. (Please let me no if I start talking rubbish too)

Basically I have my base class with 3 functions and two global integers. I also have a derived class which overrides two of the base class functions.

When I create the derived class object, I want it to set the first integer, then run the base class function that isn't overridden, which will set the second integer based on the first integer, then the other overridden function will be able to use both the integers' new values. I'm not too good at explaining so ill try demonstrate.

Expand|Select|Wrap|Line Numbers
  1. class base
  2. {
  3.    int nA = 0, nB = 0;
  4.    funcA(void);
  5.    funcB(void);
  6.    funcC(void);
  7. };
  8.  
  9. base::funcA() { return nA + nB; }
  10. base::funcB() { nA = 1; }
  11. base::funcC() {nB = 1; }
  12.  
  13. class derived
  14. {
  15.    funcB(void);
  16.    funcC(void);
  17. };
  18.  
  19. derived::funcB() { nA = 5; nB = 5; }
  20. derived::funcC() { return funcA() * 2; }
  21.  
  22. int main()
  23. {
  24.    derived dClass;                //create object
  25.    dClass.funcB();                //run function to set integers
  26.    cout << dClass.funcC();    //do the sums and output
  27. }
Basically will this output 20? if not, how can I get it to?

I think my problem is that the base class will try do 0+0.

If no one can help ill try rephrase it, cheers any ways though.
Apr 12 '07 #1
3 1440
Ganon11
3,652 Expert 2GB
As it stands, I'm pretty sure this program won't compile. The integers nA and nB are not given access specifiers in base - that is, you don't explicitly say whether they are private, public, or protected. When you don't explicitly say public, etc. it is assumed that the members are private (when using a class). That means that only base can see nA and nB - derived knows nothing about them. When you type nA = 5; nB = 5; in derived::funcB(), you will run into errors.

I don't know how accurate this example is to your actual problem, but this could be something that will affect it. Also, you should look up virtual functions for overriding base class functions - it may not be necessary, but the learning is worth it.
Apr 12 '07 #2
Thanks for the quick reply. They are all public. I basically want an integer specified in the base class that both classes can access. So like a function in the base class might set it to 1, then a function in the derived class may set it to 5, then a function in the base class may read it, perform a calculation and set it to 3 etc..
Apr 12 '07 #3
Ganon11
3,652 Expert 2GB
If they are all public, then I can't think of any problem you will have. Since a derived is-a base, it will have its own nA and nB (or maybe just have access to base::nA and base::nB). You may want to make nA and nB protected, however - it still lets derived access and change them, but it prevents other classes from accessing them directly, too.
Apr 12 '07 #4

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

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
12
by: jinal jhaveri | last post by:
Hi All, I have one question regarding circular inheritance I have 3 files 1) A.py , having module A and some other modules 2) B.py having module B and some other modules 3) C.py having...
7
by: Hung Jung Lu | last post by:
Hi, I think Microsoft did look into Python when they designed C#. (E.g. they got rid of checked exceptions of Java.) However, they followed Java in avoiding multiple inheritance (MI), which is a...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
15
by: Sinex | last post by:
Hi, Why does C# disallow multiple inheritance? Whats the reason behind this? Is there any advantage or is it just a method to avoid some problems (if so, what problems?) that come with multiple...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
6
by: Pascal Polleunus | last post by:
Hi, I'm wondering if there could be problems related to inheritance in the following scenario (with PostgreSQL 7.4.1)... 1 A-table, abstract. Max 10 B-tables that inherit from A, with...
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
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,...

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.