Question 2: Consider a class named memberType . Each object of memberType can ho
ID: 3788569 • Letter: Q
Question
Question 2:
Consider a class named memberType. Each object of memberType can hold the name of a person, member ID, and number of books bought. Complete the member functions to perform the following operations on the objects of memberType: create a new member, modify name, show name, modify member ID, show member ID, modify number of books bought and show number of books bought. Add the appropriate constructors. Write the definitions of the member functions of memberType.
Hint: Download the provided “class header file”, and “driver program file” from the blackboard, then create “class implementation file” based on function porotypes in the “class header file.” You should not change the driver program or the class header file unless your program will not be executed.
Make sure your code provides the same output as follows:
MAIN program:
MEMBERTYPE.h:
C: WINDOW Member 1: Initialized using the constructor function Name of the person: John Smith, Member ID: 100, Number of books bought: 1 Member 2: Created a new member using utility function of the class Name of the person: Robert De, Member ID: 200, Number of books bought: 2 Modify name for member 2: Peter Sam Modify member ID for member 2: 300 Modify number of books bought for member 2 5 Member 2: Updated name: Peter Sam Updated ID: 300 Updated number of books bought 5 Press any key to continueExplanation / Answer
//memberType.h:
#include <iostream>
#include <string.h>
using namespace std;
class memberType
{
private:
string name;
int memberID;
int numBooksBought;
public:
void setMember(string name, int memberID, int numBooksBought);
void modifyName(string name);
void showName() const;
void modifyMemberID(int memberID);
void showMemberID() const;
void modifyNumBooksBought(int numBooksBought);
void showNumBooksBought() const;
void print() const;
memberType();
memberType(string name, int memberID, int numBooksBought);
};
// memberType.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <iomanip> // std::setprecision
#include "memberType.h"
using namespace std;
void memberType::setMember(string name, int memberID, int numBooksBought)
{ this->name = name;
this->memberID = memberID;
this->numBooksBought = numBooksBought;
}
void memberType::modifyName(string name)
{
this->name = "Peter Sam";
}
void memberType::showName() const
{
cout << name << endl;
}
void memberType::modifyMemberID(int memberID)
{
this->memberID = 300;
}
void memberType::showMemberID() const
{
cout << memberID << endl;
}
void memberType::modifyNumBooksBought(int numBooksBought)
{
this->numBooksBought = 5;
}
void memberType::showNumBooksBought() const
{
cout << numBooksBought << endl;
}
void memberType::print() const
{
cout << "Name of the person: " << name << ", Member ID: " << memberID << ", Number of books bought: " << numBooksBought << endl;
}
memberType::memberType()
{
name = "Ayush Verma";
memberID = 10;
numBooksBought = 5;
}
memberType::memberType(string name, int memberID, int numBooksBought)
{
this->name = name;
this->memberID = memberID;
this->numBooksBought = numBooksBought;
}
// main.cpp
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "memberType.h"
using namespace std;
int main()
{
string name;
int num,id;
memberType member1("John Smith", 100, 1);
cout << "Member 1: Initialized using the constructor function" << endl;
member1.print();
cout << endl;
memberType member2;
member2.setMember("Robert De", 200, 2);
cout << "Member 2: Created a new member using utility function of the class"<< endl;
member2.print();
cout << endl;
cout << "Modify name for member 2: ";
getline(cin, name);
member2.modifyName(name);
cout << "Modify member ID for member 2: ";
cin >> id;
member2.modifyMemberID(id);
cout << "Modify number of books bought for member 2: ";
cin >> num;
member2.modifyNumBooksBought(num);
cout << endl;
cout << "Member 2:" << endl;
cout << "Updated name: ";
member2.showName();
cout << endl;
cout << "Updated ID: ";
member2.showMemberID();
cout << endl;
cout << "Updated number of books bought: ";
member2.showNumBooksBought();
cout << endl;
return 0;
}
/*
output:
Member 1: Initialized using the constructor function
Name of the person: John Smith, Member ID: 100, Number of books bought: 1
Member 2: Created a new member using utility function of the class
Name of the person: Robert De, Member ID: 200, Number of books bought: 2
Modify name for member 2: Peter Sam
Modify member ID for member 2: 300
Modify number of books bought for member 2: 5
Member 2:
Updated name: Peter Sam
Updated ID: 300
Updated number of books bought: 5
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.