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
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...
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 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; }
Design ONE FUNCTION in a C++ code to find minimum, maximum and average of items in...
Design ONE FUNCTION in a C++ code to find minimum, maximum and average of items in an array, then place them proper locations in the array. Follow these steps: 1. Create an array with 11 integers, which will be randomly selected from the range of 10 to 100. Only random numbers between 10 and 100 are allowed in the array. Print the items of the array on screen as one line. 2. Develop a function that takes the array as...
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to...
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to create a data type SimpleCalculator capable of performing arithmetic operations. • Creating const member functions to enforce the principle of least privilege. The follow-up questions and activities also will give you practice: • Using constructors to specify initial values for data members of a programmer-defined class. Description of the Problem Write a SimpleCalculator class that has public methods for adding, subtracting, multiplying and dividing...
Write a full C++ program to read a series of integer numbers from standard input and...
Write a full C++ program to read a series of integer numbers from standard input and print them out in reverse order, one per line. You may assume that there will be a minimum of 2 numbers to read and a maximum of 200.