C++ Programing: In this exercise, you will design a class memberType. Part A: De
ID: 671617 • Letter: C
Question
C++ Programing:
In this exercise, you will design a class memberType.
Part A:
Design a class memberType can hold the name of a person, member ID, number of books bought, and amount spent.
Part B:
Include the functions to perform this operations:
i.) Set, get, and print a person’s name (3 different functions).
ii.) Set, get, and print the number of books bought (3 different functions).
iii.) Set, get, and print the number of amount spent (3 different functions).
Part C:
Define a constructor which set all the private member variables.
Part D:
Write a program to test various functions of your class memberType.
*NOTE: There should be 3 different Header Files (.h)
Explanation / Answer
Part A
#include<iostream>
#include<string>
using namespace std;
class memberType
{
public: string Name;
string ID;
int no_books;
float amount;
memberType()
{
Name=" ";
ID=" ";
no_books=0;
amount=0.00;
}
public: void set(string name, string id, int no, float a);
void modify(string name);
void modify(int no, float a);
void show();
};
void memberType::set(string n, string id, int no, float a)
{
Name=n;
ID=id;
no_books=no;
amount=a;
}
void memberType::modify(string name)
{
Name=name;
}
void memberType::modify(int no, float a)
{
no_books=no;
amount=a;
}
void memberType::show()
{
cout<<"Person's Name is: "<<Name<<endl;
cout<<"Person's ID is: "<<ID<<endl;
cout<<"Number of Books is: "<<no_books<<endl;
cout<<"Amount Spent is: "<<amount<<endl;
}
Part c.
private members of a class are accessible only from within other members of the same class (or from their "friends").
classes example
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area() {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
int main () {
Rectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
Part D
//Employee.cpp – calculates and displays a new salary
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
//declaration section
class Employee
{
public:
//initializes varaibles
Employee();
//assigns program value to variables
void AssignValue(string, float);
//returns name
string DisplayName();
//returns salary
double DisplaySalary();
//calculates new salary
double CalcNewSalary();
private:
string name;
double salary;
double raiseRate;
};
//implementation sectoin
Employee::Employee()
{
name = "";
salary = 0;
raiseRate = 0.0;
} //end of default constructor
void Employee::AssignValue(string n, double s, double r)
{
name = n;
salary = s;
raiseRate = r;
} //end of AssignValue method
string Employee::DisplayName();
{
return name;
} //end of DisplayName method
double Employee::DisplaySalary();
{
return salary;
} //end of DisplaySalary() method
double Employee::CalcNewSalary()
{
if (raiseRate >= 0)
{
salary = salary * raiseRate;
}
else
{
salary = 0.00;
}//end if
return salary;
} //end of CalcNewSalary method
int main()
{
//create Employee object
Employee hireEmployee;
//declare variables
string name = "";
double pay = 0;
double rate = 0.0;
//get name, salary, and raise percentage
cout << "Employee's name: ";
getline(cin, name);
cout << "Employee's current salary: ";
cin >> pay;
cout << "Raise rate: ";
cin >> rate;
//assign name and salary to the Employee object
hireEmployee.AssignValue(name, pay, rate);
//use the Employee object to display the name and current salary
cout << "Name: "
<< hireEmployee.DisplayName() << endl;
cout << "Salary: "
<< hireEmployee.DisplaySalary() << endl;
//use the Employee object to calculate the new salary
pay = hireEmployee.CalcNewSalary();
//use the Employee object to display the new salary
cout << pay << endl;
} //end of main function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.