下列程序的运行结果为:#include<iostream> #include<string> using namespace std ; class A_class { char name[20] ; public : void put_name( char * s ) { strcpy( name, s ) ; } void show() { cout << name << "\n" ; } }; class B_class : public A_class { char phone_num[ 20 ] ; public : void put_phone( char * num ) { strcpy ( phone_num , num ) ; } void show() { cout << phone_num << "\n" ; } }; int main() { B_class B_obj ; B_class * B_p ; A_class A_obj; //B_p=&A_obj; //出错,不用用派生类指针指向派生类对象 B_p = & B_obj ; B_p-> put_phone ( "5555_12345678" ); B_p -> show() ; B_p -> put_name( "Chen ming" ) ; B_p-> A_class::show(); ((A_class*)B_p )-> show() ; }
时间: 2024-03-30 15:35:28 浏览: 17
该程序的运行结果为:
5555_12345678
Chen ming
Chen ming
程序中定义了两个类A_class和B_class,B_class是A_class的公有派生类。在主函数中,定义了B_class类型的对象B_obj和指向B_class类型的指针B_p,以及A_class类型的对象A_obj。
在程序中,首先将B_p指向B_obj,然后调用了B_class的成员函数put_phone,将一个电话号码赋给phone_num成员变量,并通过B_p的show函数输出该号码。接着调用B_class的成员函数put_name,将一个名字赋给name成员变量,并通过B_p->A_class::show()函数输出该名字,最后通过(A_class*)B_p将B_p强制转换为A_class类型的指针,再通过该指针调用A_class的成员函数show(),输出该名字。因此,程序的运行结果为:
5555_12345678
Chen ming
Chen ming
相关问题
#include<iostream> using namespace std; class Car { char* pName; int id; int m; public: Car(string name) { } }; 怎么将string name转换为char*pName
您可以使用`c_str()`函数将`std::string`类型转换为`const char*`类型,然后再将其复制到`char*`类型的变量中。下面是修改后的代码:
```
#include <iostream>
using namespace std;
class Car {
char* pName;
int id;
int m;
public:
Car(string name) {
pName = new char[name.length() + 1];
strcpy(pName, name.c_str());
id = 0;
m = 0;
}
~Car() {
delete[] pName;
}
};
int main() {
Car car("My car");
return 0;
}
```
在构造函数中,我们使用`new char[name.length() + 1]`为`pName`分配内存,该内存可以容纳字符串及其终止符。然后我们使用`strcpy()`函数将`name.c_str()`复制到`pName`中。最后,在析构函数中,我们使用`delete[]`释放内存。
下列代码的运行结果为:#include<iostream> #include<string> using namespace std ; class A_class { string name[20] ; public : void put_name( string * s ) { strcpy( name, s ) ; } void show_name() { cout << name << "\n" ; } }; class B_class : public A_class { char phone_num[ 20 ] ; public : void put_phone( char * num ) { strcpy ( phone_num , num ); } void show_phone() { cout << phone_num << "\n"; } }; int main() { A_class * A_p ; A_class A_obj ; B_class B_obj ; A_p = & A_obj ; A_p -> put_name( "Wang xiao hua" ) ; A_p -> show_name() ; A_p = & B_obj ; A_p -> put_name( "Chen ming" ) ; A_p -> show_name() ; B_obj. put_phone ( "5555_12345678" ); ( ( B_class * ) A_p ) -> show_phone() ; }
该代码会出现编译错误。因为在类A_class中的name成员变量是一个字符串数组,而在成员函数put_name中使用了strcpy函数将一个字符串指针直接赋给了name数组,这是错误的。应该使用strcpy的安全版本strcpy_s,并指定要复制的字符串的长度。因此,需要将代码中的put_name函数修改为以下形式:
```
void put_name(string* s)
{
strcpy_s(name, sizeof(name), s->c_str());
}
```
另外,由于在类A_class中的name成员变量是一个字符串数组,因此在show_name函数中应该使用cout输出name数组的内容,而不是直接输出name指针。因此,需要将代码中的show_name函数修改为以下形式:
```
void show_name()
{
cout << name << "\n";
}
```
修改后的代码如下,运行结果会输出"Wang xiao hua"、"Chen ming"和"5555_12345678":
```
#include<iostream>
#include<string>
using namespace std;
class A_class
{
string name[20];
public:
void put_name(string* s)
{
strcpy_s(name, sizeof(name), s->c_str());
}
void show_name()
{
cout << name << "\n";
}
};
class B_class : public A_class
{
char phone_num[20];
public:
void put_phone(char* num)
{
strcpy(phone_num, num);
}
void show_phone()
{
cout << phone_num << "\n";
}
};
int main()
{
A_class* A_p;
A_class A_obj;
B_class B_obj;
A_p = &A_obj;
A_p->put_name(new string("Wang xiao hua"));
A_p->show_name();
A_p = &B_obj;
A_p->put_name(new string("Chen ming"));
A_p->show_name();
B_obj.put_phone("5555_12345678");
((B_class*)A_p)->show_phone();
}
```