Question: Does this following program cause a compile error? If not, what is the output of the following program? #include #include using namespace std; struct part_struct
Does this following program cause a compile error? If not, what is the output of the following program?
#include
#include
using namespace std;
struct part_struct {
char descrip[31], part_num[11];
float unit_price; int qty;
};
part_struct getOldPart();
int main()
{
part_struct new_part, old_part;
old_part = getOldPart();
cout << old_part.descrip << " " << old_part.part_num << " " << old_part.unit_price << " " << old_part.qty << endl;
}
part_struct getOldPart(){
part_struct old_part;
strncpy_s(old_part.descrip, "SINGLE COLOR LED", 31);
strncpy_s(old_part.part_num, "123456", 11);
old_part.unit_price = 10.23;
old_part.qty = 10;
return old_part;
}
25(5 points). What is the output of the following program?
#include
#include
#include
using namespace std;
struct PayRoll
{
int empNumber; // Employee number
string name; // Employee name
double hours; // Hours worked
double payRate; // Hourly pay rate
};
int main()
{
PayRoll employee; // Employee is a PayRoll structure
double grossPay; // Gross amount the employee earned this week
employee.empNumber=1;
employee.name = "Smith";
employee.hours =20;
employee.payRate=10;
// Calculate the employee's gross pay
grossPay = employee.hours * employee.payRate;
// Display the results
cout << "Name: " << employee.name << endl;
cout << "Employee number: " << employee.empNumber << endl;
cout << "Hours worked: " << employee.hours << endl;
cout << "Hourly pay rate: " << employee.payRate << endl;
cout << "Gross pay: $" << grossPay << endl;
return 0;
}
26(5 points). What is the output of the following program?
// This program demonstrates the use of overloaded constructors.
#include
#include
using namespace std;
// Sale class declaration
class Sale
{
private:
double taxRate;
public:
Sale(double rate)
{ taxRate = rate;
}
Sale()
{ taxRate = 0.0;
}
double calcSaleTotal(double cost)
{ double total = cost + cost*taxRate;
return total;
}
};
int main()
{
Sale cashier1(.05);
Sale cashier2;
// Format the output
cout << fixed << showpoint << setprecision(2);
// Get and display the total sale price for two $24.95 sales
cout << "With a 0.05 sales tax rate, the total ";
cout << "of the $20 sale is $";
cout << cashier1.calcSaleTotal(20) << endl;
cout << " On a tax-exempt purchase, the total ";
cout << "of the $20 sale is, of course, $";
cout << cashier2.calcSaleTotal(20) << endl;
return 0;
}
27(5 points). What is the output of the following program?
#include
using namespace std;
class Demo
{ public:
Demo() // Constructor
{ cout << "running ";
}
};
int main()
{ cout << " before ";
Demo demoObj; // Define a Demo object
cout << "after ";
return 0;
}
28(5 points). What is the output of the above program?
#include
using namespace std;
class Polygon { protected:
int width, height;
public:
void set_values(int a, int b)
{ width = a; height = b;
}
};
class Rectangle : public Polygon { public:
int area()
{ return width * height;
}
};
class Triangle : public Polygon { public:
int area()
{ return width * height / 2;
}
};
int main() { Rectangle rect;
Triangle trgl;
rect.set_values(4, 5);
trgl.set_values(4, 5);
cout << rect.area() << ' ';
cout << trgl.area() << ' ';
return 0;
}
29(5 points). What is the output of the above program?
#include
#include
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
#include
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?
#include
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;
}
31 (Extra 6 points). Add methods to the classes named Employee and Manager.
setName()
setSalaried()
/*
* Employee class definition.
*/
#ifndef _EMPLOYEE_H
#define _EMPLOYEE_H
#include
class Employee { public:
Employee(string theName, float thePayRate);
string getName() const;
float getPayRate() const;
float pay(float hoursWorked) const;
protected:
string name;
float payRate;
};
#endif /* not defined _EMPLOYEE_H */
/*
* Employee method definitions.
*/
#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;
}
/*
* Manager class definition.
*/
#ifndef _MANAGER_H
#define _MANAGER_H
#include "employee.h"
class Manager : public Employee { public:
Manager(string theName,
float thePayRate,
bool isSalaried);
bool getSalaried() const;
float pay(float hoursWorked) const;
protected:
bool salaried;
};
#endif /* not defined _MANAGER_H */
/*
* Manager method definitions.
*/
#include "manager.h"
Manager::Manager(string theName,
float thePayRate,
bool isSalaried)
: Employee(theName, thePayRate)
{ salaried = isSalaried;
}
bool Manager::getSalaried() const
{ return salaried;
}
float Manager::pay(float hoursWorked) const
{ if (salaried)
return payRate;
/* else */
return Employee::pay(hoursWorked);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
