Ad Code

Responsive Advertisement

Write a program that defines a class date and overload ==, < and > operators to compare two dates.








2) Write a program that defines a class date and overload ==, < and > operators to compare two dates.
Note:
    It returns 0 if both dates are equal.
    It returns negative value if first date is less than the second one.
    It returns positive value if first date is greater than the second one.  :


#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class A{
 char name[15],name1[15],n3[2];
 public:

 void set_name1(char *n){
  strcpy(name,n);
 } 
 void set_name2(char *n1){
  strcpy(name1,n1);
 }
 int get_name1(){
 int i=0;
  for(int k=0; k<strlen(name); k++){
   if(name[k]>='0' && name[k]<='9'){
    n3[0]=name[k];
    i=i*10+(atoi(n3)); 
   }
  }
  return i;
 }

 int get_name2(){
  int j=0;
  for(int k=0; k<strlen(name1); k++){
   if(name1[k]>='0' && name1[k]<='9'){
    n3[0]=name1[k];
    j=j*10+(atoi(n3)); 
   }
  }
  return j;
 }
};
A operator ==(A obj,A obj2){
 if(obj.get_name1()==obj2.get_name2()){
  cout<<"\n\nDATE is equal\n\n";
 }
 else{
  cout<<"\n\nDate are not equal\n\n";
 }
}
A operator >(A obj,A obj2){
 if(obj.get_name1()>obj2.get_name2()){
  cout<<"\n\nFirst date is greater then second\n\n";
 }
}
A operator <(A obj,A obj2){
 if(obj.get_name1()<obj2.get_name2()){
  cout<<"\n\nFirst date is smaller then second\n\n";
 }
}
int main(){
 A obj,obj2;
 cout<<"Enter a date :";
 char name[15],name1[15];
 cin>>name;
 obj.set_name1(name);
 cout<<"\nEnter a second date :";
 cin>>name;
 obj2.set_name2(name);
 obj==obj2;
 obj>obj2;
 obj<obj2;
 
}



out put:
 Enter a date 12-4-2019
 Enter a second date 12-4-2019


DATE is equal

If you like code with vibhu  and would like to contribute, you can also write an article using   This link :

https://docs.google.com/forms/d/e/1FAIpQLScAmvlPvNUz35R-G0nc_zpRVP3o8xlhtFgC3aKPyLetX_RyXg/viewform?pli=1


OR  mail your article to codewithvibhu@gmail.com.  See your article appearing on the code with vibhu main page and help other students. 

Post a Comment

0 Comments