A few things you'll have to watch out for, are inheritance, constructors, destructors, assignment operators, etc.
Main Topics
Browse All TopicsLets say I have the following main method that I want to run and when I run it, it produces the following out out:
testing
hope this works
testing
hope this works
oops
transaction
printing 2
printing 0
printing 0
printing 2
printing 0
routing route
changing route
routing route
changing route
routing route
What would the class A and B look like? Can you give me some hints?
changing route
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
1. Derive class B from class A.
2. Use overloaded assignment operator in both the classes.
3. In class A, assignment optr use class B type object as argument.
4. Use copy constuctor in class A.
5. Need to use zero argment constructor and one argument constructor in class B, zero arg constructor in class A .
Subrat2009, part of the idea of an assignment like this is to figure these things out for yourself. The challenge is not to implement it, but to figure out what needs to be implemented - you've just taken away that challenge.
kuntilanak, please try to figure this out for yourself - it's so much more rewarding, and you'll learn so much more from it.
>> if B inherits A
I guess you mean "if B inherits FROM A".
>> and then A has a function print, then does it mean that B must implement print?
No. B also inherits the member functions from A. You can always re-implement it to give it different behavior though.
On the topic of inheritance, please have a look at this basic tutorial :
http://www.cplusplus.com/d
If I can break down the printing, the:
testing
hope this works
testing
hope this works
is printed at the constructor... but how can I print this out if I can only use the method print to print stuffs out.. is it saying that I should call the print method inside the constructor it self? as the print method is the only one that has a '\n'
remember: I can only use one '\n' in my code
>> the reason why I am confused is because the print method doesn't take any arguments..
Maybe you should let it take some arguments ?
Note that you can use default arguments to make sure that it's also possible to call the function without arguments.
Or, maybe you should have another function that does the actual write operation (with the newline character), and that is called by all others.
Or, maybe you can find another solution ;)
It's a free world, and there are plenty of options hehe
I still don't get why B should inherit from A, my only guess is because of the assignment a = b1 and I still haven't figure out..
After thinking of it as well, I think the only place where the '\n' should be placed is at the print method... I still don't get it on how using the print to print something else... all I know is that the print should just do:
"printing x \n"
so how can I use a method that already have that inside the implementation to print something as:
testing
hope this works
You generally do not design classes by looking at the code.
You need to get your requirements straight and then design your classes. Printing a new line can be done in any class depending on your requirements. B should inherit from A only if it can be considered as a specialized substitute for A and can be used in all places where an A can be used.
>>You generally do not design classes by looking at the code.
I generally don't do that, this is the only time where I do it as it's the assignment and I dont' have more choice..
and maybe you're not reading the topic from the start, but I can only use one '/n' in all of my implementation.. don't ask me why
>> so how can I use a method that already have that inside the implementation to print something as:
We already went over that earlier, didn't we ? (http:#25664933)
>> I still don't get why B should inherit from A, my only guess is because of the assignment a = b1 and I still haven't figure out..
I'd say, just try it out the way you think it should be, and see if you can get the same output.
One question and maybe the last before I close this topic down:
The last few parts of the line is:
routing route
changing route
routing route
changing route
routing route
we know this is from the destructor, how am I suppose to know on which destructor is called first? Is the:
changing route
from B
and routing route from
A
Bottom point is that if B inherits from A, then is B's destructor called then A's destructor is called....
oh and with the printing methods, I can't create another method just to print the new line, all I can have in class A and B are only the Big4 and a print method, nothing else.... so how can I make the print method to print several strings with a new line... the print method does't take any arguments as you can see from above.... any idea or hints?
>> so it calls from what it inherits from first the it's own constructor...
We were talking about the destructors, weren't we ?
>> Now I just need to figure out how to use the print(), or otherwise I can't do anything as I need to print something to try it out... and I can only use this print()
Well, several options were mentioned. I'm sure one of them works ;)
Depends if it's a virtual method or not.
But the more important question you should be asking is : which one do you want it to call ?
Btw, I hope you're also trying this out with your compiler to get a better idea of how things work, and how close/far you are from the required result ? Solving it in your head is fine if you can, but at some point, doing it in practice (ie. trying stuff out) is a lot more effective.
I got access to my compiler at least now, and got the following error:
error: new types may not be defined in a return type
note: (perhaps a semicolon is missing after the definition of âAâ)
error: two or more data types in declaration of âAâ
error: return type specification for constructor invalid
and the error is on the code below
After several investigation I found the following:
1. The last 4 prints will print a's print method, so none of the print method needs to be declared as virtual
2. My result does no. 1 correctly, however it got the value wrong... when we do b1 = b2, (b2 has a value of
2 so b1's value should be assigned to 2 as well)... However when we do printit(b1) why is the value
of b1 0 instead of 2?
Business Accounts
Answer for Membership
by: Infinity08Posted on 2009-10-25 at 01:01:49ID: 25655907
Take a look at the code, and figure out which functionalities you need for each of the classes. Then implement those functionalities.
For example, this line :
>> a.print( );
indicates that the A class needs a print method that takes no parameters. You'll have to implement that.
Just do the same for the rest of the code, until it runs :)