- 3.11
- GradeBook.h
(Modifying Class GradeBook) Modify class GradeBook (Figs. 3.113.12) as follows:
a. Include a second string data member that represents the course instructor's name.
b. Provide a set function to change the instructor's name and a get function to retrieve it.
c. Modify the constructor to specify two parametersone for the course name and one for the instructor's name.
d. Modify member function displayMessage such that it first outputs the welcome message and course name, then outputs "This course is presented by: " followed by the instructor's name.
Use your modified class in a test program that demonstrates the class's new capabilities.
#include
using std::string;
class GradeBook
{
public:
GradeBook(string, string);
void setCourseName(string);
string getCourseName();
void setCourseInstructorName(string);
string getCourseInstructorName();
void displayMessage();
private:
string courseName;
string courseInstructorName;
};
#include
using std::cout;
using std::endl;
#include "GradeBook.h"
GradeBook::GradeBook(string name, string insName)
{
setCourseName(name);
setCourseInstructorName(insName);
}
void GradeBook::setCourseName(string name)
{
courseName = name;
}
string GradeBook::getCourseName()
{
return courseName;
}
void GradeBook::setCourseInstructorName(string insName)
{
courseInstructorName = insName;
}
string GradeBook::getCourseInstructorName()
{
return courseInstructorName;
}
void GradeBook::displayMessage()
{
cout << "Welcome to the grade book for\n" << getCourseName() << "!" <<
"\nThis course is presented by:" << getCourseInstructorName()
<< endl;
}
#include
using std::cout;
using std::endl;
#include "GradeBook.h"
int main(int argc, char *argv[])
{
GradeBook gradeBook1("CS101 Instroduction to C++ Programming", "John");
gradeBook1.displayMessage();
system("PAUSE");
return EXIT_SUCCESS;
}
Download Source from here.
(Account Class) Create a class called Account that a bank might use to represent customers' bank accounts. Your class should include one data member of type int to represent the account balance. [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., 2.75)called floating-point valuesto represent dollar amounts.] Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the constructor should display an error message, indicating that the initial balance was invalid. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and should ensure that the debit amount does not exceed the Account's balance. If it does, the balance should be left unchanged and the function should print a message indicating "Debit amount exceeded account balance." Member function getBalance should return the current balance. Create a program that creates two Account objects and tests the member functions of class Account.
- Account.h
#include
using std::string;
class Account
{
public:
Account(int);
void credit(int);
void debit(int);
int getBalance();
private:
int balance;
};
#include
using std::cout;
using std::endl;
#include "Account.h"
Account::Account(int initAmount)
{
if(initAmount < 0)
{
balance = 0;
cout << "ERROR!!! The initial balance was invalid.\n";
}
else
balance = initAmount;
}
void Account::credit(int amount)
{
balance += amount;
}
void Account::debit(int amount)
{
if(amount > balance)
{
cout << "ERROR!!! Debit amount exceeded account balance.\n";
return;
}
balance -= amount;
}
int Account::getBalance()
{
return balance;
}
#include
using std::cout;
using std::endl;
#include "Account.h"
int main(int argc, char *argv[])
{
Account acct1(-10);
cout << "Acct 1, Balance => $" << acct1.getBalance() << "\n";
acct1.credit(100);
cout << "Acct 1, Balance => $" << acct1.getBalance() << "\n";
Account acct2(1000);
cout << "Acct 2, Balance => $" << acct2.getBalance() << "\n";
acct2.debit(2000);
cout << "Acct 2, Balance => $" << acct2.getBalance() << "\n";
system("PAUSE");
return EXIT_SUCCESS;
}
Download source from here.
(Rational Class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class.
Use integer variables to represent the private data of the classthe numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction
would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks:
a. Adding two Rational numbers. The result should be stored in reduced form.
b. Subtracting two Rational numbers. The result should be stored in reduced form.
c. Multiplying two Rational numbers. The result should be stored in reduced form.
d. Dividing two Rational numbers. The result should be stored in reduced form.
e. Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator.
f. Printing Rational numbers in floating-point format.
- TBD