29(5 points). What is the output of the above program? #include <iostream> #incl
ID: 3676120 • Letter: 2
Question
29(5 points). What is the output of the above program?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
Circle circle1;
Circle circle2;
circle2.setRadius(2.5);
cout << "The area of circle1 is " << circle1.getArea() << endl;
cout << "The area of circle2 is " << circle2.getArea() << endl;
return 0;
}
--Circle.h ------
#include <iostream>
#include <cmath>
using namespace std;
// Circle class declaration
class Circle
{ private:
double radius;
public: // Member function prototypes
Circle();
void setRadius(double);
double getArea();
};
30(5 points). What is the output of the above program?
Explanation / Answer
main.cpp
#include <iostream>
#include <cmath>
#include "Circle.h"
using namespace std;
int main()
{
Circle circle1;
Circle circle2;
circle2.setRadius(2.5);
cout << "The area of circle1 is " << circle1.getArea() << endl;
cout << "The area of circle2 is " << circle2.getArea() << endl;
return 0;
}
Circle.cpp
#include "Circle.h"
// Construct a circle object
Circle::Circle()
{
radius_ = 1;
}
// Construct a circle object
Circle::Circle(double newRadius)
{
radius_ = newRadius;
}
// Return the area of this circle
double Circle::getArea()
{
return radius_ * radius_ * 3.14159;
}
// Return the radius of this circle
double Circle::getRadius()
{
return radius_;
}
// Set a new radius
void Circle::setRadius(double newRadius)
{
radius_ = (newRadius >= 0) ? newRadius : 0;
}
Circle.h
// radius --> private
// getter/setter
class Circle
{
public:
Circle();
Circle(double);
double getArea();
double getRadius();
void setRadius(double);
private:
double radius_;
};
sample output
The area of circle1 is 3.14159
The area of circle2 is 19.6349
Demo.cpp
#include <iostream>
using namespace std;
class Demo
{
public:
Demo(); // Constructor prototype
~Demo(); // Destructor prototype
};
Demo::Demo() // Constructor function definition
{ cout << " the constructor" << " is running. ";
}
Demo::~Demo() // Destructor function definition
{ cout << "Now the destructor is running. ";
}
int main()
{
Demo demoObj; // Declare a Demo object;
return 0;
}
sample output
the constructor is running.
Now the destructor is running.
manager.h
#ifndef _MANAGER_H
#define _MANAGER_H
#include "employee.h"
class Manager : public Employee {
public:
Manager(string theName,
float thePayRate,
bool isSalaried);
bool getSalaried() const;
void setSalaried(bool theSalaried);
float pay(float hoursWorked) const;
protected:
bool salaried;
};
#endif /* not defined _MANAGER_H */
manager.cpp
#include "manager.h"
Manager::Manager(string theName,
float thePayRate,
bool isSalaried)
: Employee(theName, thePayRate)
{
salaried = isSalaried;
}
bool Manager::getSalaried() const
{
return salaried;
}
void Manager::setSalaried(bool theSalaried){
salaried = theSalaried;
}
float Manager::pay(float hoursWorked) const
{
if (salaried)
return payRate;
/* else */
return Employee::pay(hoursWorked);
}
employee.h
using namespace std;
#ifndef _EMPLOYEE_H
#define _EMPLOYEE_H
#include <string>
class Employee {
public:
Employee(string theName, float thePayRate);
string getName() const;
float getPayRate() const;
void setName(string theName);
void setPayRate(float thePayRate);
float pay(float hoursWorked) const;
protected:
string name;
float payRate;
};
#endif /* not defined _EMPLOYEE_H */
employee.cpp
#include "employee.h"
Employee::Employee(string theName, float thePayRate)
{
name = theName;
payRate = thePayRate;
}
string Employee::getName() const
{
return name;
}
float Employee::getPayRate() const
{
return payRate;
}
float Employee::pay(float hoursWorked) const
{
return hoursWorked * payRate;
}
void Employee::setName(string theName){
name = theName;
}
void Employee::setPayRate(float thePayRate){
payRate = thePayRate;
}
main.cpp
#include <iostream>
#include "employee.cpp"
#include "manager.cpp"
int main()
{
Employee empl("John Burke", 25.0);
Manager mgr("Jan Kovacs", 1200.0, true);
// Assume all employees worked 40 hours this period.
cout << "For Employee:" << endl;
cout << "Name: " << empl.getName() << endl;
cout << "Pay: " << empl.pay(40.0) << endl;
cout << "Changing the Employee's name..." << endl;
empl.setName("Doug Conners");
cout << "New Name: " << empl.getName() << endl;
cout << endl;
cout << "For Manager:" << endl;
cout << "Name: " << mgr.getName() << endl;
cout << "Salaried: " << mgr.getSalaried() << endl;
cout << "Pay: " << mgr.pay(40.0) << endl;
cout << "Changing the Manager's salaried status..." << endl;
mgr.setSalaried(false);
cout << "New Pay: " << mgr.pay(40.0) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.