1-Assume there is a library function called sort as follows:

void sort(int values[], int size);

and it is declared in a headerfile called "sort.h", in a namespace called tools. Sort receives the values to be sorted in an array and the size of the array and sorts it.

Complete the following program to get unknown number of integers from user and print them back sorted as follows:

Output:

Enter the number of integers: 4
1: 20
2: 34
3: 1
4: 44
1 20 34 44

The Program:

//includes and namespaces
...

int main() {
int num, i;
int* values;
cout << "Enter the number of integers: ";
//get the number of integers (num)
cin >> num;

// allocate memory (store address in values)
...

// get the numbers one by one
for (i =0; i < num; i++) {
cout << (i + 1) << ": ";
cin >> values[i];
}

// sort the values using the sort function.
...

// now that the arrays is sorted
// print the elements back space separated
for (i =0; i < num; i++) {
cout << values[i] << " ";
}
cout << endl;
// free the allocated memory
...

return 0;
}

2-Design a class definition (no implementation), make sure that the privacy is properly set and member function have meaningful. Attribute names must follow the workshop standards.

NOTE: you are to only write the class definition; that is essentially what you write in a header file. DO NOT IMPLEMENT THE CLASS.

Create a class called a "Student".

A Student has following information:

  • a name, max 20 chars long
  • a student number that is a 6 digit integer
  • a Grade Point Average that is a floating point number
  • a semester that is a single digit integer.

Constructors and methods:

  • A student can get instantiated only in two ways; 1- with no information provided to be set in a safe empty state,2- with a name and a student number.
  • Grade Point Average and Semester can be set to values using modifier methods. (setters )
  • Grade Point average and student number can be accessed through queries. (getters).
  • A Student can display itself.

3-Complete the following class and fully implement the methods:

#include < iostream>
using namespace std;
class Canister { //
double m_capacity;
public:
Canister(...);
... display() ...;
... operator ... (...);
... ... operator ... (... ..., ... ...);
};
// See the end of the question for the tester example

A-The Constructor: sets the capacity or sets it to -1 (as safe empty state) if capacity value is not provided.

B-The display method: if in safe empty state, prints "Empty!" otherwise it prints the m_capacity value in 10 spaces, 2 digits after decimal and padded with zeros at left. Then it goes to new line

C-Overload the += operator so a double value can be added to a Canister and then return the Canister object.

D-Overload the + operator as a helper function that adds the values of two Canisters in a new Canister and then returns it.

Your implementation should work with the following main:

int main() {
Canister A = 10.1, B, C = 20.1;
A.display();
B.display();
C.display();
cout << "----------------" < B = A + C;
C = B += 5.1;
A.display();
B.display();
C.display();
return 0;
}
Output:
Cap: 0000010.10
Empty!
Cap: 0000020.10
----------------
Cap: 0000010.10
Cap: 0000035.30
Cap: 0000035.30

4-What is the output of the following code snippets:

A.int a = 10; cout << !!a;
B.int foo(int a = 2) { return a + 10; }
cout << foo() << " , << foo(10);
C. int a = 10; int& r = a; r += 2;
cout << a << , " << r ;
D.int a = 10, b = 20, c = 30;
int* p[4] = {&a, &c, &b, &a};
for(int i=0;i<4;i++) cout << *p[i] << ;

5-Determine the exact output of the following program:

#include < iostream>
#include < cstring>
using namespace std;
class Test {
char data[21];
public:
Test() {
cout << "Default" << endl;
data[0] = 'X'; data[1] = '';
}
Test(const Test& T) {
cout << "copy " << T.data << endl;
strcpy(data, T.data);
}
Test(const char* str) {
cout << "Make " << str << endl;
strcpy(data, str);
}
~Test() {
cout << data << " gone!" << endl;
}
void prn() {
cout << data << endl;
}
void print() {
cout << data;
}
};

void display(Test T) {
cout << "Displaying ";
T.print();
cout << endl;
}
int main() {
Test A, B = "BB";
display(A);
Test* p = new Test("Dyn");
p->prn();
delete p;
B.prn();
return 0;
}

6-Please circle the statements that are CORRECT:

A.C has no object-oriented support.
B.C++ is C with object-oriented capability, therefore programs written in this language could be object oriented or not.
C.C++ is fully object-oriented.
D.Java is fully object-oriented.

7-In two sentences explain what is a namespace, where do we use them and where do we create them?

8-Fill in the spaces using some or all of the following words or phrases. (some may be used more than once or not used at all)

A._____ is to organize all the code for relative tasks and objects into separate files and header files under the name of those tasks and objects.

B.C++ _____ works in three stages, first it will _____the files to interpret all the compiler instructions. These instructions begin with a "#" like #include. Then it will _____ all the CPP files separately into _____. So essentially _____ will run to the number of CPP files in your project. Finally, _____ will run to link all compiled code into _____.

C.Object-Oriented Programing is to use _____, _____ and _____ to design and code a solution to implement object to work in the program.

D._____ is to pack the data and behaviour of an object together in a class.

E._____ means having many forms(shapes).

F.Refining, using or expanding an already existing design of a class, to create a new class is called _____

9-True or False:

A.Fundamental types are made up of several compound types packed into a package.

B.Declaration associates an entity with a type, telling the compiler how to interpret the entity's identifier.

C.Including a file is literally copying the contents of the included file where the "#include" statement is written.

D.#include "headerfile" is for standard libraries and #include is for custom modules written by programmers.

E.The destructor is called right before the object goes out of scope.

F.To reset an object to its original values, a constructor can be called anywhere like a regular method.

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.