c++默认构造参数,有参构造函数和拷贝构造函数的使用示例

#include <iostream>
#include<cstring>
using namespace std;
class Student

public:
 Student( );  //默认构造函数
 Student(int id, char n[ ], bool s, float w);  //有参构造函数
 Student(const Student &s);  //拷贝构造函数
 void setData(int, char [ ], bool, float);
 void show( );
private:
 int ID; 
char name[9];
 bool sex; 
float weight; 
};
Student::Student( )
 { ID=0;
 sex=true;  
 weight=0.0;
strcpy(name, "none");
 }
Student::Student(int id, char n[ ], bool s, float w)
{
      ID=id;
 sex=s;
      weight=w;
     strcpy(name, n);
     }
Student::Student(const Student &s)
{
 ID=s.ID;
sex=s.sex;
weight=s.weight;
   strcpy(name, s.name);
}
void Student::setData(int Id, char Name[ ], bool Sex, float weight)
{ ID = Id;
 strcpy(name, Name);
 sex = Sex;
 this->weight = weight;
}
void Student::show( )
{cout << "学号:" << ID << "\t姓名:" << name << "\t性别:" << (sex?"男":"女") << "\t体重:" << weight << endl;
}
int main()
{
 Student s1;
 Student s2(90001,"Lily",false,45),s3=s2;
 s1.show( );
 s2.show( );
 s3.show( );
 s1.setData(90002,"Tom",true,60);
    s1.show( );
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值