C++。处理在构造函数中初始化的类中的私有成员变量时出错

发布于 2025-01-11 07:45:05 字数 2811 浏览 0 评论 0原文

我现在正在大学学习如何使用 C++ 课程。为了实践,我们被告知要创建一个具有私有成员变量的 DateC 类:日、月和年,以及我们需要编写的一些其他公共函数来打印日期并修改它。

这是到目前为止我的代码:

prog.cpp(主要)

#include <iostream>
#include "date.h"

//ASK HOW THE CONTENTS OF DATE.CPP ARE ACCESSED IN THIS FILE SEEING AS ITS NOT INCLUDED EXPLICITLY LIKE DATE.H

int main() 
{
    int d,m,y;
    std::cin >> d; //>> std::endl;
    std::cin >> m; //>> std::endl;
    std::cin >> y; //>> std::endl;
    //^ Takes input for the d,m,y

    DateC date(d,m,y);
    //^ Creates an object using the DateC class passing d,m,y into the member variables using the constructor that has parameters 

    std::cout << date.getDay() << "/" << date.getMonth() << "/" << 
    date.getYear() << std::endl;

    return 0;
}

date.cpp(所有成员函数的文件)

#include <iostream>
#include "date.h"

//DateC::DateC() {}
//^ using this line the values for d,m,y will be set to 0 which isnt really a suitable default for a date.
//something more reasonable would be 1/1/2015

DateC::DateC(): day(1), month(1), year(2015) {}

DateC::DateC(int d, int m, int y): day(d), month(m), year(y)
{    
   try 
   {
        int day = d;
        if (0<day || day < 32) {
        std::cout << "Valid day" << std::endl;
        }
        else {
            throw (day);
        }
    }
    catch (int Num) {
        std::cout << "Invalid day" << std::endl;
     }
}

int DateC::getDay()
{
    return this->day;
    //this is a pointer to the object that called the getDay() function, in this case date. (check prog.cpp to confirm this)
}

int DateC::getMonth()
{
    return this->month;
    //this is a pointer to the object that called the getMonth() function, in this case date. (check prog.cpp to confirm this)
}

int DateC::getYear()
{
    return this->year;
    //this is a pointer to the object that called the getYear() function, in this case date. (check prog.cpp to confirm this)
}

date.h(头文件)

#ifndef DATEC_H
#define DATEC_H

class DateC {
    private:
        int day, month, year;
    public:
        DateC();
        DateC(int d, int m, int y);
        ~DateC() {};
        int getDay();
        int getMonth();
        int getYear();
        int setDate(int d, int m, int y);
        int printDate();
};

#endif /*DATEC_H*/

注意:我还没有实现所有这些函数。

所以我正在努力解决的是当天的错误处理(还不是月份和年份,因为我只是看看我现在是否可以错误处理这一天)。

我正在尝试使用异常在 date.cpp 文件中进行错误处理,条件是如果日期小于 0 或大于 32,则使用条件应该无效:

if (0<day || day < 32)

此条件似乎允许日期的任何值通过但是当我将其更改为

if (0<day)

它时,它工作得很好,

所以我的第一个问题是我在这个条件中做错了什么。

那么我的下一个问题是我将如何制作它,以便如果输入了无效的日期,程序将使用户再次输入日期,直到他们拥有正确的格式。

Im learning how to use classes in C++ in university right now. To practice we have been told to make a DateC class that has the private member variables: day, month and year with some other public functions that we need to write to print the date and modify it.

Here's my code so far:

prog.cpp (the main)

#include <iostream>
#include "date.h"

//ASK HOW THE CONTENTS OF DATE.CPP ARE ACCESSED IN THIS FILE SEEING AS ITS NOT INCLUDED EXPLICITLY LIKE DATE.H

int main() 
{
    int d,m,y;
    std::cin >> d; //>> std::endl;
    std::cin >> m; //>> std::endl;
    std::cin >> y; //>> std::endl;
    //^ Takes input for the d,m,y

    DateC date(d,m,y);
    //^ Creates an object using the DateC class passing d,m,y into the member variables using the constructor that has parameters 

    std::cout << date.getDay() << "/" << date.getMonth() << "/" << 
    date.getYear() << std::endl;

    return 0;
}

date.cpp (file for all the member functions)

#include <iostream>
#include "date.h"

//DateC::DateC() {}
//^ using this line the values for d,m,y will be set to 0 which isnt really a suitable default for a date.
//something more reasonable would be 1/1/2015

DateC::DateC(): day(1), month(1), year(2015) {}

DateC::DateC(int d, int m, int y): day(d), month(m), year(y)
{    
   try 
   {
        int day = d;
        if (0<day || day < 32) {
        std::cout << "Valid day" << std::endl;
        }
        else {
            throw (day);
        }
    }
    catch (int Num) {
        std::cout << "Invalid day" << std::endl;
     }
}

int DateC::getDay()
{
    return this->day;
    //this is a pointer to the object that called the getDay() function, in this case date. (check prog.cpp to confirm this)
}

int DateC::getMonth()
{
    return this->month;
    //this is a pointer to the object that called the getMonth() function, in this case date. (check prog.cpp to confirm this)
}

int DateC::getYear()
{
    return this->year;
    //this is a pointer to the object that called the getYear() function, in this case date. (check prog.cpp to confirm this)
}

date.h (header file)

#ifndef DATEC_H
#define DATEC_H

class DateC {
    private:
        int day, month, year;
    public:
        DateC();
        DateC(int d, int m, int y);
        ~DateC() {};
        int getDay();
        int getMonth();
        int getYear();
        int setDate(int d, int m, int y);
        int printDate();
};

#endif /*DATEC_H*/

Note: I haven't implemented all of these functions yet.

So what im struggling with is the error handling of the day(not month and year yet because i'm just seeing if i can error handle the day right now).

I am trying to do the error handling in the date.cpp file using an exception with the condition that if the day is less than 0 or greater than 32 it should be invalid using the conditional:

if (0<day || day < 32)

This condition seems to let any value for day through but when I change it to

if (0<day)

it works just fine

so my first question is what am i doing wrong in this conditional.

Then my next question is how would I go about making it so that if an invalid day is entered the program would get the user to enter the date again until they have the correct format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2025-01-18 07:45:05

您需要使用 &&运算符而不是||。如果您使用“或”运算符,您将始终输入条件。例如,如果日期是-5,则它一定是无效的。 0<-5 返回 false,可以,但 -5<32 返回 true,所以结果为 true。

You need to use && operator instead of ||. If you use "or" operator you will always enter the condition. For example if the day is -5, it must be invalid. 0<-5 return false, that is ok, but -5<32 returns true, so result is true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文