C++模拟CPU

Computer.cpp

#include <iostream>
#include "CPU.cpp"
#include "Memory.cpp"
using namespace std;

class Computer
{
private:
	CPU cpu;
	Memory memory;
public:
	Computer()
	{
		cout << "Computer constructor 1 begins to be called ..." << endl;
		cpu = NULL;
		memory = NULL;
		cout << "Computer constructor 1 has been called ..." << endl;
	}

	Computer(CPU cpu, Memory memory)
	{
		cout << "Computer constructor 2 begins to be called ..." << endl;
		this->cpu = cpu;
		this->memory = memory;
		cout << "Computer constructor 2 has been called ..." << endl;
	}

	void show()
	{
		cpu.show();
		memory.show();
	}
	~Computer()
	{
		cout << "Computer destructor has been called!" << endl;
	}
};

CPU.cpp


#include <iostream>
using namespace std;

class CPU
{
private:
	int type;
public:

	CPU()
	{
		type = -1;
		cout << "CPU constructor 1 has been called ..." << endl;
	}

	CPU(int type)
	{
		this->type = type;
		cout << "CPU constructor 2 has been called ..." << endl;
	}

	CPU(CPU & cpu)
	{
		type = cpu.type;
		cout << "CPU copy constructor has been called ..." << endl;
	}

	void show()
	{
		cout << "the CPU type is : " << type << endl;
	}

	~CPU()
	{
		cout << "CPU destructor has been called!" << endl;
	}
};

Memory.cpp


#include <iostream>
using namespace std;

class Memory
{
private:
	int size;
public:
	Memory()
	{
		size = -1;
		cout << "Memory constructor 1 has been called ..." << endl;
	}

	Memory(int size)
	{
		this->size = size;
		cout << "Memory constructor 2 has been called ..." << endl;
	}

	Memory(Memory & memory)
	{
		size = memory.size;
		cout << "Memory copy constructor has been called ..." << endl;
	}

	void show()
	{
		cout << "the size of Memory is : " << size << endl;
	}

	~Memory()
	{
		cout << "Memory destructor has been called!" << endl;
	}
};

test.cpp

#include "Computer.cpp";


void test1()
{
	CPU cpu;
	cpu.show();
}

void test2()
{
	Computer computer;
	computer.show();
}

void test3()
{
	CPU cpu(1);
	Memory memory(3);
	Computer test(cpu, memory);
	test.show();
}

int main()
{
	test1();
	test2();
	test3();
}

test1.cpp

#include "Computer.cpp";

int main()
{
	CPU cpu(1);
	Memory memory(3);
	Computer test(cpu, memory);
	test.show();
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值