class room{ int number; String condition;//状态 room[] rooms = new room[20];//房间数量 //初始化房间 room(int number,String condition) { this.number = number; this.condition = condition; } room(String condition){} room(int number){} void ww(){ int count = 0; for (int i = 0; i < 20; i++) { rooms[i] = new room(i,null); System.out.print(rooms[i]+"\t"); count ++; /* if (count % 5 == 0){ System.out.println(); }*/ } } room(){} @Override public String toString() { return number + "\t"+condition; } }
时间: 2024-02-14 15:30:38 浏览: 19
你好!你的代码定义了一个名为"room"的类,表示一个房间。该类有三个属性:number(间号),condition(状态)和rooms(房间数组)。构造函数用于初始化房间,有三种重载形式:一个参数是房间号和状态,两个参数是状态,和房间号。还有一个无参构造函数。ww()方法用于初始化房间数组,并打印出每个房间的信息。toString()方法用于返回房间的信息字符串。
请问你还有其他问题吗?
相关问题
帮我完善class Book { private: // 书本ID int id; string title; };
好的,以下是完善后的代码,我添加了构造函数、设置和获取 id、title 的方法:
```
class Book {
private:
int id; // 书本ID
string title;
public:
Book(int id, string title) : id(id), title(title) {}
virtual ~Book() {}
// 设置 id
void setId(int id) {
this->id = id;
}
// 获取 id
int getId() const {
return this->id;
}
// 设置 title
void setTitle(string title) {
this->title = title;
}
// 获取 title
string getTitle() const {
return this->title;
}
};
```
在上面的代码中,我添加了构造函数,用于初始化 id 和 title 成员变量。同时,我添加了设置和获取 id、title 的方法,用于对私有成员变量进行操作。这样,Book 类就能够更加完整地描述一本书的信息。
完成下列的 String 类,并在主函数 main( )中测试它。 class String { public: String(const char *str = NULL); // constructor String(const String &other); // copy constructor ~ String(void); // destructor String & operate =(char *str); String & operate =(const String &other);// 重载=运算符 int operator==(String &other); // 重载==运算符 int operator==(char *str); private: char *m_data; // used for storing the string int length; };
以下是 String 类的实现以及在主函数 main() 中的测试。
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class String {
public:
String(const char *str = NULL);
String(const String& other);
~String(void);
String& operator=(char *str);
String& operator=(const String& other);
int operator==(String& other);
int operator==(char *str);
private:
char *m_data;
int length;
};
String::String(const char* str) {
if (str != NULL) {
length = strlen(str);
m_data = new char[length + 1];
strcpy(m_data, str);
}
else {
length = 0;
m_data = new char[1];
*m_data = '\0';
}
}
String::String(const String& other) {
length = other.length;
m_data = new char[length + 1];
strcpy(m_data, other.m_data);
}
String::~String(void) {
delete[] m_data;
}
String& String::operator=(char *str) {
if (m_data == str) {
return *this;
}
if (str != NULL) {
length = strlen(str);
char *temp = new char[length + 1];
strcpy(temp, str);
delete[] m_data;
m_data = temp;
}
else {
length = 0;
delete[] m_data;
m_data = new char[1];
*m_data = '\0';
}
return *this;
}
String& String::operator=(const String& other) {
if (m_data == other.m_data) {
return *this;
}
length = other.length;
char *temp = new char[length + 1];
strcpy(temp, other.m_data);
delete[] m_data;
m_data = temp;
return *this;
}
int String::operator==(String& other) {
return strcmp(m_data, other.m_data) == 0;
}
int String::operator==(char *str) {
return strcmp(m_data, str) == 0;
}
int main() {
String s1("hello");
String s2(s1);
String s3("world");
String s4;
s4 = s1;
cout << s1.operator==("hello") << endl; // 1
cout << s1.operator==(s2) << endl; // 1
cout << s1.operator==(s3) << endl; // 0
cout << s1.operator==(s4) << endl; // 1
return 0;
}
```