Question

1.Write a c++ program to find Maximum out of two numbers using friend function. Here one...

1.Write a c++ program to find Maximum out of two numbers using friend function. Here one member is of one class and second belongs to another class.

2.Write a c++ program to swap the values of private data members of classes names classOne and classTwo using friend keyword.

Homework Answers

Answer #1

program for problem 1

#include <iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
// declaring type
class two;
class one
{
        int x;
        public:
                // using friend function
        friend void max(one,two);
        }first;
class two
{
        int y;
        public:
        friend void max(one,two);
        }second;
        
void max(one x,two y)
{
        // declaring number1 and number2
        int number1,number2;
        // prompting user 
        cout << "Enter a positive integer: ";
        // taking input
    cin >> number1;
    // assigning number1 to class one member x
        x.x=number1;
        cout << "Enter a positive integer: ";
    cin >> number2;
    // assigning number1 to class two member y
        y.y=number2;
        cout<<"\nFirst no: "<<x.x;
        cout<<"\nSecond no: "<<y.y;
        // comparing numbers
        if(x.x>y.y)
        {
        cout<<"\n"<<x.x<<" is maximum";
        }
        else
        {
        cout<<"\n"<<y.y<<" is maximum";
        }
}
int main()
{
        // calling 
        max(first,second);
        getch();
}

OUTPUT SCREEN

program 2 solution

#include <iostream>
using namespace std;

// declaring class one
class one;
// declaring class two
class two
{
public:
  void print(one& x);
};

class one
{
  int a, b;
  friend void two::print(one& x);
public:
  one() : a(1), b(2) { }
};

void two::print(one& x)
{
        // printing output
        cout << "a is " << x.a << endl;
        cout << "b is " << x.b << endl;
}

int main()
{
        
        // creating objects to one and two classes
        one Oneobj;
        two Twoobj;

        Twoobj.print(Oneobj);
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You need to write a permute class that will take first and second strings to rearrange...
You need to write a permute class that will take first and second strings to rearrange letters in first, followed by second. For example, if the first is “CAT” string and second is “MAN” string, then the program would print the strings TACMAN, ATCMAN, CTAMAN, TCAMAN, ACTMAN, and CATMAN. The first and second strings can be any length of string or a null. The permute class uses a Node class as link list node to link all letters arrangement. The...
Write program in C#. WAP on Encapsulation to hide the type members with private access and...
Write program in C#. WAP on Encapsulation to hide the type members with private access and display employee details. • Create a new project, select console application from the template and name the project as “EmployeeDetails.cs”. • Here type members are nothing but properties. • Create another class as Employee and write the properties for EmpId, EmpName and Salary. • Now, create an instance of the Employee class in the “EmployeeDetails” class and assign the values for EmpId,EmpName and Salary...
Write a program to prepare the ‘payroll’ of an employee by using single inheritance. The base...
Write a program to prepare the ‘payroll’ of an employee by using single inheritance. The base class contains employee Name and Id while the derived class ‘allowance’ has float members for basic_pay, medical_allowance and transport_allowance. It then calculates gross pay of the employee through a member function (for both classes there should be getinput() and getoutput() functions with same name) use c++
Program: 6: Function overloading AIM: To write a C++ program to illustrate the concept of function...
Program: 6: Function overloading AIM: To write a C++ program to illustrate the concept of function overloading. PSEUDOCODE: Declare necessary variables. Create a class with add(int,int) ,add(float,float) as member functions and necessary variable. add(int,int) is used to add two integer values. add(float,float) is used to add two float values. Using object call the required function with corresponding input. Display the output.
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted linked list of ints that ends in a null pointer. //=============================================================== class Cell { friend class UList; private: int data; Cell* next; Cell( int dt, Cell* nx=nullptr ) : data(dt), next(nx) {} }; //=============================================================== class UList { private: Cell* head = nullptr;    // stationary head pointer. Cell* scan = nullptr;          // for walking down the List. Cell* follow = nullptr; public: void find( int...
Two int numbers are entered through the keyboard. Write a c++ program to find the value...
Two int numbers are entered through the keyboard. Write a c++ program to find the value of one number raised to the power of another. (Do not use the pow() function. Ex: Enter two int numbers: 5 3 5 * 5 * 5 = 125 Ex: Enter two int numbers: 5 -3 1/(5 * 5 * 5) = 1/125 = 0.008 Ex: Enter two int numbers: -2 -3 1/(-2 * -2 * -2 )= 1/-8 = -1/8 = - 0.125
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
write C program using function to read 20 float numbers and print the average of them...
write C program using function to read 20 float numbers and print the average of them after changing the negative to positive number.
Write 2 C functions. One returns the maximum of 5 numbers. The other function, returns the...
Write 2 C functions. One returns the maximum of 5 numbers. The other function, returns the minimum of 5 numbers. In your main function, prompt the user to input 5 numbers and call the 2 functions above.
In C++ Please, Write a program that (1) prompts for two integers, (2) prints out their...
In C++ Please, Write a program that (1) prompts for two integers, (2) prints out their sum, (3) prints out the first divided by the second, and (4) prints out the natural log of the first number raised to the power of the second number. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0; }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT