为下面的类添加复制构造函数和赋值运算符。 #include<cstring> class A{ public: A(const char *p) { len = strlen(p)+1; pchar = new char[len]; strcpy(pchar,p); } ~A() { delete pchar; pchar=0; } private: char *pchar; int len; };
时间: 2024-01-26 15:02:25 浏览: 63
下面是添加复制构造函数和赋值运算符的代码:
```c++
#include <cstring>
class A {
public:
// 构造函数
A(const char *p) {
len = strlen(p) + 1;
pchar = new char[len];
strcpy(pchar, p);
}
// 复制构造函数
A(const A& other) {
len = other.len;
pchar = new char[len];
strcpy(pchar, other.pchar);
}
// 赋值运算符
A& operator=(const A& other) {
if (this != &other) {
delete pchar;
len = other.len;
pchar = new char[len];
strcpy(pchar, other.pchar);
}
return *this;
}
// 析构函数
~A() {
delete pchar;
pchar = nullptr;
}
private:
char *pchar;
int len;
};
```
相关问题
#include <iostream> #include <cstdio> #include <cstring> using namespace std; class STRING{ char* pstr; public: STRING(const char* s="");//已实现 ~STRING();//已实现 friend istream& operator>>(istream& in, STRING& s); friend ostream& operator<<(ostream& os, const STRING& s); }; int main() { STRING s; cin >> s; cout << s << endl; return 0; } /* 请在这里填写答案,,你只需要实现输入输出运算符(>>、<<)的重载即可 */
可以这样实现输入输出运算符的重载:
```c++
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
class STRING{
char* pstr;
public:
STRING(const char* s="");
~STRING();
friend istream& operator>>(istream& in, STRING& s);
friend ostream& operator<<(ostream& os, const STRING& s);
};
STRING::STRING(const char* s) {
pstr = new char[strlen(s)+1];
strcpy(pstr, s);
}
STRING::~STRING() {
delete []pstr;
}
istream& operator>>(istream& in, STRING& s) {
char buffer[1024];
in.getline(buffer, 1024);
s.pstr = new char[strlen(buffer)+1];
strcpy(s.pstr, buffer);
return in;
}
ostream& operator<<(ostream& os, const STRING& s) {
os << s.pstr;
return os;
}
int main() {
STRING s;
cin >> s;
cout << s << endl;
return 0;
}
```
在输入运算符中,我们使用`getline()`函数来读取输入的字符串,然后动态分配空间并将字符串拷贝到对象的数据成员中。
在输出运算符中,我们直接输出对象的数据成员。
注意,在使用动态分配空间时,需要在析构函数中释放空间。
将下面的c语言代码转化为java代码:#include <iostream> #include <cstring> #include<cmath> #include<stdio.h> #include<map> using namespace std; //词典结构 struct S { char des[15]; //目标语言 char src[15]; //英语 }dic[100010]; //定义比较器,目标语言字典序升序排列 int cmp(const void *a, const voi
Java代码如下:
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
//词典结构
static class S {
String des; //目标语言
String src; //英语
}
static S[] dic = new S[100010];
//定义比较器,目标语言字典序升序排列
static class MyComparator implements Comparator<S> {
public int compare(S s1, S s2) {
return s1.des.compareTo(s2.des);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, String> map = new HashMap<String, String>();
int cnt = 0;
while (scanner.hasNext()) {
String src = scanner.next();
String des = scanner.next();
dic[cnt] = new S();
dic[cnt].des = des;
dic[cnt].src = src;
cnt++;
}
scanner.close();
//按目标语言字典序升序排列
Arrays.sort(dic, 0, cnt, new MyComparator());
//构建映射表
for (int i = 0; i < cnt; i++) {
map.put(dic[i].des, dic[i].src);
}
//查询并输出翻译结果
while (scanner.hasNext()) {
String word = scanner.next();
if (map.containsKey(word)) {
System.out.println(map.get(word));
} else {
System.out.println("eh");
}
}
}
}
阅读全文