#include<iostream> using namespace std ; class Tclass { public : int x, y ; void print() { cout << x << "," << y << endl ; } ; } ; int add( Tclass * ptf ) { return ( ptf->x + ptf->y ) ; } int main() { Tclass test, * pt = new(Tclass) ; pt->x = 100 ; pt->y = 200 ; pt->print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout << "x+y=" << add(&test) ; delete pt; }
时间: 2024-04-21 17:25:17 浏览: 16
这是一个使用 C++ 编写的程序,定义了一个名为 Tclass 的类,其中包含两个整型变量 x 和 y,以及一个 print 方法,用于输出 x 和 y 的值。该程序还定义了一个名为 add 的函数,接受一个 Tclass 类型的指针作为参数,返回指针所指向对象 x 和 y 的和。在 main 函数中,创建了一个 Tclass 类型的对象 test,通过指针 pt 创建了另一个 Tclass 类型的对象,并分别对两个对象的 x 和 y 赋值,然后调用 print 方法输出它们的值,并调用 add 函数输出 test 对象 x 和 y 的和。最后释放了通过 new 关键字动态分配的内存空间。
相关问题
这份代码有什么问题#include <iostream> using namespace std; class time { private : int H; int F; int M; public : void sett(int, int, int); void print(); }; void time:: sett(int h, int f, int m) { H = h; F = f; M = m; } void time:: print() { printf("%2d-%2d-%2d", H, F, M); } int main() { int h, f, m; cin >> h >> f >> m; sett(h, f, m); return 0; }
这份代码有几个问题:
1. 在 main 函数中,调用了 `sett(h, f, m)`,但是 `sett` 函数并不是 main 函数的成员函数,因此应该先创建一个 time 类的对象,然后通过这个对象调用 `sett` 函数。
2. 在 `print` 函数中使用了 `printf` 函数,但是这个函数需要包含头文件 `stdio.h`,因此应该使用 `cout` 来代替。
3. 在 `print` 函数中使用了 `%2d` 的格式化字符串,但是应该使用 `%02d` 来确保输出的数字总是两位数。
4. 在 `print` 函数中没有输出分隔符,因此输出的时间格式不正确,应该在每两个数字之间加上一个分隔符,比如冒号。
下面是修改后的代码:
```
#include <iostream>
using namespace std;
class time {
private:
int H;
int F;
int M;
public:
void sett(int, int, int);
void print();
};
void time::sett(int h, int f, int m) {
H = h;
F = f;
M = m;
}
void time::print() {
cout << setw(2) << setfill('0') << H << ":" << setw(2) << setfill('0') << F << ":" << setw(2) << setfill('0') << M;
}
int main() {
int h, f, m;
cin >> h >> f >> m;
time t;
t.sett(h, f, m);
t.print();
return 0;
}
```
设有如下代码,代码无法正常运行。请添加必要的成员函数使之能够正确运行。 #include <iostream> #include <string> using namespace std; class T { char *p1; public: T(char *s1=""); ~T(); void print() {cout<<"p1="<<p1<<endl;} }; T::T(char *s1) { p1=new char[strlen(s1)+1]; strcpy(p1,s1); } T::~T(){ delete [] p1;} int main() { T t1("Lucky boy"),t2(t1),t3; t1.print(); t2.print(); t3=t2=T("Welcome"); t2.print(); t3.print(); }
代码中缺少赋值运算符重载函数,需要添加以下成员函数:
```
T& operator=(const T& t) {
if (this == &t) return *this;
delete [] p1;
p1 = new char[strlen(t.p1)+1];
strcpy(p1, t.p1);
return *this;
}
```
完整代码如下:
```cpp
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class T {
char *p1;
public:
T(char *s1="");
~T();
T& operator=(const T& t) {
if (this == &t) return *this;
delete [] p1;
p1 = new char[strlen(t.p1)+1];
strcpy(p1, t.p1);
return *this;
}
void print() {cout<<"p1="<<p1<<endl;}
};
T::T(char *s1) {
p1=new char[strlen(s1)+1];
strcpy(p1,s1);
}
T::~T(){
delete [] p1;
}
int main() {
T t1("Lucky boy"),t2(t1),t3;
t1.print();
t2.print();
t3=t2=T("Welcome");
t2.print();
t3.print();
return 0;
}
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)