void main() { char *ps="this is a book"; int n=10; cout<<ps<<'\n'; }
时间: 2023-11-27 19:49:15 浏览: 39
这段代码存在一些问题:
1. `void main()` 应该改为 `int main()` ,因为 `main()` 函数需要有返回值。
2. 缺少头文件,应该加上 `#include <iostream>` 和 `using namespace std;` 。
3. 变量 `n` 没有被使用,可以删除。
4. `ps` 是一个指向字符的指针,应该使用 `%s` 格式符输出字符串,而不是 `%d` 。
5. 最后应该加上 `return 0;`。
修改后的代码如下:
```cpp
#include <iostream>
using namespace std;
int main() {
char *ps = "this is a book";
cout << ps << '\n';
return 0;
}
```
相关问题
优化以下代码:#include <iostream> #include<string.h> using namespace std; class Book { private: char bookname[30]; char authers[30]; char publishing_house[40]; int pages; float price; public: char getbookname(); char getauthers(); char getpublishing_house(); int getpages(); float getprice(); void setbookname(char *a); void setauthers(char *a); void setpublishing_house(char *a); void setpages(int a); void setprice(float a); Book(char*a,char*b,cahr*c,int d,float e) { strcpy(bookname,a); strcpy(authers,b); strcpy(publishing_house,c); pages=d; price=e; } }; char getbookname() { char*a=bookname; return a; } char getauthers() { char*a=authers; return a; } char getpublishing_house() { char*a=publishing_house; return a; } int getpages() { int a=pages; return a; } float getprice() { float a=price; return a; } void setbookname(char *a) { strcpy(bookname,a); } void setauthers(char *a) { strcpy(authers,a); } void setpublishing_house(char *a) { strcpy(publishing_house,a); } void setpages(int a) { pages=a; } void setprice(float a) { price=a; } int main() { Book a(char a="《C++程序设计》",char b="谭浩强编制",char c="清华大学出版社",d=484,e=36.00); Book b(char a="《吹牛大王历险记》",char b="拉斯伯等编著",char c="天津人民出版社",d=149,e=12.80); cout<<a.getbookname()<<" "<<a.getauthers()<<" "<<a.getpublishing_house()<<" "<<"页数:"<<a.getpages()<<"价钱:"<<a.getprice()<<endl; cout<<b.getbookname()<<" "<<b.getauthers()<<" "<<b.getpublishing_house()<<" "<<"页数:"<<b.getpages()<<"价钱:"<<b.getprice()<<endl; return 0; }
以下是优化后的代码:
#include<iostream>
#include<string.h>
using namespace std;
class Book {
private:
char bookname[30];
char authers[30];
char publishing_house[40];
int pages;
float price;
public:
Book(char* a="《C++程序设计》", char* b="谭浩强编制", char* c="清华大学出版社", int d=484, float e=36.00) {
strcpy(bookname, a);
strcpy(authers, b);
strcpy(publishing_house, c);
pages = d;
price = e;
}
char* getbookname() {
return bookname;
}
char* getauthers() {
return authers;
}
char* getpublishing_house() {
return publishing_house;
}
int getpages() {
return pages;
}
float getprice() {
return price;
}
void setbookname(char* a) {
strcpy(bookname, a);
}
void setauthers(char* a) {
strcpy(authers, a);
}
void setpublishing_house(char* a) {
strcpy(publishing_house, a);
}
void setpages(int a) {
pages = a;
}
void setprice(float a) {
price = a;
}
};
int main() {
Book a;
Book b("《吹牛大王历险记》", "拉斯伯等编著", "天津人民出版社", 149, 12.80);
cout << a.getbookname() << " " << a.getauthers() << " " << a.getpublishing_house() << " " << "页数:" << a.getpages() << "价钱:" << a.getprice() << endl;
cout << b.getbookname() << " " << b.getauthers() << " " << b.getpublishing_house() << " " << "页数:" << b.getpages() << "价钱:" << b.getprice() << endl;
return 0;
}
主要的修改如下:
1. 在类定义中,将所有的成员函数都定义在类的内部,并修改它们的访问修饰符为 public。
2. 在类定义中,将构造函数的参数都设置默认值,以免在创建对象的时候不必要地传递参数。
3. 在类定义中,将所有的成员变量都设置为 private。
4. 在类定义中,修改字符串类型成员变量的 getter 和 setter 函数,使它们返回和接收字符指针类型的数据。
5. 在类定义中,修改整型和浮点型成员变量的 getter 和 setter 函数,使它们返回和接收对应的数据类型。
6. 在主函数中,第一个 Book 对象 a 的构造函数调用中去掉了赋值语句,因为这些赋值已经在类的构造函数中完成了。
7. 在主函数中,第二个 Book 对象 b 的构造函数调用中省略了参数的类型,因为在构造函数定义中已经指明了它们的类型。
8. 在类定义中,修改 char 类型成员变量的 setter 函数,使它们使用 strcpy 函数进行赋值。
#include <iostream> #include <cmath>**** using namespace std; class Book { public: Book(const char *bookTitle) { pTitle = new char[strlen(bookTitle) + 1]; strcpy(pTitle, bookTitle); } void printInfo() const { cout << pTitle << endl; } ~Book() { delete pTitle;*** } private: char *pTitle; }; int main() { char bookTitle[60]; Book *pBook; cout << "Please enter book title: "; gets(bookTitle); Book book(); pBook = book; pBook.printInfo(); return 0; }
这段代码中有多处错误:
1. `#include <cmath>` 库未被使用,可以删除。
2. `using namespace std;` 没有错误。
3. 在类 `Book` 的析构函数中,应该使用 `delete[]` 释放 `new` 分配的数组内存,而不是使用 `delete`。
4. 在 `main` 函数中,应该使用 `std::cin` 和 `std::cout`,而不是 `gets` 和 `cout`。
5. 在 `main` 函数中,创建对象的方式不正确,应该使用 `new` 创建对象,并且调用对象的方法时应该使用箭头运算符 `->`。
下面是修改后的代码:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class Book {
public:
Book(const char *bookTitle) {
pTitle = new char[strlen(bookTitle) + 1];
strcpy(pTitle, bookTitle);
}
void printInfo() const {
cout << pTitle << endl;
}
~Book() {
delete[] pTitle;
}
private:
char *pTitle;
};
int main() {
char bookTitle[60];
Book *pBook;
cout << "Please enter book title: ";
cin.getline(bookTitle, 60);
pBook = new Book(bookTitle);
pBook->printInfo();
delete pBook;
return 0;
}
```