Thread: i just dont get it !

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    17

    i just dont get it !

    ok this is for an object oriented programming class.

    i have 5 files
    Main.cpp (which i cant modify)
    computer.cpp (implementation of computer.h)
    computer.h (interface for computer.cpp)
    memory.cpp(implementation of memory.h)
    memory.h (interface for memory.cpp)


    Code:
    //main.cpp
    #include <iostream>
    using namespace std;
    #include "Computer.h"
    
    	The output from the program is shown below:
    	comp1: Computer has 256 MB memory
    	comp1: Computer has 512 MB memory
    	comp1: Computer has 512 MB memory
    	comp2: Computer has 512 MB memory
    	comp1: Computer has 512 MB memory
    	comp2: Computer has 1024 MB memory
    */
    
    int main(int argc, char* args[])
    {
    	Computer comp1(new Memory(256));
    	cout << "comp1: " << comp1.get() << endl;
    	comp1.changeMemory(new Memory(512));
    	cout << "comp1: " << comp1.get() << endl;
    	Computer comp2 = comp1;
    	cout << "comp1: " << comp1.get() << endl;
    	cout << "comp2: " << comp2.get() << endl;
    	comp2.changeMemory(new Memory(1024));
    	cout << "comp1: " << comp1.get() << endl;
    	cout << "comp2: " << comp2.get() << endl;
    	return 0;
    }
    Code:
    //computer.h
    #include "Memory.h"
    
    class Computer
    {
    public:
    	Computer(Memory* newMem);
    	~Computer();
                    char* get(void);
    	void changeMemory(Memory* newMem);
    	int theMem;
    
    };
    Code:
    //computer.cpp
    #include "Computer.h"
    #include <iostream>
    
    
    using namespace std;
    
    
    Computer::Computer(Memory* newMem){
    
    }
    
    Computer::~Computer(){
    }
    
    char* Computer::get(void){
    }
    
    void Computer::changeMemory(Memory* newMem){
    }
    Code:
    //Memory.h
    class Memory
    {
    public:
    
    	Memory(const int& newMem);
    	~Memory();
    	int& memVar;
    };
    Code:
    //Memory.cpp
    #include "Memory.h"
    #include <iostream>
    
    using namespace std;
    
    Memory::Memory(const int& newMem){
    	cout << newVar << endl;
    }
    
    
    Memory::~Memory(){
    }
    i cant seem to implement the code. the interface is ok, it works. but i cant implement it and its driving me crazy ive tried everything and then finally deleted the implementation and tried to start over, and cant figure this out. i know i need a delete in the destructor. but i cant make it print the right information, ive gotten it to print some wierd negative number, a bunch of zeros and then finally the number... but the number was out of place.

    any help is apreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why doesn't your constructor set the memory? You pass it over, but you don't do anything with it other than print it out. Shouldn't you be setting the value of 'memVar' to something? Also, why is that a reference? It shouldn't be. Why are you even using pointers or references at all?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    i cant change main.cpp... thats the assignment! unless there is some way to make the comp1(new Memory(256)); into a reference but as i recall that returns a pointer. i got confused and deleted alot of my crappy code, cuz it was useless and didnt work and memVar was just a piece i missed. i couldnt figure out how to transfer the pointer from memory's interface to computer's interface.

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    /* deleted for some reason */
    Last edited by Dae; 06-20-2005 at 02:57 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hey Dae,

    What did you think these meant?

    ok this is for an object oriented programming class.
    i cant change main.cpp... thats the assignment!
    You should delete your post.
    Last edited by 7stud; 06-20-2005 at 01:18 PM.

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by 7stud
    Hey Dae,

    What did you think these meant?





    You should delete your post.
    Sup 7stud,

    Should I just give him hints instead then?

    I did it because I wanted to figure it out too, just figured I'd post the solution since it was just there.. which isnt exactly mean.

    Hint #1: your passing the number 256 (etc, which isnt attached to a variable) from main so not everything should be a pointer (aka the first variable to recieve 256). That would be fun though, passing the memory location 5 times through the program but you have to save it somewhere (for various reasons), which the get() method would return.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Should I just give him hints instead then?
    Yes. Instead of posting a solution with no explanation, instead try posting an explanation. You can always post some general code to help clarify your explanation. The poster will get more out of an explanation, and they will learn more arriving at the specific solution themselves.

    I did it because I wanted to figure it out too
    There's no reason why you can't do that, just don't post solutions to people's homework--it's not fair to the other people in the class. If you want to, work out the solution yourself so you know what the issues are, then post an explanation of the issues.

    Do you think quzah couldn't come up with the code?
    Last edited by 7stud; 06-20-2005 at 04:40 PM.

  8. #8
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by 7stud
    Yes. Instead of posting a solution with no explanation, instead try posting an explanation. You can always post some general code to help clarify your explanation. The poster will get more out of an explanation, and they will learn more arriving at the specific solution themselves.


    There's no reason why you can't do that, just don't post solutions to people's homework--it's not fair to the other people in the class. If you want to, work out the solution yourself so you know what the issues are, then post an explanation of the issues.

    Do you think quzah couldn't come up with the code?
    Of course he could, and I'm sure you could too, but I actually had the code laying there because I wanted to do it.. so why not post it anyway.

    I see your point at any rate, but you know what I personally get more out of seeing the code than general explanation.

    Both are good though, its just giving the code could be misused by the person not even taking the time to find his faults I see that point, and of course its not fair to others in his class. However your opinion is .. your opinion

    I had the code there because I actually did it for myself, so I prefered to just give the code instead of giving an explanation since that would be extra work and it was late.

    Also, I'm new to helping people with their code so give me a break and say "it might be better not to hand the assignment to him" instead of assuming I know some assignment guideline and asking if I read his post.
    Last edited by Dae; 06-20-2005 at 05:03 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    However your opinion is .. your opinion
    Actually, it's more than my opinion--cboard policy does not allow posting homework solutions.

    instead of assuming I know some assignment guideline and asking if I read his post.
    You should read the posting guidelines before posting on any forum.
    Last edited by 7stud; 06-20-2005 at 05:22 PM.

  10. #10
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by 7stud
    Actually, it's more than my opinion--cboard policy does not allow posting homework solutions.
    No.. cboard policy is to not ASK for homework solutions. I can help people in any way I want.

    I did his assignment for myself and if I didnt post the solution then I would have not posted anything at all. I just posted it to be nice because it was sitting there and some people arent going to misuse it and are sincerely trying to learn from the solution.

    But please do not ask people to do your entire homework for you, it simply annoys people most of the time.
    It did not annoy me to post the solution so I did it.

    Most people are guided by common sense, and you should read the posting guidelines before posting on any forum.
    I know its common sense not to do other peoples homework for them intentionally, but I was doing it for myself and was nice enough to share my outcome instead of just not posting anything.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Stop arguing or I'll close the thread. I wouldn't have called it "official" CBoard policy not to give complete solutions, but I would call it "unofficial" CBoard policy, especially when the OP is clearly capable of more than you give him credit for.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If it's any consolation to you, the initial main.cpp is very stupid and requires things that probably go far beyond what you've learned in order to work properly and reliably. Namely smart pointers. (There is just no way to guarantee that memory is not leaked otherwise AND keep the interface of the classes easy to understand.)
    Then there's the little issue that the current main.cpp cannot use smart pointers the way they're usually written.

    Very, VERY peculiar.

    Is Computer.h also predefined? Have you learned std::string yet?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed