Question

Consider the following code snippet. Would this code compile/run? Justify your answer.

#include < iostream>
using namespace std;

int main()
{
int * pPointer = new int;
*pPointer = 72;
pPointer = new int;
*pPointer = 84;
return 0;
}

Question

Determine the exact output of the following program:

#include < iostream>
class Polygon {
int num;
protected:
void setnum(const int n) { num = n; }
public:
Polygon() { num = 0; }
Polygon(int n) {
std::cout << "Polygon:" << n << std::endl;
num = n;
}
Polygon(const Polygon& src) {
num = src.num + 2;
std::cout << "cc1:" << num << std::endl;
}
Polygon & operator=(const Polygon& src) {
num = src.num - 2;
std::cout << "=1:" << num << std::endl;
return *this;
}
virtual ~Polygon() {
std::cout << "~Polygon:" << num << std::endl;
}
virtual void go()const {
std::cout << "Top:" << num << std::endl;
}
};

class Square : public Polygon {
int num;
protected:
int getnum() const { return num; }
public:
Square(int n) :Polygon(n / 2) {
std::cout << "Square:" << n << std::endl;
num = n;
}
virtual ~Square() {
std::cout << "~Square:" << num << std::endl;
}
void go()const {
std::cout << "bottom:" << num << std::endl;
}
};

void execute(Polygon &p) {
p.go();
}

void stop(Polygon p) {
p.go();
}

int main() {
Square sq(19);
std::cout << "--------------" << std::endl;
execute(sq);
stop(sq);
std::cout << "--------------" << std::endl;
}

Question

Consider adding the following function calls to the main function in above question.(add each function separately) Show the correct output and if any, would cause a compile or runtime warning or error? Show output or error by adding each separately. If it does compile show where you added the script and show the correct output

1) sq.setnum(3);
2) int number = sq.getnum();

Question

Upgrade the following two classes to derived classes in an inheritance hierarchy that has an abstract class named Cycle as its base class. Add the necessary syntax directly below. (Code the base class separately as described in section b below.)

class Bicycle
{
double diameter[2];
public:
Bicycle(const double* dia) { for (int i = 0; i < 2; i++) diameter[i] = dia[i]; }
double wheel(const int i) const { return i <= 2 && i > 0 ? diameter[i - 1]; }
};

class Tricycle
{
double diameter[3];
public:
Tricycle(const double* dia) { for (int i = 0; i < 3; i++) diameter[i] = dia[i]; }
double wheel(const int i) const { return i <= 3 && i > 0 ? diameter[i - 1]; }
};

Question

Consider the following function that accesses the Bicycle and Tricycle classes in your hierarchy.

void wheelDiameter(const Cycle& cycle, int i)
{
cycle.wheel(i);
}

Define the Cycle abstract base class here. Code your definition so that the wheelDiameter() function above selects the appropriate version of the wheel() function from your hierarchy.

Question

Given the following class with a resource, implement all necessary methods to make sure the memory allocated at m_name is properly copied and assigned with no memory leak or crash:

class Name {
char* m_name;
public:
Name(const char* name = nullptr);
std::ostream& display(std::ostream& ostr = std::cout)const;
};

std::ostream& display(std::ostream& ostr)const {
return ostr << (m_name ? m_name : "EMPTY!");
}

A-Implement the constructor that receives the address of a C-style null-terminated string as name and dynamically allocates enough memory to store the incoming string and then copies its value into m_name.

B-Implement the copy constructor.

C-Implement the assignment operator so a Name can be set to another Name.

D-Implement the destructor.

Question

Consider the following function, which calculates and returns the sum of the set of values pointed to by x.

double sum(const double* x, int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
sum += x[i];
return sum;
}

Write a function template that extends this definition to any fundamental type.

Question

In the segment below, add the necessary code

a)Declare a private variable named pPassengerAges in the Car class, which is of type pointer to an int;

b)Dynamically allocate an array of int size 6 when an instance of the Car class is created; make the array accessible via the pPassengerAges member variable.

a)Given the specific example in the above question, explain a specific task that should be performed by a destructor in the Car class.

b)Write a prototype for a function named scarlarMult that receives as parameters an array of double precision floating point numbers, an integer indicating the number of elements in the array, and a double-precision floating point number (which defaults to 1.0). The function returns nothing.

Question

Following program contains at least five different syntactic errors. Identify five errors by circling them in the code. For each error you circle: In one of the spaces below, give a brief explanation of the error and provide a fix.

//player.h
namespace game {
class Player {
int m_age;
int m_score;
char m_handle[20];

public:
Player(int age =18, char* handle);
void updateScore(double incr) const;
void getScore() const;
};
}

//mygame.cpp
#include “player.h”
using namespace game;

int main() {
Player p;
}


//player.cpp
#include “player.h”
namespace game {
Player::Player (int age =18. Char* handle) {
m_age =age;
std::strncpy(m_handle, handle, 20);
m_handle[20] =’’;
m_score = 20;
}

void updateScore (int incr) const {
M_score += incr;
}

void Player::getScore() const {
return score;
}
}

Question

Considering the following main.cpp, code a function that sorts an array of any type that contains n elements. Your function sorts the elements in ascending order from lower to higher values. Its prototype is

void sort(int* a, int n);
#include< iostream>
int main( ) {
int m[] = {189, 843, 321};
double f[] ={2.3,30.5,34.6};
sort(m, 3);
for (int i = 0; i < 3; i++)
std::cout << m[i] << std::endl;
sort(f, 3);
for (int i = 0; i < 3; i++)

Question

Design a class named PlayTracker in the namespace game, which can be used to track the aggregate playtime of gamers.

The PlayTracker class must be coded according to the following specification:

  • It stores two attributes: a pointer to a single int (to store the number of players) and a pointer to a single float( which stores the total amount of playing time).
  • It has a default constructor that sets the attributes to the null pointer address( representing the empty state)
  • It has a constructor that takes two parameters: an int( the number of players) and a float( the total number of minutes played). This constructor dynamically allocates memory first, then stores the data from the parameters.
  • It has an isEmpty() function, which returns true if and only if the object is in the empty state.
  • The class has a destructor, which performs the appropriate tasks
  • The class has a query function that prints the number of players and the total number of minutes.
  • The class has an update function that receives two parameters (the number of players and the total number of minutes played), and sets the attributes to the values of the parameters(allocating memory if necessary)
  • The class defines the + operator to allow two PlayTracker objects to be added together. The operator returns a PlayTracker in which the numPlayers is the sum of the numPlayers for the two operands, and similarly for numMinutes. If either operand is empty, it should be treated as if it has players/minutes of zero. The operator does not change either of the operands
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.