Thread: Making a new method

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    Making a new method

    Hello, programmers. I`ve made a code in C++, done some methods, but I cant get past this one. I need to make a method that shows the MaxRAM value for example the methods name would be GetMaxRAM.

    Bare in mind, GetAtm and SetAtm are methods for RAM therefore, Getting the RAM value and setting it.

    How could i make a method in ComputerNetwork class that gives me MAX Ram value at the end of the program?


    The code is here:

    Code:
    #include <iostream.h>#include <conio.h>
    #include <stdio.h>
    class OverflowException {
       public:
          OverflowException() {
                cout << endl << "Exception created!" << endl;
          }
          OverflowException(OverflowException&) {
             cout << "Exception copied!" << endl;
            }
          ~OverflowException() {
             cout << "Exception finished!" << endl;
          }
    };
    class Computer {
        protected:
            char* CPU;
            float freq;
            short RAM;
        public:
            Computer();
            Computer(char*, float, short);
            virtual ~Computer() {
                cout << "Message from the \"Computer\" - destroyed!" << endl;
            }
            char* GetProcesors() const {
                return CPU;
            }
            void SetProcesors(char* CPU) {
                this->CPU = CPU;
            }
            float GetFrekv() const;
            void SetFrekv(float);
            short GetAtm() const;
            void SetAtm(short);
    
    
            virtual void Print() const;
    };
    
    
    class NetComputer : public Computer {
        private:
            char* IP_adrese;
        public:
            NetComputer():Computer(), IP_adrese ("81.94.532.142") {
            }
            NetComputer(char*, float, short, char*);
            virtual ~NetComputer() {
                cout << endl << "Message from the \"NetComputer\" - destroyed!" << endl;
            }
    
    
            char* GetIP_adrese() const {
                return IP_adrese;
            }
            void SetIP_adrese(char* IP_adrese) {
                this->IP_adrese = IP_adrese;
            }
    
    
            virtual void Print() const;
    };
    class ComputerNetwork {
       private:
            typedef NetComputer* CPointer;
            CPointer *Nodes;
            static const unsigned int DEF_MAX_LENGTH;
          unsigned int MaxLength;
            unsigned int Length;
       public:
            ComputerNetwork() : MaxLength(DEF_MAX_LENGTH), Length(0) {
                Nodes = new CPointer[MaxLength];
          }
            ComputerNetwork(unsigned int MaxLength) : MaxLength(MaxLength), Length(0) {
                Nodes = new CPointer[MaxLength];
          }
            ~ComputerNetwork();
            static unsigned int GetDefaultMaxLength() {
             return DEF_MAX_LENGTH;
          }
            int GetMaxLength() const {
             return MaxLength;
          }
          int GetLength() const {
             return Length;
            }
            void AddNode(const NetComputer&);
            void Print() const;
    };
    const unsigned int ComputerNetwork::DEF_MAX_LENGTH = 20;
    Computer::Computer() : CPU("AMD"), freq(2.2), RAM(256) {
    }
    
    
    Computer::Computer(char* PCPU, float Pfreq, short PRAM) : CPU(PCPU) {
        freq = Pfreq;
        RAM = PRAM;
    }
    inline float Computer::GetFrekv() const {
        return freq;
    }
    inline void Computer::SetFrekv(float freq) {
        this->freq = freq;
    }
    inline short Computer::GetAtm() const {
        return RAM;
    }
    inline void Computer::SetAtm(short RAM) {
        this->RAM = RAM;
    }
    inline void Computer::Print() const {
        cout << "CPU = " << CPU << ", Frekvence = " << freq <<", RAM= " << RAM;
    }
    
    
    NetComputer::NetComputer(char* PCPU, float Pfreq, short PRAM, char* PIP_adrese) : Computer(PCPU, Pfreq, PRAM) {
        IP_adrese = PIP_adrese;
    }
    
    
    inline void NetComputer::Print() const {
        Computer::Print();
        cout << ", IP_adrese = " << IP_adrese;
    }
    ComputerNetwork::~ComputerNetwork() {
       for(unsigned int i=0; i<Length; i++)
          delete Nodes[i];
       delete [] Nodes;
    }
    void ComputerNetwork::Print() const {
        cout <<  "." << endl
          << "Line's nodes:" << endl;
       for (unsigned int i=0; i<Length; i++) {
          cout << (i+1) << ". ";
            Nodes[i]->Print();
            cout << "." << endl;
            }
    }
    void ComputerNetwork::AddNode(const NetComputer& Node) {
       if (Length == MaxLength)
          throw OverflowException();
       else 
            Nodes[Length++] = new NetComputer(
                Node.GetProcesors(), Node.GetFrekv(), Node.GetAtm(), Node.GetIP_adrese()
          );
    }
    
    
    void main(void) {
        ComputerNetwork *Line = new ComputerNetwork(2);
        NetComputer *C1 = new NetComputer("AMD", 3.2, 4, "81.42.151.242");
        NetComputer  C2("Intel", 2.8, 8, "81.42.451.234");
    
    
       try {
            Line->AddNode(*C1);
          cout << "\nNew node added successfully!" << endl;
       }
          catch (OverflowException&) {
             cout << "*** Error: maximal length exceeded ! ***" << endl;
          }
          catch (...) {
             cout << "Unknown Error !" << endl;
          }
            delete C1;
    
    
       cout << "\n\nDefault maximal length (from CLASS): " << 
            ComputerNetwork::GetDefaultMaxLength() << "." << endl;
       cout << "Default maximal length (from OBJECT): " << 
            Line->GetDefaultMaxLength() << "." << endl;
       cout << "Maximal length: " << Line->GetMaxLength() << "." << endl;
        cout << "Current length: " << Line->GetLength() << "." << endl;
    
    
       try {
            Line->AddNode(C2);
          cout << "\nNew node added successfully!" << endl;
       }
          catch (OverflowException&) {
             cout << "*** Error: maximal length exceeded ! ***" << endl;
          }
          catch (...) {
             cout << "Unknown Error !" << endl;
          }
    
    
       try {
            Line->AddNode(C2);
          cout << "\nNew node added successfully!" << endl;
       }
          catch (OverflowException&) {
             cout << "*** Error: maximal length exceeded ! ***" << endl;
          }
          catch (...) {
             cout << "Unknown Error !" << endl;
          }
    
    
          Line->Print();
    
    
          delete Line;
    
    
          while (kbhit())
             getch();
          getch();
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Bump!?

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Just wait until someone replies. Your thread's not going anywhere and the members here are eager to help you if they can.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    How could i make a method in ComputerNetwork class that gives me MAX Ram value at the end of the program?
    perhaps show your attempt on the function GetMaxRam() and describe what problem you are having with it, in light of the work you have posted so far.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What does "MAX Ram value" mean?

    I don't understand what GetAtm or SetAtm are for either, i.e how they have any relation to what you are asking, and why they are of type 'short'.

    You're not getting a useful response quickly because you haven't explained the problem well enough.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    1. You're using void main(), this is wrong, you should be using int main().
    2. You're using deprecated headers, use <iostream> instead of <iostream.h>, and don't use <conio.h> at all, also, use <cstdio> instead of <stdio.h>.
    3. You're using char* instead of std::string, why?
    4. Don't use getch()
    5. Don't use raw array, use std::vector or some other form of STL container instead.
    6. I suggest using smart pointers instead of managing memory by hand.
    7. Are you by any chance a Java programmer? Stuff like this smells like Java to me:

    Code:
    inline void Computer::SetAtm(short RAM) {
        this->RAM = RAM;
    }
    Just use a different name for the parameter and lose the "this->". Imo atleast.

    8. I get the feeling you're using some ancient compiler from the 90's. Don't.
    9. Your indentation is inconsistent, it shouldn't be.
    10. Would it kill you to use some spacing? It's very crammed, which makes it harder to follow.

    Fix the above, and we will have a much easier time helping you.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  7. #7
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by Neo1 View Post
    1. You're using void main(), this is wrong, you should be using int main()
    Making a new method-avatar40_2-gif Salem
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    call me a cynic but was this code written by the op to start with
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by rogster001 View Post
    call me a cynic but was this code written by the op to start with
    If he stole that, he needs to find a new place to take code from. It looks like C++ and C had a baby with a learning disorder, dropped it on its head at birth and made it live in a closet with nothing to eat but noodles and rat feces for 21 years, then released it into the world and said "Go crazy."
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Here's where OverflowException came from, as well as a lot of other structural stuff..

    Code:
         while (kbhit())
             getch();
          getch();
    This is ...unfortunate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which method is better?
    By airesore in forum Windows Programming
    Replies: 9
    Last Post: 10-24-2011, 02:12 PM
  2. difference between this->method() and method()
    By nacho4d in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2009, 04:11 PM
  3. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 3
    Last Post: 09-19-2009, 10:35 AM
  4. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 10:56 AM
  5. Replies: 2
    Last Post: 01-13-2003, 01:28 PM