请问为什么b2的构造函数执行完,obj的num值是8?
- 关于c++的一个问题,构造函数number(ElemType n=0):num(n){}中的num(n)是指什么意思
- 类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为obj赋予初始值(内容自定).
- 一道c++题
- c++程序设计问题

关于c++的一个问题,构造函数number(ElemType n=0):num(n){}中的num(n)是指什么意思
这个是基类构造函数有参数的情况 number是num类的派生类。num(n)的意思是调用基类的构造函数 并将前面的n的值作为参数传递给基类的构造函数
比如 定义 number *xn=new number(10);
此时会做两件事 首先将会调用num(10)构建基类 然后再执行number(10)自己的方法
类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为obj赋予初始值(内容自定).
我会采用深复制构造函数实现Copy对象,而不是重载=运算符。原因:因为使用了动态内存分配,当被Copy的对象使用完毕时析构函数将释放其所申请的内存空间,但Copy的对象还在使用中,此时释放内存将造成程序崩溃。 #includeusing namespace std;namespace ns{ class Person{ public: Person(){} Person(char *name, int age,char *sex){ this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->sex = new char[strlen(sex) + 1]; strcpy(this->sex, sex); this->age = age; } // 深复制构造函数 Person(const Person &obj){ this->name = new char[strlen(obj.name) + 1]; strcpy(this->name, obj.name); this->sex = new char[strlen(obj.sex) + 1]; strcpy(this->sex, obj.sex); this->age = obj.age; } ~Person(){ delete this->sex; delete this->name; } void print(){ cout << "name=" < this->name << "\tage=" < this->age << "\tsex=" < this->sex << endl; } private: char *name; int age; char *sex; };}int main(int argc, char *argv[]){ ns::Person person("James", 20, "男"); person.print(); ns::Person person2("Lisa", 22, "女"); person2.print(); ns::Person deepCopy=person2; deepCopy.print(); return 0;}
一道c++题
#include
using namespace std;
class MyClass
{
public:
MyClass(int n){number = n;}
//拷贝构造函数
MyClass(MyClass &other)
{
static count=0;//加入这两行可计算,结果是4
 cout<<++count<  number=other.number; }  ~MyClass(){}  private:  int number;  };  MyClass fun(MyClass p)  {  MyClass temp(p); //这里1次 return temp;  }  int main()  {  MyClass obj1(10), obj2(0); //这里2次 MyClass obj3(obj1); //这里1次 obj2=fun(obj3);  return 0;  }  //结果是4 编译运行通过,但是哥们,您的编程风格需要改善。程序易读性不好。这样不方便排错的。 #include  using namespace std; class date { private:  int year;int month;int day; public:  date(){}  date(int y,int m,int d)   {   year=y;   month=m;   day=d;  }  void set()  {cin>>year>>month>>day;  }  void display()  {   cout<  } }; class person { private:  int num;  char sex[3];  date birthday;  char ID[18];  static int howmanys;//静态数据成员 public:  person(){}  person(int n,int y,int m,int d,char id[18],char s[3]):birthday(y,m,d)  {   num=n;   strcpy(sex,s);   strcpy(ID,id);  }  person(person &p)  {   num=p.num;      strcpy(sex,p.sex);   birthday=p.birthday;   strcpy(ID,p.ID);  }  void input()  {   cout<<"录入数据"<   cout<<"编号:";   cin>>num;   cout<<"性别:m or n\n";   cin>>sex;   sex[3]='\0';   cout<<"生日:";   birthday.set();   cout<<"身份证号:";   cin>>ID;   ID[18]='\0';   cout<   howmanys++;  }  void output()  {   cout<<"编号:"<   cout<<"性别:"<   cout<<"生日:";birthday.display();cout<   cout<<"身份证号:"<  }  static void gethowmany(){cout<<"这里共有"<  ~person()  {   cout<  } }; class teacher:public person {private: char principalship[11]; char department[21]; public:  void setprincipalship(){cout<<"职务:"<  void setdepartment(){cout<<"部门:"<     void outputprincipalship(){cout<<"职务:"<  void outputdepartment(){cout<<"部门:"<  teacher(){}; } ; /*acher::teacher(int n,int y,int m,int d,char id[18],char s[3],char pr[11],char dep[21]):person(int n,int y,int m,int d,char id[18],char s[3]) {strcpy(principalship,pr); strcpy(department,dep); }*/ int person::howmanys=0; int main() {  teacher t[2]; for(int i=0;i<2;i++) {  t[i].input();t[i].setprincipalship();t[i].setdepartment();  t[i].output();t[i].outputprincipalship();t[i].outputdepartment(); }//对象数组  person::gethowmany();//输出人员总数 return 0; }c++程序设计问题
