I am a beginner c++ learner and I'm trying to learn how to build classes with minimal use of pass by reference so I learn more object oriented programming. I am building a very small phone simulator. This is my code.
My bool function won't work, and I'm trying to create a function where I can count the number of phone calls each phone makes, but I can't call the display function in displaySummaryInfo, even with passing by reference. I'm stuck. Does anyone know how to help me?Code:
// Example program
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
static int live = 0;
static int text = 0;
class PhoneNumber {
public:
static int MaxPhones;
PhoneNumber();
PhoneNumber(string, string, string, int);
void input();
void dial(PhoneNumber &);
void txt(PhoneNumber &);
void display();
bool maxTextsreached();
void displaySummaryinfo(PhoneNumber&, PhoneNumber &);
private:
const int MAXTEXTS = 10;
string areaCode;
string exchange;
string line;
int nlive;
int ntext;
};
PhoneNumber::PhoneNumber(){
nlive = 0;
ntext = 0;
}
PhoneNumber::PhoneNumber(string aC, string e, string l, int m){
aC = areaCode;
e = exchange;
l = line;
m = MAXTEXTS;
}
void PhoneNumber::input(){
cout << "Enter phone number in (NNN) NNN-NNNN format: ";
cin.ignore(0);
cin >> setw( 4 ) >> areaCode;
cin.ignore( 1 ); // skip ( and space
cin >> setw( 4 ) >> exchange;
cin.ignore();
cin >> setw( 5 ) >> line; // input line
}
void PhoneNumber::display(){
cout << "(" << areaCode << ")" << exchange << "-" << line;
}
void PhoneNumber::dial(PhoneNumber &p2){
this->display();
cout << " is Calling phone number ";
p2.display();
cout << " ...call made." << endl;
cout << "Printing nlive: " << live << endl;
live++;
nlive++;
}
void PhoneNumber::txt(PhoneNumber &p1){
display();
cout << " is texting number " ;
p1.display();
cout << " .....message sent." << endl;
cout << "Printing ntext: " << text << endl;
text++;
ntext++;
}
bool PhoneNumber::maxTextsreached(){
if (text <= 10){
return true;
} else {
cout << "The maximum number of texts has been reached." << endl;
return false;
}
}
void PhoneNumber::displaySummaryinfo(PhoneNumber &p1, PhoneNumber &p2){
cout << "A total of " << live << " calls and " << text << " texts were made." <<endl;
cout << "Phone number " << PhoneNumber::p1.display << " made " << nlive << "calls and " << ntext << " texts." << endl;
cout << "Phone number " << PhoneNumber::p2.display <.< " made " << nlive << "calls and " << ntext << " texts." << endl;
}
int main() {
PhoneNumber p1, p2; // create two instances of PhoneClass
char answer;
bool maxTextsreached = false;
p1.input(); // input the phone number into the first phone object
p2.input(); // input the phone number into the second phone object
p1.dial(p2); // simulate phone 1 dialing phone 2
p2.txt(p1); // simulate phone 2 sending a text to phone 1
do {
cout << "Enter c to make a a call, t to text, s for summary information, or x to exit. " << endl;
cin >> answer;
if (answer == 'c'){
p1.dial(p2);
}
else if (answer == 't'){
bool maxTextsreached(text);
p2.txt(p1);
}
else if (answer == 's'){
;
}
} while (answer != 'x');
cout << "You made " << live << " calls and " << text << " texts." << endl;
return 0;
if (answer == 'y'){
main();
}
else
return 0;
}