Các bạn cho mình hỏi hàng code : “this->chiptype = new char[strlen(chiptype), +1];” có tác dụng gì ?
#include <iostream>
using namespace std;
class Computer
{
char *chiptype;
int speed;
public:
void getdetails()
{
cin >> speed;
cin.ignore();
chiptype = new char[86];
gets(chiptype);
}
void showdetails()
{
cout << "Chip" << chiptype << " Speed = "<< speed;
}
Computer()
{
chiptype = NULL;
speed = 0;
}
Computer(char *chiptype, int speed)
{
this->chiptype = new char[strlen(chiptype), +1];
strcpy(this->chiptype, chiptype);
this->speed = speed;
}
Computer(Computer &cp)
{
this->chiptype = new char[strlen(cp.chiptype) + 1];
strcpy(this->chiptype, cp.chiptype);
this->speed = cp.speed;
}
~Computer()
{
if (this->chiptype)
delete[]chiptype;
}
};
void main()
{
Computer cp1;
Computer cp2("ABC", 4);
Computer cp3(cp2);
cp1.getdetails();
cp1.showdetails();
cp2.showdetails();
cp3.showdetails();
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?