C++ experiment---give me a computer

Shandong University of Science and Technology C++ Experiment—Give me a computer
Description

CPU frequency and main memory capacity are the main indicators to measure the performance of a computer. Please define:

CPU class: There is only one int type data member to indicate its main frequency, and please write the necessary member functions (including constructors) according to the output and the given main() function.

Memory class: There is only one int type data member to express its capacity, and please write the necessary member functions (including constructors) according to the output and the given main() function.

Computer class:

(1) The three data members are the CPU object, the Memory object, and a string (indicating who owns the computer).

(2) Write the necessary member functions (including constructors) according to the output and the given main() function.

(3) The void show() method is used to output the computer's information according to the sample output format.

Input

There are 2 lines of input. Each line includes 2 integers and 1 character string, which respectively represent the CPU frequency, memory capacity, and the name of the computer owner.

Output

See example.

Sample Input

2 1000 Zhang
4 2000 Li

Sample Output

This is Zhang’ computer with CPU = 2GHz, memory = 1000MB.
This is Li’ computer with CPU = 4GHz, memory = 2000MB.

Given the main function of the title:

int main()
{
    
    
    int c, m;
    string n;
    cin>>c>>m>>n;
    CPU cpu(c);
    Memory mem(m);
    Computer com1(cpu, mem, n);
    cin>>c>>m>>n;
    Computer com2(c, m, n);
    com1.show();
    com2.show();
    return 0;
}

code:

#include<iostream>

using namespace std;
class CPU{
    
    
	int f;
public:
	CPU(int F){
    
    
		f=F;
	}
	int getF(){
    
    
		return f;
	}
};

class Memory{
    
    
	int m;
public:
	Memory(int M){
    
    
		m=M;
	}
	int getMemory(){
    
    
		return m;
	}
};

class Computer{
    
    
	CPU cpu;
	Memory memory;
	string master;
	
public:
	Computer(CPU C,Memory M,string MA):cpu(C),memory(M),master(MA){
    
    
		
	}
	void show(){
    
    
		cout<<"This is "+master+"’ computer with CPU = "<<cpu.getF()<<"GHz, memory = "<<memory.getMemory()<<"MB."<<endl;
	}
};

int main()
{
    
    
    int c, m;
    string n;
    cin>>c>>m>>n;
    CPU cpu(c);
    Memory mem(m);
    Computer com1(cpu, mem, n);
    cin>>c>>m>>n;
    Computer com2(c, m, n);
    com1.show();
    com2.show();
    return 0;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115033726