Point base-class and derived-class pointers at base-class and derived-class objects, respectively. - C++ Class

C++ examples for Class:Inheritance

Description

Point base-class and derived-class pointers at base-class and derived-class objects, respectively.

#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
    
class Employee {
public:
  Employee(const std::string&, const std::string&,
    const std::string&, double, double);
    
  void setFirstName(const std::string&);
  std::string getFirstName() const;
    
  void setLastName(const std::string&);
  std::string getLastName() const;
    
  void setSocialSecurityNumber(const std::string&);
  std::string getSocialSecurityNumber() const;
    
  void setGrossSales(double);
  double getGrossSales();
    
  void setCommissionRate(double);
  double getCommissionRate() const;
    
  virtual double earnings() const;
  virtual void print() const;
    
private:
  std::string firstName;
  std::string lastName;
  std::string socialSecurityNumber;
  double grossSales;
  double commissionRate;
};
    
#include <string>
    
class BasePlusEmployee : public Employee {
public:
  BasePlusEmployee(const std::string&,
    const std::string&, const string&,
    double = 0.0f, double = 0.0f, double = 0.0f);
    
  void setBaseSalary(double);
  double getBaseSalary() const;
    
  virtual double earnings() const;
  virtual void print() const;
    
private:
  double baseSalary;
};
    
    
    
int main(int argc, const char *argv[]) {
  // create base class object
  Employee commissionEmployee("Sue", "Jones", "222-22-2222", 10000, .06);
    
  // create base class pointer
  Employee *commissionEmployeePtr = 0;
    
  // create derived class object
  BasePlusEmployee basePlusEmployee("Bob", "Lewis", "333-33-3333", 5000, .04, 300);
    
  // create derived class pointer
  BasePlusEmployee *basePlusEmployeePtr = 0;
    
  // set floating-point output formatting
  std::cout << std::fixed << std::setprecision(2);
    
  // output objects commissionEmployee and basePlusEmployee
  std::cout << "Print base-class and derived-class objects:\n\n";
  commissionEmployee.print();
  std::cout << "\n\n";
  basePlusEmployee.print();
    
  // aim base-class pointer at base-class object and print
  commissionEmployeePtr = &commissionEmployee;
  std::cout << "\n\n\nCallint print with base-class pointer to " << "\nbase-class object invokes base-class print function:\n\n";
  commissionEmployeePtr->print();
    
  // aim derived-class pointer at derived-class object and print
  basePlusEmployeePtr = &basePlusEmployee;
  std::cout << "\n\n\nCallint print with derived-class pointer to "
    << "\nderived-class object invokes derived-class "
    << "print function:\n\n";
  basePlusEmployeePtr->print();
    
  // aim base-class pointer at derived-class object and print
  commissionEmployeePtr = &basePlusEmployee;
  std::cout << "\n\n\nCalling print with base-class pointer to "
    << "derived-class object\ninvokes base-class print "
    << "function on that derived-class object:\n\n";
  commissionEmployeePtr->print();
    
  std::cout << std::endl;
    
  return 0;
}

Related Tutorials