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