C++ Class Example #2
// C++ program illustrates derived classes.
// The base class contains name, street, city, state
// and zip information. Derived classes add either
// loan or savings information to base class info.
// Copyright (c) Chris H. Pappas and William H. Murray, 1990
// Obtained from Borland C++ Handbook, 3rd ed., McGraw Hill, 1992
#include < iostream.h >
#include < string.h >
char newline;
class customer {
char name[60],
street[60],
city[20],
state[15],
zip[10];
public:
void info_out(void);
void info_in(void);
};
void customer::info_out(void)
{
cout << "Name: " << name << "\n";
cout << "Street: " << street << "\n";
cout << "City: " << city << "\n";
cout << "State: " << state << "\n";
cout << "Zip: " << zip << "\n";
}
void customer::info_in(void)
{
cout << "Enter Customer Name: ";
cin.get(name,59,'\n');
cin.get(newline); //flush carriage return
cout << "Enter Street Address: ";
cin.get(street,59,'\n');
cin.get(newline);
cout << "Enter City: ";
cin.get(city,19,'\n');
cin.get(newline);
cout << "Enter State: ";
cin.get(state,14,'\n');
cin.get(newline);
cout << "Enter Zip Code: ";
cin.get(zip,9,'\n');
cin.get(newline);
}
class loan:public customer {
char loan_type[20];
float l_bal;
public:
void loan_customer( );
void l_disp( );
};
void loan::loan_customer( )
{
info_in( );
cout << "Enter Loan Type: ";
cin.get(loan_type,19,'\n');
cin.get(newline);
cout << "Enter Loan Balance: ";
cin >> l_bal;
cin.get(newline); //flush carriage return
}
void loan::l_disp( )
{
info_out( );
cout << "Loan Type: " << loan_type << "\n";
cout << "Loan Balance: $ " << l_bal << "\n";
}
class savings:public customer {
char savings_type[20];
float s_bal;
public:
void savings_customer( );
void s_disp( );
};
void savings::savings_customer( )
{
info_in( );
cout << "Enter Savings Account Type: ";
cin.get(savings_type,19,'\n');
cin.get(newline); //flush carriage return
cout << "Enter Account Balance: ";
cin >> s_bal;
cin.get(newline);
}
void savings::s_disp( )
{
info_out( );
cout << "Savings Type: " << savings_type << "\n";
cout << "Savings Balance: $ " << s_bal << "\n";
}
main( )
{
loan borrow; //get loan information
cout << "\n--Loan Customers--\n";
borrow.loan_customer( );
savings save; //get savings information
cout << "\n--Savings Customers--\n";
save.savings_customer( );
//display all information
cout << "\n--Loan Customers--\n";
borrow.l_disp( );
cout << "\n--Savings Customers--\n";
save.s_disp( );
return (0);
}