lianxi12.cpp

来自「用vc++来实现对学生信息的管理程序.着重在复制构造函数的使用方法上」· C++ 代码 · 共 59 行

CPP
59
字号
// lianxi12.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <string.h>
class student
{public:
  student();
  student(char *pid,char *pname,int a,float s);
  student(student &init);
  void display();
  ~student();
private:
	char *id;
	char *name;
	int age;
	float score;
};
student::student()
{id=new char[9];
 strcpy(id,"00000000");
 name=new char[11];
 strcpy(name,"        ");
 age=0;
 score=0;
}
student::student(char *pid,char *pname,int a,float s)
{id=new char[strlen(pid)+1];
 strcpy(id,pid);
 name=new char[strlen(pname)+1];
 strcpy(name,pname);
 age=a;
 score=s;
}
student::student(student &init)
{delete[] id;
delete[] name;
id=new char[strlen(init.id)+1];
strcpy(id,init.id);
name=new char[strlen(init.name)+1];
strcpy(name,init.name);
}
void student::display()
{cout<<"  id:"<<id<<endl;
 cout<<" name"<<name<<endl;
 cout<<" age:"<<age<<endl;
 cout<<" score:"<<score<<endl;
}
student::~student()
{ delete[] name;
  delete[] id;
}
void main()
{ student s1("03410101","zhanghua",19,95);
  s1.display();
  student s2(s1);
  s2.display();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?