Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Challenge activity 7.6.1: Constructor overloading. Write a second constructor as

ID: 666204 • Letter: C

Question

Challenge activity 7.6.1: Constructor overloading.

Write a second constructor as indicated. Sample output:

#include <iostream>
using namespace std;

class PhonePlan{
public:
PhonePlan();
PhonePlan(int numMinutes, int numMessages);
void Print() const;
private:
int freeMinutes;
int freeMessages;
};

PhonePlan::PhonePlan() { // Default constructor
freeMinutes = 0;
freeMessages = 0;
return;
}

// FIXME: Create a second constructor with numMinutes and numMessages parameters.

/* Your solution goes here */

void PhonePlan::Print() const {
cout << "Minutes: " << freeMinutes << ", Messages: " << freeMessages << endl;
return;
}

int main() {
PhonePlan user1Plan; // Calls default constructor
PhonePlan user2Plan(1000, 5000); // Calls newly-created constructor

cout << "User1: ";
user1Plan.Print();

cout << "User2: ";
user2Plan.Print();

return 0;
}

Explanation / Answer

#include <iostream>

using namespace std;

class PhonePlan{
public:
PhonePlan();
PhonePlan(int numMinutes, int numMessages);
void Print() const;
private:
int freeMinutes;
int freeMessages;
};
PhonePlan::PhonePlan() { // Default constructor
freeMinutes = 0;
freeMessages = 0;
return;
}
// FIXME: Create a second constructor with numMinutes and numMessages parameters.
/* Your solution goes here */
PhonePlan::PhonePlan(int numMinutes,int numMessages){
freeMinutes=numMinutes;
freeMessages=numMessages;
return;
}
void PhonePlan::Print() const {
cout << "Minutes: " << freeMinutes << ", Messages: " << freeMessages << endl;
return;
}
int main() {
PhonePlan user1Plan; // Calls default constructor
PhonePlan user2Plan(1000, 5000); // Calls newly-created constructor
cout << "User1: ";
user1Plan.Print();
cout << "User2: ";
user2Plan.Print();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote