当前位置: 首页 > article >正文

C++之Person类中调用Date类

main.cpp

#include <iostream>
#include "Person.h"
using namespace std;

int main()
{
    Person myPerson;
    // Person myPerson("S.M.Wang", 070145, "莲花路200号");

    cout << "请输入姓名:" ;
    string name;
    cin >> name;

    cout << "请输入ID:" ;
    int id;
    cin >> id;

    cout << "请输入住址:" ;
    string address;
    cin >> address;

    Date myDate;

    cout << "请输入生日年:" ;
    int year;
    cin >> year;

    cout << "请输入生日月:" ;
    int month;
    cin >> month;

    cout << "请输入生日天:" ;
    int day;
    cin >> day;

    myPerson.setName(name);
    myPerson.setID(id);
    myPerson.setAddress(address);

    myDate.setYear(year);
    myDate.setMonth(month);
    myDate.setDay(day);

    myPerson.setBirthday(myDate); // 调用的形参为类

    myPerson.print();
    return 0;
}

Person.h

#include <iostream>
#include "Date.h"
using namespace std;

class Person
{
public:
    Person();
    Person(string name, int id, string address);
    ~Person();

    void setPerson(string name, int id, string address);
    void setName(string name);
    void setID(int id);
    void setAddress(string address);
    void setBirthday(Date d);

    string getName();
    int getID();
    string getAddress();
    Date getBirthday();

    void print(); // outPutResult
private:
    string Name;
    int ID;
    string Address;
    Date Birthday;
};

Person.cpp

#include "Person.h"
#include <iostream>
using namespace std;

Person::Person()
{
    Name = "S.M.Wang";
    ID = 070145;
    Address = "莲花路200号";
}

Person::Person(string name, int id, string address)
{
    setPerson(name, id, address);
}

Person::~Person()
{
    //cout << "object Destructor is called" << endl;
}


void Person::setPerson(string name, int id, string address)
{
    Name = name;
    ID = id;
    Address = address;
}


void Person::setName(string name)
{
    Name = name;
}

void Person::setID(int id)
{
    ID = id;
}

void Person::setAddress(string address)
{
    Address = address;
}

string Person::getName()
{
    return Name;
}

int Person::getID()
{
    return ID;
}

string Person::getAddress()
{
    return Address;
}


void Person::setBirthday(Date d) // 调用的形参是类
{
    Birthday = d;
}

Date Person::getBirthday() // 返回的是类
{
    return Birthday;
}

void Person::print()
{
    cout << "Name:" << getName() << endl;
    cout << "ID:" << getID() << endl;
    cout << "Address:" << getAddress() << endl;
    
    cout << "Birthday:" << getBirthday().getYear(); // getBirthday()返回的是类,再调用类中的子函数满足cout的返回值。
    cout << " " << getBirthday().getMonth();
    cout << " " << getBirthday().getDay() << endl;
}

Date.h

#include <iostream>

using namespace std;


class Date
{
    friend class Person;
public:
    Date();
    Date(int y, int m, int d);
    ~Date();

    void setYear(int y);
    void setMonth(int m);
    void setDay(int d);

    int getYear();
    int getMonth();
    int getDay();

    void print();
private:
    int Year;
    int Month;
    int Day;

};

Date.cpp

#include "Date.h"
#include <iostream>
using namespace std;

Date::Date()
{
    Year = 0;
    Month = 0;
    Day = 0;
}

Date::Date(int year, int month, int day)
{
    setYear(year);
    setMonth(month);
    setDay(day);
}

Date::~Date()
{
    //cout << "object Destructor is called" << endl;
}


void Date::setYear(int year)
{
    Year = year;
}

void Date::setMonth(int month)
{
    Month = month;
}

void Date::setDay(int day)
{
    Day = day;
}

int Date::getYear()
{
    return Year;
}

int Date::getMonth()
{
    return Month;
}

int Date::getDay()
{
    return Day;
}

void Date::print()
{
    cout << "Year :" << getYear() << endl;
    cout << "Month :" << getMonth() << endl;
    cout << "Day :" << getDay() << endl;
}

Result:

 

 


http://www.kler.cn/news/325503.html

相关文章:

  • 电脑使用adb工具连接手机,全文完整教程
  • MFC - 常用基础控件
  • 高防服务器如何抵御网络攻击?
  • RT-DETR改进策略:BackBone改进|PoolFormer赋能RT-DETR,视觉检测性能显著提升的创新尝试
  • 11.C++程序中的常用函数
  • 【含文档】基于Springboot+Vue的个性化推荐电商平台(含源码+数据库+lw)
  • 【网络安全】公钥密码体制
  • 关于QSizeGrip在ui界面存在布局的情况下的不显示问题
  • 绿色新纪元:光伏技术飞跃与能源体系重塑
  • keil仿真||示波器的使用
  • unixODBC编程(三)查询数据库表中的数据
  • LangChain:构建复杂 NLP 应用的框架
  • ENV | docker 安装使用(简单实操版)
  • Llama 3.2来了,多模态且开源!AR眼镜黄仁勋首批体验,Quest 3S头显价格低到离谱
  • C语言介绍
  • Object Pascal 过程与函数
  • Ubuntu网卡配置
  • rabbitMQ 简单使用
  • 23中设计模式,以及三种常见的设计模式demo
  • 使用::selection改变文字被选中后的颜色
  • 深圳mes制造系统的主要功能
  • WIFI密码默认显示
  • OpenAI员工流失的背后:地盘争夺、倦怠、薪酬要求
  • 大模型+AIGC技术实操:GPT 大模型部署使用 AIGC实战落地方案
  • LeetCode讲解篇之3. 无重复字符的最长子串
  • springboot异常(三):异常处理原理
  • 超详细的华为ICT大赛报名流程
  • golang学习笔记32——哪些是用golang实现的热门框架和工具
  • Android Webview和ScrollView冲突和WebView使用总结
  • 数仓建模:DataX同步Mysql数据到Hive如何批量生成建表语句?| 基于SQL实现