×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to check the year is leap year or not using class

Given a year, we have to check whether it is a leap year or not using class and object approach.
Submitted by Shubh Pachori, on August 04, 2022

Problem statement

Given a year, we have to check whether it is a leap year or not using class and object approach.

Example

Input: 
Enter Year: 1994
Output: 1994 is not a leap year.

C++ Code to check the year is leap year or not using class and object approach

#include <iostream>
using namespace std;

// create a class
class Leap {
  // declare private data member
 private:
  int year;

  // a public function with a int type parameter
 public:
  void leapyear(int y) {
    // copying parameter value in data members
    year = y;

    // if condition to check leap year in centuries year like 2000,2100
    if (year % 400 == 0) {
      cout << year << " is a leap year.";
    }

    // if condition to check if a century year is a non leap year
    else if (year % 100 == 0) {
      cout << year << " is not a leap year.";
    }

    // if condition to check leap year in non century year
    else if (year % 4 == 0) {
      cout << year << " is a leap year.";
    }

    // all other years are not leap years
    else {
      cout << year << " is not a leap year.";
    }
  }
};

int main() {
  // create a object
  Leap L;

  // int type variable to store year
  int y;

  cout << "Enter Year: ";
  cin >> y;

  // calling function using object
  L.leapyear(y);
  return 0;
}

Output

Enter Year: 2022
2022 is not a leap year.

Explanation

In the above code, we have created a class Leap, one int type data member year to store the year, and a public member function leapyear() to check the given year.

In the main() function, we are creating an object L of class Leap, reading a year by the user, and finally calling the leapyear() member function to check the given year whether it is a leap year or not. The leapyear() function contains the logic to check the given year and printing the result.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.