A. Write The Definition of The Member Function Func So That U Is Set To 10 and W Is Set To 15.3
A. Write The Definition of The Member Function Func So That U Is Set To 10 and W Is Set To 15.3
A. Write The Definition of The Member Function Func So That U Is Set To 10 and W Is Set To 15.3
class xClass
{
public:
void func();
void print() const ;
xClass ();
xClass (int, double);
private:
int u;
double w; };
and assume that the following statement is in a user program: xClass
x;
b. Write the definition of the member function print() that prints the
contents of u and w.
c. Write the definition of the default constructor of the class xClass so that the private
member variables are initialized to 0.
xClass::xClass() : u(0), w(0) {}
d. Write a C++ statement that print the values of the member variables of the object x
The answer to the question "Write a C++ statement that prints the values of the
member variables of the object x" is:
cpp
Copy code
x.print();
This single line of code will invoke the print() function for the x object, displaying the
values of its member variables u and w. This statement assumes that the print()
function is defined in xClass to output these values, as shown below:
cpp
Copy code
void xClass::print() const {
cout << "u: " << u << ", w: " << w << endl;
}
When x.print(); is called, it will output the current values of u and w for the x object.
Write a C++ statement that declares an object “t” of type xClass and initializes the
member variables of “t” to 20 and 35.0, respectively.
#include <iostream>
class xClass {
public:
private:
int u;
double w;
};
void xClass::func() {
u = 10;
w = 15.3;
cout << "u: " << u << ", w: " << w << endl;
int main() {
return 0;
Q2
class Distance
float feet;
int inches;
public:
Distance(float floatval) {
cout << feet << " - " << inches << endl; // Output the distance
}; // end of class
int main() {
return 0;
}
Dry Run Steps
1. Initialization:
a. Distance d1; calls the default constructor, so:
i. d1.feet = 0
ii. d1.inches = 0
b. Distance d2(14.8); calls the parameterized constructor:
i. d2.feet = 14
ii. d2.inches = (14.8 - 14) * 12 = 9.6 (which gets truncated to
9 since inches is an int)
c. d1: 0 - 0
d. d2: 14 - 9
2. Postfix Increment (d2++):
a. The operator++(int) is called for d2:
i. temp.feet = feet++ → temp.feet = 14 and then feet becomes
15.
ii. temp.inches = ++inches → inches becomes 10.
b. d2 becomes 15 - 10, and temp is 14 - 10.
3. Output for d2.showdist();:
a. This prints d2's current state:
Copy code
15 - 10
Final Output
Copy code
15 - 10
16 - 11
16 - 11
Summary of Outputs