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 |
An array of size 5X5 is given to us. The elements from 1 to 25 are to be inserted in the array, such that starting from a particular position for an element i, the next element i+1can be inserted only at the mentioned positions (u,v), and if these all positions are occupied then it returns giving a count of how many positions have been occupied in the array: (u,v) = (x+/-3 , y) (u,v) = (x , y+/-3) (u,v) = (x+/-2 , y+/-2). Example: if the starting element is 1 with the given positions (1,2), then next element 2 can be placed at any one of the positions marked with *. _ _ _ _ _ 1 _ _ _ * _ _ _ _ _ _ _ * _ _ * _ _ _ _
How do I store linked list datas into an array?
Find the maximum product of three numbers in an array? Eg. 9,5,1,2,3 Max product= 9*5*3= 135 The array can hav negative numbers also..
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
A string of charaters were given. Find the highest occurance of a character and display that character. eg.: INPUT: AEGBCNAVNEETGUPTAEDAGPE OUTPUT: E
swap prog
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
Implement a command console for changing settings on a particular object. The command console should allow you to enter a string and will return the response (very similar to a terminal session). The commands are as follows: SET propertyname=newvalue will change the target object’s member named “propertyname” to have a value equal to “newvalue”. If the input value is incompatible (i.e. an int being set to a string), print out an appropriate error message. GET propertyname will print out the current value of the target object’s member named “propertyname”. GET * will print out a list of all target object members and their current values. The system should be extensible for future commands and should accept an arbitrary object, such that another developer could insert another object into the system and rely on the command console to get and set the properties correctly.
0 Answers ABC, Guidance Software,
write a function -oriented program that generates the Fibonacci, the current numbers of n(as input) and display them (series). In Fibonacci, the current third number is the sum of the previous number.
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; }
write a program using 2 D that searches a number and display the number of items 12 inputs values input 15,20, 13, 30, 38, 40,16, 18, 20 ,18 ,20 enter no. to search : 20
what is the use of using for loop as "for(;;)"?