c++ program to swap the objects of two different classes



c++ program to swap the objects of two different classes..

Answer / Suresh.d

In C++, you cannot directly swap objects of different classes without using a third variable or a function. Here's an example demonstrating how to use a function for swapping:

```cpp
#include <iostream>
using namespace std;

class A {
public:
int data;
A(int data) : data(data) {}
};

class B {
public:
char name[10];
B(char *name) : str(name) {}
const char* getName() { return str; }
private:
char str[10];
};

void swap(A &a, A &b) {
A temp = a;
a = b;
b = temp;
}

void swap(B &a, B &b) {
char *temp = a.getName();
a.str = b.getName();
b.str = temp;
}

int main() {
A a1(10);
A a2(20);
B b1("John");
B b2("Doe");

cout << "a1 data: " << a1.data << "n";
cout << "a2 data: " << a2.data << "n";
cout << "b1 name: " << b1.getName() << "n";
cout << "b2 name: " << b2.getName() << "n";

swap(a1, a2);
swap(b1, b2);

cout << "After swapping:n";
cout << "a1 data: " << a1.data << "n";
cout << "a2 data: " << a2.data << "n";
cout << "b1 name: " << b1.getName() << "n";
cout << "b2 name: " << b2.getName() << "n";

return 0;
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More OOPS Interview Questions

assume the program must insert 4 elements from the key board and then do the following programs.sequential search(search one of the elements),using insertion sort(sort the element) and using selection sort(sort the element).

1 Answers  


What is difference between class and object with example?

1 Answers  


What is the difference between class and object?

4 Answers   Apple, Heinz,


What is byval and byref? What are differences between them?

1 Answers   HCL, Wipro,


#include <stdio.h> #include <alloc.h> #include <stdlib.h> #include <conio.h> void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

1 Answers  


What is this pointer in oop?

1 Answers  


#include <string.h> #include <stdio.h> #include <stdlib.h> #include <conio.h> void select(char *items, int count); int main(void) { char s[255]; printf("Enter a string:"); gets(s); select(s, strlen(s)); printf("The sorted string is: %s.\n", s); getch(); return 0; } void select(char *items, int count) { register int a, b, c; int exchange; char t; for(a = 0; a < count-1; ++a) { exchange = 0; c = a; t = items[ a ]; for(b = a + 1; b < count; ++b) { if(items[ b ] < t) { c = b; t = items[ b ]; exchange = 1; } } if(exchange) { items[ c ] = items[ a ]; items[ a ] = t; } } } design an algorithm for Selection Sort

1 Answers  


What is a class oop?

1 Answers  


What is abstrac class?where is it use?

2 Answers  


I am DeePu sotware engineer working with EMC corporation ,recently I had attended mcafee interview . Their questions were at heights of stupidity , I don't know what they want , I am a developer with 4 year experienced .I am listing the questions asked 1:What is the flag in g++ to avoid structure padding 2:In wht order parameters are passed to stack 3:How you will edit code segment of an exe

1 Answers  


What is the real time example of inheritance?

1 Answers  


difference between static and non-static variables?

2 Answers  


Categories