C++ 实验一 类与对象

  • Post author:
  • Post category:其他


1.设计一个 Tree (树)类,有成员 ages (树龄),成员函数 grow(int years) ages 加上years,age( )显示 tree 对象的 ages 的值。

2.设计一个用于人事管理的People类。考虑到通用性,可以只抽象出所有人员都具有的属性:num(编号),name(姓名) ,birth(生日),id(身份证号)等等。其中“生日”定义为一个“日期”类内嵌子对象。用成员函数实现对人员的信息录入和显示。

Tree.h

#ifndef SHIYAN1_2__TREE_H
#define SHIYAN1_2__TREE_H

#include<iostream>
using namespace std;

class Tree
{
public:
    Tree();
    int grow(int years);
    void age();
private:
    int ages;

};


#endif //SHIYAN1_2__TREE_H

Tree.cpp

#include "Tree.h"

Tree::Tree() {}
int Tree::grow(int years)
{
    cout<<"请输入树龄ages:"<<endl;
    cin>>ages;
    ages=ages+years;
    return ages;
}
void Tree::age()
{
    cout<<"树龄="<<ages<<endl;
}

Date.h

#ifndef SHIYAN1_2__DATE_H
#define SHIYAN1_2__DATE_H

#include<iostream>
using namespace std;

class Date{
private:
    int year;
    int mon;
    int day;
public:
    Date();
    Date(int year,int mon,int day);
    Date(const Date& d);
    void setData(int y,int m,int d);
    void getDate();
    ~Date();
};


#endif //SHIYAN1_2__DATE_H

Date.cpp

#include "Date.h"

Date::Date(){}
Date::Date(int year,int mon,int day)
{
    Date::year=year;
    Date::mon=mon;
    Date::day=day;
}
Date::Date(const Date &d)
{
    year=d.year;
    mon=d.mon;
    day=d.day;
}
void Date::setData(int y, int m, int d)
{
    year=y;
    mon=m;
    day=d;
}
void Date::getDate()
{
    cout<<year<<"年"<<mon<<"月"<<day<<"日"<<endl;
}
Date::~Date()
{
    //cout<<"输入完成"<<endl;
}

People.h

#include<iostream>
#include <cstring>
#include "Date.h"

using namespace std;

class People {
private:
    int Num;
    char Name[10];
    char Id[18];
    Date birth;
public:
    int y,m,d;
    People();
    People(int num, char name[10], char id[18], int y, int m, int d);

    People(People& p);
    void inPut();
    void outPut();
    ~People();
};

#endif //SHIYAN1_2__PEOPLE_H

People.cpp

#include "People.h"

People::People() {}
People::People(int num, char *name, char *id, int y, int m, int d)
{
    Date birth1(y,m,d);
    Num=num;
    strcpy(Name,name);
    strcpy(Id,id);
    People::birth=birth1;
}

People::People(People &p)
{
    Num=p.Num;
    strcpy(Name,p.Name);
    strcpy(Id,p.Id);
    birth=p.birth;
}
void People::inPut()
{
    int y,m,d;
    cout<<"录入:"<<endl;
    cout<<"编号:";
    cin>>Num;
    cout<<"姓名:";
    cin>>Name;
    cout<<"身份证号:";
    cin>>Id;
    cout<<"生日:";
    cin>>y>>m>>d;
    birth.setData(y,m,d);
}
void People::outPut()
{
    cout<<"编号:"<<Num<<endl;
    cout<<"姓名:"<<Name<<endl;
    cout<<"身份证号:"<<Id<<endl;
    cout<<"生日:";
    birth.getDate();
    cout<<endl;
}
People::~People()
{
    //cout<<" "<<Num<<"号已录入"<<endl;
}

main.cpp

#include <iostream>
#include "People.h"
#include "Date.h"
#include "Tree.h"

void tree()
{
    cout<<"tree类测试"<<endl;
    Tree tree1;
    Tree tree2;
    int year1;
    cout<<"请输入年份years:"<<endl;
    cin>>year1;
    tree1.grow(year1);
    tree1.age();

    cout<<"请输入年份years:"<<endl;
    cin>>year1;
    tree2.grow(year1);
    tree2.age();
}
void people()
{
    //Data测试
    cout<<"Data测试:"<<endl;
    int y,m,d;
    cout<<"请输入日期:";
    cin>>y>>m>>d;
    Date data1(y,m,d);
    data1.getDate();
    cout<<"修改日期:"<<endl;
    cin>>y>>m>>d;
    data1.setData(y,m,d);
    data1.getDate();

    cout<<"请输入日期:";
    cin>>y>>m>>d;
    Date data2(y,m,d);
    data2.getDate();
    cout<<"修改日期:"<<endl;
    cin>>y>>m>>d;
    data2.setData(y,m,d);
    data2.getDate();

    cout<<"拷贝:"<<endl;
    Date d3(data2);
    d3.getDate();


    //People测试
    cout<<"\n"<<"People测试:"<<endl;
    int num;
    char name[10],id[18];
    cout<<"录入:"<<endl;
    cout<<"编号:";
    cin>>num;
    cout<<"姓名:";
    cin>>name;
    cout<<"身份证号:";
    cin>>id;
    cout<<"生日:";
    cin>>y>>m>>d;
    //23cin>>num>>name>>id>>y>>m>>d;
    Date birthday;
    People p1(num,name,id,y,m,d);

    p1.outPut();

    People p2;
    p2.inPut();
    p2.outPut();

    People p3(p2);
    p3.outPut();
}

int main()
{
    tree();
    people();
}



版权声明:本文为m0_73991668原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。