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();
}