1 条题解

  • 0
    @ 2025-4-12 22:06:17

    C++ :

    #include <iostream>
    #include <string.h>
    using namespace std;
    class Person{
    	public:
    	Person();
    	void Register();//成员 赋值函数
    	void  showme(); 
    	~Person();
    	private:
    		char name[20];
    		int age;
    		char x;
    		
    };
    Person::Person(){
    	char w[20]="XXX";
    	int a=0;
    	char c='m';
    	strcpy(name,w);
    	age=a;
    	x=c;
    }
     void Person::Register(){
     	cin>>name;
     	cin>>age;
     	cin>>x;
     }
     void Person::showme(){
     	cout<<name<<" "<<age<<" "<<x<<endl; 
     }
     Person::~Person(){
     	cout<<"Now destroying the instance of Person"<<endl;
     }
      
    int main(){
    	int i;
    	Person s[2];
    	Person *p1,*p2;
    	p1=&s[0];
    	p2=&s[1];
    		s[0].Register();
    	for(i=0;i<2;i++){
    		cout<<"person"<<i+1<<":";
    	   s[1].showme();
    	}
    	s[1]=s[0];
    	cout<<"person"<<1<<":";
    	p1->showme();
    	cout<<"person"<<2<<":";
    	p2->showme(); 
    return 0;	
    }  
    
    • 1

    C++作业6章指针:动态生成Person类的对象

    信息

    ID
    2867
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者