【程序阅读题】 定义一个学生类CStudent:包含共有的属性id,name, 和公用的方法setname($name2),getname():分别设置属性name的值、获取属性name的值。 填空补充: class CStudent { public $id=1; public $name="who"; public function setname($name2) { _____________ = $name2; //填空补充 } public function getname() { return $this->name; } }
时间: 2024-03-19 10:42:02 浏览: 95
class CStudent
{
public $id=1;
public $name="who";
public function setname($name2)
{
$this->name = $name2; //填空补充
}
public function getname()
{
return $this->name;
}
}
相关问题
#include <iostream.h> #include <string.h> class CStudent { public: CStudent(char *n, int a); ~CStudent(); static void SetAge(int age); private: char *name; int age; static int nTotalObj; }; int CStudent::nTotalObj = 0; CStudent::CStudent(char *n, int a) :age(a) { int nLen = strlen(n); name = new char[nLen+1]; strcpy(name,n); name[nLen] = '\0'; nTotalObj++; } CStudent::~CStudent() { delete[] name; nTotalObj--; } void CStudent::SetAge(int age) { this->age = age; } void main() { CStudent stu1("张三",25); CStudent str2("李四",26); cout<<"CStudent::nTotalObj="<<CStudent::nTotalObj<<endl; }问题三:如果不改变主函数和类 CStudent 中的成员变量的属性,应该如何改正该程序?
这个程序存在一个问题,就是在类 CStudent 中定义的 name 和 age 成员变量没有进行释放,会导致内存泄漏。解决方法是在类的析构函数中释放这些成员变量。
修改后的代码如下:
```
#include <iostream.h>
#include <string.h>
class CStudent {
public:
CStudent(char *n, int a);
~CStudent();
static void SetAge(int age);
private:
char *name;
int age;
static int nTotalObj;
};
int CStudent::nTotalObj = 0;
CStudent::CStudent(char *n, int a) : age(a) {
int nLen = strlen(n);
name = new char[nLen + 1];
strcpy(name, n);
name[nLen] = '\0';
nTotalObj++;
}
CStudent::~CStudent() {
delete[] name;
nTotalObj--;
}
void CStudent::SetAge(int age) {
this->age = age;
}
void main() {
CStudent stu1("张三", 25);
CStudent str2("李四", 26);
cout << "CStudent::nTotalObj=" << CStudent::nTotalObj << endl;
}
```
这样就能保证在对象被销毁时,其成员变量占用的内存也会被释放,避免了内存泄漏的问题。
#include<iostream> #include<string> using namespace std; class Cstudent{ private: int number; string name; static int total; static int actualtotal; public: Cstudent(string name); Cstudent(const Cstudent &p); ~Cstudent(); void showstudent(); int getnumber(){return number;} static int gettotal(){return total;} static int getactualtotal(){return actualtotal;} }; int Cstudent::total =0; int Cstudent::actualtotal=0; Cstudent::Cstudent(string name){ name = name; number = ++total; actualtotal++; } Cstudent::Cstudent(const Cstudent &p){ name = p.name; number = ++total; actualtotal++; } Cstudent::~Cstudent(){ actualtotal--; } void Cstudent::showstudent(){ cout << "student's name is " << name << "\tnumber is " <<number << endl; } int main(){ Cstudent stu[10]; for(int i=0;i<10;i++) stu[i].showstudent(); cout<<Cstudent::gettotal()<<stu[0].getactualtotal()<<endl;//两种访问静态方法 Cstudent *pa = new Cstudent("wang"); pa->showstudent(); delete pa; Cstudent *pb; pb = new Cstudent[10]; for(int i=0;i<10;i++) pb[i].showstudent(); delete[]pb; return 0; }哪里错了
在类的构造函数中,私有成员变量`name`应该使用`this->name`来表示当前对象的`name`成员变量,而不是将函数参数`name`赋值给自身`name`变量。因此,`Cstudent`类的构造函数应该改为:
```
Cstudent::Cstudent(string name){
this->name = name;
number = ++total;
actualtotal++;
}
```
此外,在输出`Cstudent`对象的实际数量时,应该在两个数之间添加空格或者其他分隔符,否则两个数会被拼接在一起,难以区分。因此,`main`函数中输出实际数量的代码应该改为:
```
cout << Cstudent::gettotal() << " " << stu[0].getactualtotal() << endl;
```
以上修改后的完整代码如下:
```
#include<iostream>
#include<string>
using namespace std;
class Cstudent{
private:
int number;
string name;
static int total;
static int actualtotal;
public:
Cstudent(string name);
Cstudent(const Cstudent &p);
~Cstudent();
void showstudent();
int getnumber(){return number;}
static int gettotal(){return total;}
static int getactualtotal(){return actualtotal;}
};
int Cstudent::total =0;
int Cstudent::actualtotal=0;
Cstudent::Cstudent(string name){
this->name = name;
number = ++total;
actualtotal++;
}
Cstudent::Cstudent(const Cstudent &p){
name = p.name;
number = ++total;
actualtotal++;
}
Cstudent::~Cstudent(){
actualtotal--;
}
void Cstudent::showstudent(){
cout << "student's name is " << name << "\tnumber is " <<number << endl;
}
int main(){
Cstudent stu[10];
for(int i=0;i<10;i++)
stu[i].showstudent();
cout << Cstudent::gettotal() << " " << stu[0].getactualtotal() << endl;//两种访问静态方法
Cstudent *pa = new Cstudent("wang");
pa->showstudent();
delete pa;
Cstudent *pb;
pb = new Cstudent[10];
for(int i=0;i<10;i++)
pb[i].showstudent();
delete[]pb;
return 0;
}
```
阅读全文