C++第二章课后习题

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include"stdafx.h"

#include

using namespace std;

class Date

{

private:

int year;

int month;

int day;

public:

void Set(int y,int m,int d)

{year=y;month=m;day=d;}

void Show()const

{cout<

int main()

{

Date d;

d.Set(2016,3,31);

d.Show();

system("PAUSE");

return 0;

}

Result:

#include"stdafx.h"

#include

using namespace std;

class Time

{

private:

int hour;

int minute;

int second;

public:

Time(int h=0,int m=0,int s=0):

hour(h),minute(m),second(s){}

void Set(int h,int m,int s){

hour=h;minute=m;second=s;

}

void Show()const{

cout<

}

};

int main()

{

Time t;

t.Set(14,40,56);

t.Show();

system("PAUSE");

return 0;

}

Result:

class People

{

private:

char name[3];int age;int height;double weight;static int num; public:

People(char nm[]="小明",int a=18,int h=171,int w=150):

age(a),height(h),weight(w){strcpy(name,nm);num++; }

void Eatting(){weight++;}

void Sporting(){height++;}

void Show()const

{ cout<<"个人信息:"<

cout<<"年龄"<

cout<<"体重"<

static void ShowNum()

{ cout<<"人数"<

};

int People::num=0;

int main()

{

People p1;

p1.Show();

People p2("赵一奇",20,182,155);

p2.Eatting();

p2.Sporting();

p2.Show();

People::ShowNum();

system("PAUSE");

return 0;

}

Result:

#include"stdafx.h"

#include

using namespace std;

class Employee

{

protected:

int num;

char name[18];

char sex[3];

int wage;

static int count;

static int totalWage;

public:

Employee(int nu,char nm[],char sx[],int wg):num(nu),wage(wg) {

strcpy(name,nm);strcpy(sex,sx);count++;totalWage+=wg;

};

void ShowBase()

{

cout<<"个人基本信息:"<

cout<<"学号:"<

cout<<"姓名:"<

cout<<"性别:"<

cout<<"工资:"<

}

static void ShowStatic()

{

cout<<"人数:"<

cout<<"总工资:"<

}

};

int Employee::count=0;

int Employee::totalWage=0;

int _tmain(int argc, _TCHAR* argv[])

{

Employee e1(10000,"张三","男",1500);

e1.ShowBase();

Employee e2(100001,"李四","男",2000);

e2.ShowBase();

相关文档
最新文档