1. Given the following class C (10 points): class C { private: int x; public: in
ID: 3549606 • Letter: 1
Question
1. Given the following class C (10 points):
class C
{
private:
int x;
public:
int y;
C();
C(int x_val, int y_val);
int Fn();
};
Indicate whether or not each of the following lines (a) through (e) is legal or illegal and give the reason.
int main()
{
C c1(7, 2); // (a)
C c3; // (b)
cout << c1.x; // (c)
c1.y = 5; // (d)
int n = c1.Fn(7); // (e)
}
2. Implement two functions in the following code (10 points)
#include <iostream>
using namespace std;
void random_fill(int arr[], int size);
int sum_array(int arr[], int size);
int main()
{
const int SIZE = 10;
int *arr = new int[SIZE];
random_fill(arr, SIZE); //fill the arr with random int value
int result = sum_array(arr, SIZE); //
cout << "The sum of all the elements in the array is " << result << endl;
delete []arr;
return 0;
}
a) Implement the function void random_fill(int arr[], int size) to initialize all the elements in array arr using randomly generated int value.
b) Implement the function int sum_array(int arr[], int size) to return the sum of all the elements in the array arr.
3. Write the definition of two functions in the class Rectangle in following code (15 points)
class Rectangle
{
public:
Rectangle(); // initialize width to 1.0 and length to 2.0
Rectangle(double x, double y); // initialize width as x and length as y
double get_perimeter(); // return the perimeter of the rectangle.
private:
double width;
double length;
};
a) Write the definition of the constructor without parameter, Rectangle(), which initializes width to 1.0 and length to 2.0. Write it as it written outside of the class declaration.
b) Write the definition of the constructor with two double parameters, Rectangle (double w, double l), using x to initialize width and y to initialize length. Write it as it written outside of the class declaration.
c) Write the definition of the method double get_perimeter(), which returns the perimeter of the rectangle. Write it as it written outside of the class declaration -- (hint: the perimeter of Rectangle is (width+length)*2 ).
Explanation / Answer
1. Given the following class C (10 points):
class C
{
private:
int x;
public:
int y;
C();
C(int x_val, int y_val);
int Fn();
};
Indicate whether or not each of the following lines (a) through (e) is legal or illegal and give the reason.
int main()
{
C c1(7, 2); // (a) LEGAL BECAUSE WE HAVE TWO ARGUMENT CONSTRUCTOR THAT CREATES THIS OBJECT.
C c3; // (b) LEGAL BECAUSE WE HAVE TWO DEGAULT CONSTRUCTOR THAT CREATES THIS OBJECT.
cout << c1.x; // (c) ILLEGAL BECAUSE X is private member variable we cant access outside class.
c1.y = 5; // (d) LEGAL BECAUSE y is public member variable of class.
int n = c1.Fn(7); // (e) //ILLEGAL BECAUSE function Fn doesn't take any arguments.
}
2. Implement two functions in the following code (10 points)
#include <iostream>
using namespace std;
void random_fill(int arr[], int size);
int sum_array(int arr[], int size);
int main()
{
const int SIZE = 10;
int *arr = new int[SIZE];
random_fill(arr, SIZE); //fill the arr with random int value
int result = sum_array(arr, SIZE); //
cout << "The sum of all the elements in the array is " << result << endl;
delete []arr;
return 0;
}
a) Implement the function void random_fill(int arr[], int size) to initialize all the elements in array arr using randomly generated int value.
void random_fill(int arr[], int size)
{
for(int i=0; i<size; i++)
{
int random_value1 = rand()%size;
int random_value2 = rand()%size;
int temp = arr[random_value1];
arr[random_value1] = arr[random_value2];
arr[random_value2] = temp;
}
}
b) Implement the function int sum_array(int arr[], int size) to return the sum of all the elements in the array arr.
int sum_array(int arr[], int size)
{
int sum = 0;
for(int i=0; i<size; i++)
sum = sum + arr[i];
return sum;
}
3. Write the definition of two functions in the class Rectangle in following code (15 points)
class Rectangle
{
public:
Rectangle(); // initialize width to 1.0 and length to 2.0
Rectangle(double x, double y); // initialize width as x and length as y
double get_perimeter(); // return the perimeter of the rectangle.
private:
double width;
double length;
};
a) Write the definition of the constructor without parameter, Rectangle(), which initializes width to 1.0 and length to 2.0.
Write it as it written outside of the class declaration.
Rectangle::Rectangle()
{
width = 1.0;
length= 2.0;
}
b) Write the definition of the constructor with two double parameters, Rectangle (double w, double l),
using x to initialize width and y to initialize length. Write it as it written outside of the class declaration.
Rectangle::Rectangle(double x, double y)
{
width = x;
length= y;
}
c) Write the definition of the method double get_perimeter(), which returns the perimeter of the rectangle.
Write it as it written outside of the class declaration -- (hint: the perimeter of Rectangle is (width+length)*2 ).
double Rectangle::get_perimeter()
{
return (width+length)*2;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.