Ad Code

Responsive Advertisement

Write a program to create class to read and add two times using binary plus (+) operator overloading.










#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class A{
 char tim[15],t[3],t3[4];
 int t1=0,t2=0;
 public:
 void set_time(){
  cout<<"Enter a time :";
  cin>>tim;
 }
 A operator +(A obj){
  for(int i=0; i<strlen(tim); i++){
   if(tim[i]>='0' && tim[i]<='9'){
    t[0]=tim[i];
    t1=t1*10+(atoi(t));
   }
  }
  for(int i=0; i<strlen(obj.tim); i++){
   if(obj.tim[i]>='0' && obj.tim[i]<='9'){
    t[0]=obj.tim[i];
    t2=t2*10+(atoi(t));
   }
  }
   cout<<"\n"<<t1+t2;
  if(t1+t2>1200){
   if((t1+t2)/100>12){
    if(((t1+t2)%100)>60){
     cout<<"\nhour : "<<((t1+t2)/100)+1-12;
     cout<<"\tminute : "<<((t1+t2)%100)-60;
    }else{
     cout<<"\nhour :"<<((t1+t2+1)/100);
     cout<<"\tminute : "<<((t1+t2)%100);
    }
   }
   else{
    if(((t1+t2)%100)>60){
     cout<<"\nhour : "<<((t1+t2)/100)+1-12;
     cout<<"\tminute : "<<((t1+t2)%100)-60;
    }else{
     cout<<"\nhour : "<<((t1+t2+1)/100);
     cout<<"\tminute : "<<((t1+t2)%100);
    }
   }
  }if(t1+t2<=1200){
   if(((t1+t2)%100)>60){
     cout<<"\nhour :"<<((t1+t2)/100)+1;
     cout<<"\tminute :"<<((t1+t2)%100)-60;
    }else{
     cout<<"\nhour :"<<((t1+t2)/100);
     cout<<"\tminute :  "<<((t1+t2)%100);
    }
   }
  }
};
int main(){
 A obj,obj1;
 //system("clear");
 obj.set_time();
 obj1.set_time();
 obj+obj1;
}



out put:
Enter a time 6:15
Enter a time 2:10

hour:8 minute:25
 



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