Question

In: Computer Science

class A { public: //constructors // other members private: int a; int b; }; Give declatations...

class A

{

public:

//constructors //

other members private:

int a;

int b;

};

Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function

Solutions

Expert Solution

a) as friend function

class A
{
   private:
   int a;
   int b;
  
   public:
   A(int a,int b)
   {
       this->a = a;
       this->b = b;
   }
   friend A operator +(const A &obj1,const A &obj2); // declared friend as public method
  
};

b) as member function

class A
{
   private:
   int a;
   int b;
  
   public:
   A(int a,int b)
   {
       this->a = a;
       this->b = b;
   }
   A operator +(const A &obj); // public method inside the class
  
};

c) as non-friend, non-member function

class A
{
   private:
   int a;
   int b;
  
   public:
   A(int a,int b)
   {
       this->a = a;
       this->b = b;
   }

// net public get methods to access and b outside class
int getA(){

return a;

}

int getB(){

return b;

}
  
};

A operator +( A &obj1, A &obj2); // outside the class

Do ask if any doubt.


Related Solutions

public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
Create separate class with these members a, b, c, x, y, z int a b c...
Create separate class with these members a, b, c, x, y, z int a b c float x y z Demonstrate 3) A two arg float, int constructor, and a three arg int, float, float constructor to instantiate objects, initialize variables read from the keyboard, display the sum Note:- Please type and execute this above java program and also give the output for both problems. (Type a java program)
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
C++ Class class Billfold { private: int twenties{}, tens{}, fives{}, dollars{}; public: /* Step 1. (5...
C++ Class class Billfold { private: int twenties{}, tens{}, fives{}, dollars{}; public: /* Step 1. (5 points) In class Billfold, write two Billfold constructors: default: set all bill counts to zero; do not allow the bill counts to be uninitialed. 2nd: set all bill counts to non-negative initial values; use parameters for: twenty, ten, five, dollar. IMPORTANT: prohibit negative values - if negative bill value given, use 0 for that bill instead. Accept other bills if >= 0. Tip: you...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT