#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; }
时间: 2024-03-31 12:37:27 浏览: 44
这是一个简单的 C++ 程序,定义了一个名为 CStudent 的类,包含了一个构造函数、一个析构函数、一个静态成员变量 nTotalObj 和一个静态成员函数 SetAge。程序的主函数 main 中创建了两个 CStudent 对象 stu1 和 stu2,输出 nTotalObj 的值。这个值表示在程序运行过程中创建的 CStudent 对象的数量。输出结果应该是:CStudent::nTotalObj=2。
相关问题
#include<iostream> using namespace std; class CStudent { private: char name[20]; int num; int Chinese, Physical, History; public: CStudent(); //无参构造函数的声明 CStudent(char* pName, int n, int C, int P, int History) ; //有参构造函数的声明 CStudent(const CStudent &stu) //拷贝构造函数 { strcpy(name, stu.name); num = stu.num; Chinese = stu.Chinese; Physical = stu.Physical; this->History = stu.History; } ~CStudent() {} char* getName() { return name; } int getNum() { return num; } int getChinese() { return Chinese; } int getPhysical() { return Physical; } int getHistory() { return History; } void operator=(CStudent stu); }; #include"student.h" #include <string> CStudent::CStudent(char *pName, int n, int C, int P, int History) { strcpy(name, pName); num = n; Chinese =C; Physical = P; this->History = History; } void CStudent::operator=(CStudent stu) { strcpy(name, stu.name); num = stu.num; Chinese = stu.Chinese; Physical = stu.Physical; History = stu.History; } int main() { cout << "用不带参数的构造函数创建对象:" << endl; CStudent s1; cout << sizeof(s1) << endl; cout << s1.getName() << " " << s1.getNum() << " " << s1.getChinese() << " " << s1.getHistory() << " " << s1.getPhysical() << endl; cout << "用带参数的构造函数创建对象:" << endl; CStudent s2("顾斌", 221404205, 120, 98, 70); cout << sizeof(s2) << endl; cout << s2.getName()<< "顾斌" << s2.getNum() << " " << s2.getChinese() << " " << s2.getHistory() << " " << s2.getPhysical() << endl; cout << "用拷贝初始化构造函数创建对象:" << endl; CStudent s3(s2); cout << sizeof(s3) << endl; cout << s3.getName()<< " " << s3.getNum() << " " << s3.getChinese() << " " << s3.getHistory() << " " << s3.getPhysical()<< endl; cout << "用=给对象赋值:" << endl; CStudent s4; s4 = s2; cout << s4.getName()<< " " << s4.getNum() << " " << s4.getChinese() << " " << s4.getHistory() << " " << s4.getPhysical() << endl; return 1; }
这段代码定义了一个名为 CStudent 的类,其中包括了无参构造函数、有参构造函数、拷贝构造函数和一个重载了赋值运算符的函数。主函数中创建了四个 CStudent 类的对象,并对这些对象进行了不同的操作。
在这段代码中,CStudent 类的成员变量有 name、num、Chinese、Physical 和 History,分别表示学生的姓名、学号、语文成绩、物理成绩和历史成绩。CStudent 类的成员函数包括了构造函数、拷贝构造函数、析构函数、获取成员变量值的函数和重载赋值运算符的函数。其中,构造函数可以有无参和有参两种形式,拷贝构造函数用于将一个已有对象的值赋给新创建的对象,赋值运算符的函数用于将一个对象的值赋给另一个对象。
在主函数中,首先创建了一个不带参数的 CStudent 类对象 s1,然后输出该对象的成员变量值。接着创建了一个带参数的 CStudent 类对象 s2,并输出其成员变量值。然后又创建了一个拷贝初始化的 CStudent 类对象 s3,并输出其成员变量值。最后,将 s2 对象的值赋给了 s4 对象,并输出 s4 的成员变量值。
#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;
}
```
阅读全文