// omputer.h: interface for the Computer class.
//
//
#if !defined(AFX_OMPUTER_H__2C841290_BF9E_410C_960A_9E152D78E5D0__INCLUDED_)
#define AFX_OMPUTER_H__2C841290_BF9E_410C_960A_9E152D78E5D0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include<string>
using std::string;
class Computer
{
int nCPUs;
int cpuFreq;
string cpuBrand;
public:
//有默认参数的函数起到重载函数的作用
Computer(int n=1);//Computer(); //default constructor
virtual ~Computer();//destructor
};
#endif // !defined(AFX_OMPUTER_H__2C841290_BF9E_410C_960A_9E152D78E5D0__INCLUDED_)
int main()
{
Computer acer;//默认初始化
int x(3);
int y;
Computer hp(16);
Computer *p=NULL;
p = new Computer(("Cortex"),4);
Computer dell(string("intel"),8,3.2);
Computer IBM(string("AMD"));
if(p)
delete p;
Computer *q= new Computer[4];//对象数组
delete []q;
Computer w101[60]={{},{},{}};//对象初始化
//Computer a{string("Intel"),7,2.4};新版本的初始化,vc6.0不支持
return 0;
}
// computer.cpp: implementation of the computer class.
//
//
#include "computer.h"
#include<iostream>
using std::cout;
using std::endl;
Computer::Computer(int n)
{
nCPUs=n;
cout<<"内核:"<<n<<endl;
cout<<"默认构造函数"<<endl;
}
Computer::~Computer()
{
cout<<"析构函数\t";
cout<<"销毁电脑的CPU核心数:"<<nCPUs<<endl;
}