1.program to add any two objects using operator overloading
2.program to add any two objects using constructors
3.program to add any two objects using binary operator
4.program to add any two objects using unary operator

Answer Posted / raj

Create a class called 'time' that has three integer data
members for hours, minutes and seconds, define a member
function to read the values, member operator function to add
time, member function to display time in HH:MM:SS format.
Write a main function to create two time objects, use
operator function to add them and display the results in
HH:MM:SS format.


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class time
{
int h,m,s;
public:
void read();
void print();
time operator+(time t2);
};
void time::read()
{
cout<<"\nEnter hour,minutes and seconds\n";
cin>>h>>m>>s;
}
void time::print()
{
cout<<"\nTime is-> "<<setfill('0')<<setw(2)<<h;
cout<<":"<<setfill('0')<<setw(2)<<m;
cout<<":"<<setfill('0')<<setw(2)<<s<<endl;
}
time time::operator+(time t2)
{
time t;
t.h=h+t2.h;
t.m=m+t2.m;
t.s=s+t2.s;
return t;
}
void main()
{
clrscr();
time t1,t2,t3;
t1.read();
t1.print();
t2.read();
t2.print();
t3=t1+t2;
cout<<"\nTime1+ Time2:\n";
t3.print();
getch();
}

This link may help

http://programscpp.blogspot.in/2012/08/operator-overloading-adding-two-time.html

Is This Answer Correct ?    20 Yes 11 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

write a program to convert temperature from fa height into celcius and vise versa,use modular programming

2431


Definition of priority queue was given. We have to implement the priority queue using array of pointers with the priorities given in the range 1..n. The array could be accessed using the variable top. The list corresponding to the array elements contains the items having the priority as the array index. Adding an item would require changing the value of top if it has higher priority than top. Extracting an item would require deleting the first element from the corresponding queue. The following class was given: class PriorityQueue { int *Data[100]; int top; public: void put(int item, int priority); // inserts the item with the given priority. int get(int priority); // extract the element with the given priority. int count(); // returns the total elements in the priority queue. int isEmpty(); // check whether the priority queue is empty or not. }; We had to implement all these class functions.

4388


Create a program to read two random data set in two files named data1.txt and data2.txt manifold contains integer numbers, whereas data2.txt file contains the float type numbers. Simpanlahmasing each into 2 pieces of data that is an array of type integer array and an array of type float, then calculate the average numbers in the second array.

2138


write a program to calculate the amount of investment after a period n years if the principal investors was p and interest is calculated using compound interest,formular=a=p(1+r)^n

2361


how to diplay a external image of output on winxp by using c & c++,

2927






how to take time as input in the format (12:02:13) from user so that controls remains between these columns?

1809


3. Program to find the Sum of give series. a. (1)+(1+2)+(1+2+3)+(1+2+3+4)+……………………………….. b. 1/1+1/9+1/25+1/49+……………...

4343


Given a table of the form: Product Sold on A 1/1/1980 B 1/1/1980 C 1/1/1980 A 1/1/1980 B 1/1/1980 C 2/1/1980 A 2/1/1980 There are 30 products and 10,000 records of such type. Also the month period during which sales happened is given to u. Write the program to display the result as: Product Month No. of copies A January 12 A February 15 A March 27 B January 54 B February 15 B March 10 C January 37

2185


We need to write the function to check the password entered is correct or not based on the following conditions.. a) It must have atleast one lower case character and one digit. b)It must not have any Upper case characters and any special characters c) length should be b/w 5-12. d) It should not have any same immediate patterns like abcanan1 : not acceptable coz of an an pattern abc11se: not acceptable, coz of pattern 11 123sd123 : acceptable, as not immediate pattern adfasdsdf : not acceptable, as no digits Aasdfasd12: not acceptable, as have uppercase character

3946


Given a table of the form: Product Sold on A 1/1/1980 B 1/1/1980 C 1/1/1980 A 1/1/1980 B 1/1/1980 C 2/1/1980 A 2/1/1980 There are 30 products and 10,000 records of such type. Also the month period during which sales happened is given to u. Write the program to display the result as: Product Month No. of copies A January 12 A February 15 A March 27 B January 54 B February 15 B March 10 C January 37

2039


Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)

3261


What output does the following code generate? Why? What output does it generate if you make A::Foo() a pure virtual function? class A { A() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; class B : public A { B() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; int main(int, char**) { A objectA; B objectB; return 0; }

635


U hv to enter a range from a and b and search hw many no. of times a pattern n. occurs between the range a and b. Eg :i/p:enter range :0 100 Enter pattern: 13 o/p: the no. times 13 occurred betwwn 0 to 100:1 Eg :i/p:enter range :100 1000 Enter pattern: 13 o/p: the no. times 13 occurred betwwn 100 to 1000: (in this 13,113,131,132,133…139,213,313,…913 all these will be counted)

2116


write a program that prompt the user to enter his height and weight,then calculate the body mass index and show the algorithm used

4297


output for printf("printf");

1975