Site icon Tutor Bin

Wayne State University Operator Overloading C++ Programming Code

Wayne State University Operator Overloading C++ Programming Code

Description

Instructions

Make sure your Assignment 1’s class* contains at least one (1) numeric attribute (int, float, double). If it does not, add a numeric attribute to your class.

Define the behavior of the following operators in your class’ objects:

  • Two objects of that class are considered equal (==) if …
  • The sum (+) of two objects of that class returns …
  • When passing one object of that class to cout operator, it displays …

Using those definitions, modify your class to overload those operators. In the corresponding operator overloading function add a comment that explains the operator functionality (i.e. the definition of each operators)

In the main program

Create three objects of your Assignments 1’s class and demonstrate that ==, +, and cout operators works as expected when using them with your class’ objects.

Submission

Files to submit:

  • Visual Studio solution/project files.
  • Class files (.h and .cpp)
  • Main program (.cpp)

Submit the Visual Studio project in a zip folder.

Example of expected submission

Note: the Solution/project files are not presented in this examples since those are automatically generated by Visual Studio. However, those are expected in students’ submission.

GroupCohesiveness.h

#include <iostream>

class GroupCohesiveness {
private:
int answer0, answer1, answer2, answer3;
public:
void setAnswer0(int a);
void setAnswer1(int a);
void setAnswer2(int a);
void setAnswer3(int a);
int getAnswer0() const;
int getAnswer1() const;
int getAnswer2() const;
int getAnswer3() const;
float getGroupCohesiveness() const;
bool operator==(const GroupCohesiveness& survey2) const;
GroupCohesiveness operator+(const GroupCohesiveness& survey2) const;
friend ostream& operator<<(ostream& os, const GroupCohesiveness& survey);
GroupCohesiveness();
GroupCohesiveness(int a0, int a1, int a2, int a3);
~GroupCohesiveness();
};

GroupCohesiveness.cpp

#include "GroupCohesivness.h"

void GroupCohesivness::setAnswer0(int a) {
if (a>=1 && a<=5) answer0=a;
}
void GroupCohesivness::setAnswer1(int a) {
if (a>=1 && a<=5) answer1=a;
}
void GroupCohesivness::setAnswer2(int a) {
if (a>=1 && a<=5) answer2=a;
}
void GroupCohesivness::setAnswer3(int a) {
if (a>=1 && a<=5) answer3=a;
}
int GroupCohesivness::getAnswer0() const {
return answer0;
}
int GroupCohesivness::getAnswer1() const {
return answer1;
}
int GroupCohesivness::getAnswer2() const {
return answer2;
}
int GroupCohesivness::getAnswer3() const {
return answer3;
}
float GroupCohesivness::getGroupCohesiveness() const {
return (answer0+answer1+answer2+answer3)/4.0;
}
// Two objects of that class are considered equal (==) if all the answers are the same
bool GroupCohesivness::operator==(const GroupCohesiveness& survey2) const {
return answer0==survey2.answer0 && answer1==survey2.answer1 && answer2==survey2.answer2 && answer3==survey2.answer3;
}
// The sum (+) of two objects of that class returns a new objects whose answers are the average of the 2 operand's GroupCohesiveness answers.
GroupCohesiveness GroupCohesivness::operator+(const GroupCohesiveness& survey2) const {
GroupCohesiveness temp;
temp.answer0=(answer0+survey2.answer0)/2;
temp.answer1=(answer1+survey2.answer1)/2;
temp.answer2=(answer2+survey2.answer2)/2;
temp.answer3=(answer3+survey2.answer3)/2;
return temp;
}
// When passing one object of that class to cout operator, it displays the answers of the surveys enclosed by parentheses.
friend ostream& operator<<(ostream& os, const GroupCohesiveness& survey) {
os << "(" << survey.answer0 << "," << survey.answer1 << "," << survey.answer2 << "," << survey.answer3 << ")";
return os;
}
GroupCohesiveness::GroupCohesiveness() {
answer0=1;
answer1=1;
answer2=1;
answer3=1;
}
GroupCohesiveness::GroupCohesiveness(int a0, int a1, int a2, int a3) {
answer0=a0;
answer1=a1;
answer2=a2;
answer3=a3;
}
GroupCohesiveness::~GroupCohesiveness() {
cout << "A Group Cohesiveness object has been removed from memory.";
}

Main.cpp

main() {
GroupCohesiveness surveyA(1,2,3,2);
GroupCohesiveness surveyB(3,5,4,3);
GroupCohesiveness surveyC;
surveyC=surveyA;

if (surveyA==surveyB)
cout << "Survey A and Survey B are equal" << endl;
else
cout << "Survey A and Survey B are not equal" << endl;
if (surveyA==surveyC)
cout << "Survey A and Survey C are equal" << endl;
else
cout << "Survey A and Survey C are not equal" << endl;
cout << "The average of Survey A and Survey B is: ";
GroupCohesiveness temp;
temp = surveyA+surveyB;
cout << temp << endl;
}

Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."

Exit mobile version