#include
using namespace std;
class CPU
{
CPU(int c=1);
};
class RAM
{
RAM(int ram=2);
};
class CDROM
{
CDROM(int cdrom=3);
};
class Computer
{
public:
Computer(CPU c,RAM r,CDROM d);
~Computer();
void run()
{
cout<<"cpu为:"<<" "<<"ram为:"<<" "<<"cdrom为:"<<endl;
cout<<"调用的是这个run函数"<<endl;
}
void stop()
{
cout<<"调用的是这个stop函数"<<endl;
}
private:
CPU cpu;
RAM ram;
CDROM cdrom;
};
Computer::Computer(CPU c,RAM r,CDROM d):cpu(c),ram(r),cdrom(d)
{
cout<<"调用的是构造函数"<<endl;
}
Computer::~Computer()
{
cout<<"调用的是该析构函数"<<endl;
}
int main()
{
Computer thing();
thing.run();
thing.stop();
return 0;
}
你的类定义不符合语法
#include <iostream>
using namespace std;
class CPU
{
public:
CPU(int c = 1) {}
};
class RAM
{
public:
RAM(int ram = 2) {}
};
class CDROM
{
public:
CDROM(int cdrom = 3) {}
};
class Computer
{
public:
Computer(CPU c, RAM r, CDROM d);
~Computer();
void run()
{
cout << "cpu为:" << " " << "ram为:" << " " << "cdrom为:" << endl;
cout << "调用的是这个run函数" << endl;
}
void stop()
{
cout << "调用的是这个stop函数" << endl;
}
private:
CPU cpu;
RAM ram;
CDROM cdrom;
};
Computer::Computer(CPU c, RAM r, CDROM d) :cpu(c), ram(r), cdrom(d)
{
cout << "调用的是构造函数" << endl;
}
Computer::~Computer()
{
cout << "调用的是该析构函数" << endl;
}
int main()
{
CPU c;
RAM r;
CDROM cd;
Computer thing(c, r, cd);
thing.run();
thing.stop();
return 0;
}