A. Write The Definition of The Member Function Func So That U Is Set To 10 and W Is Set To 15.3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Consider the following declarations: [5]

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;

Answer the following:


a. Write the definition of the member function func() so that u is
set to 10 and w is set to 15.3

Ans 1. void xClass::func() {


u = 10;
w = 15.3;
}

b. Write the definition of the member function print() that prints the
contents of u and w.

void xClass::print() const {


cout << "u: " << u << ", w: " << w << endl;
}

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.

xClass t(20, 35.0);

#include <iostream>

using namespace std;

class xClass {
public:

void print() const;

void func(); // Declaration of func()

xClass(int, double); // Parameterized constructor

private:

int u;

double w;

};

// Parameterized constructor definition

xClass::xClass(int a, double b) : u(a), w(b) {}

// Definition of func() to set u to 10 and w to 15.3

void xClass::func() {

u = 10;

w = 15.3;

// Definition of print() to display u and w

void xClass::print() const {

cout << "u: " << u << ", w: " << w << endl;

int main() {

xClass t(20, 35.0); // Create an object t with u = 20 and w = 35.0

t.print(); // Output: u: 20, w: 35.0


t.func(); // Set u to 10 and w to 15.3

t.print(); // Output: u: 10, w: 15.3

return 0;

Q2

class Distance

float feet;

int inches;

public:

Distance() : feet(0), inches(0) {} // Default constructor

Distance(float floatval) {

feet = floatval; // Assign feet

inches = (floatval - feet) * 12; // Calculate inches

Distance operator++(int) { // Postfix increment

Distance temp; // Temporary object to hold the current state

temp.feet = feet++; // Store current feet and then increment


temp.inches = ++inches; // Increment inches before returning

return temp; // Return the temp object

Distance operator++() { // Prefix increment

++feet; // Increment feet

inches++; // Increment inches

return *this; // Return the current object

void showdist() const {

cout << feet << " - " << inches << endl; // Output the distance

}; // end of class

int main() {

Distance d1, d2(14.8); // d1 initialized to 0 - 0; d2 initialized to 14.8

d2++; // Postfix increment on d2

d2.showdist(); // 1. Output for d2

d1 = ++d2; // Prefix increment on d2, assign to d1

d1.showdist(); // 2. Output for d1

d2.showdist(); // 3. Output for d2

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)

State after Initialization:

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

4. Prefix Increment (d1 = ++d2;):


a. The operator++() is called for d2:
i. ++feet → feet becomes 16.
ii. inches++ → inches becomes 11.
b. d2 is now 16 - 11, and d1 gets assigned this state.
5. Output for d1.showdist();:
a. Since d1 has been assigned d2's current state:
Copy code
16 - 11

6. Output for d2.showdist();:


a. Finally, we print d2:
Copy code
16 - 11

Final Output

The program will output the following:

Copy code
15 - 10
16 - 11
16 - 11

Summary of Outputs

1. After the d2++ operation, the output is 15 - 10.


2. After the d1 = ++d2 operation, the output for d1 is 16 - 11.
3. The output for d2 after the increment is also 16 - 11.

You might also like