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
Answers were Sorted based on User's Feedback
Answer / vikas
/*Program to add two object using operator overloading */
/* vikas */
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
class Stadd
{
private:
char p[20];
public:
void set(char *k)
{
strcpy(p,k);
}
Stadd operator+(Stadd c)
{
Stadd l;
strcpy(l.p,p);
strcat(l.p,c.p);
return l;
}
void display()
{
cout<<"The string is = "<<p;
}
};
void main()
{
char m[]="vikas";
char n[]="kumar";
Stadd ws,wt,wu;
ws.set(m);
wt.set(n);
wu=ws+wt;
wu.display();
getch();
}
Is This Answer Correct ? | 38 Yes | 18 No |
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 |
Display Pattern: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 …
How to swap two ASCII numbers?
write a function – oriented program that calculates the sum of the squares from 1 to n. thus, if the input is 3, the output is 14
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
write a proram using exceptional handling create a error & display the message "THERE IS AN ERROR?... PLEASE RECTIFY"?
What output does this program generate as shown? Why? class A { A() { cout << "A::A()" << endl; } ~A() { cout << "A::~A()" << endl; throw "A::exception"; } }; class B { B() { cout << "B::B()" << endl; throw "B::exception"; } ~B() { cout << "B::~B()"; } }; int main(int, char**) { try { cout << "Entering try...catch block" << endl; A objectA; B objectB; cout << "Exiting try...catch block" << endl; } catch (char* ex) { cout << ex << endl; } return 0; }
i really need help about this.. write a program to display the set of odd and even numbers separately. find the highest and lowest value of the given numbers.
write a program that calculate the volume of cylinder after user enters radius and height and show the algorithm used
1 Answers Jomo Kenyatta University,
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!!!)
using friend function find the maximum number from given two numbers from two different classes.write all necessary functions and constructor for the classes.
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
Write a program using one dimensional array that accept five values from the keyboard. Then it should also accept a number to search. This number is to be searched if it among the five input values. If it is found, display the message “Search number is found!” otherwise, display “Search is lost”. Example: Enter 5 numbers: 10 15 20 7 8 Enter number to search: 7 Search number is found!
2 Answers College School Exams Tests,