Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

13.Consider the following definition of the class myClass: class myClass { publi

ID: 3549841 • Letter: 1

Question

13.Consider the following definition of the class myClass:

class myClass


{


public:


void setX( int a);


//Function to set the value of x.


//Postcondition: x = a;


void printX() const;


//Function to output x.


static void printCount();


//Function to output count. static void incrementCount();


//Function to increment count.


//Postcondition: count++;

myClass(int a = 0);

//constructor with default parameters


//Postcondition x = a;


//If no value is specified for a, x = 0;


private:


int x;


static int count;


};





a.Write a C++ statement that initializes the member variable count to 0.




b.Write a C++ statement that increments the value of count by 1.




c.Write a C++ statement that outputs the value of count.




d.Write the definitions of the functions of the class myClass as described in its definition.




e.Write a C++ statement that declares myObject1 to be a myClass object and initializes its member variable x to 5.





F. Write a C++ statement that declares myObject2 to be a myClass object and initializes its member variable x to 7.




g.Which of the following statements are valid? (Assume that myObject1 and myObject2 are as declared in Parts e and f.)






myObject1.printCount();






//Line 1








myObject1.printX();






//Line 2








myClass.printCount();






//Line 3








myClass.printX();






//Line 4








myClass::count++;






//Line 5












h.Assume that myObject1 and myObject2 are as declared in Parts e and f. What is the output of the following C++ code?

myObject1.printX();


cout << endl;


myObject1.incrementCount();





myClass::incrementCount();


myObject1.printCount();


cout << endl;


myObject2.printCount();


cout << endl;


myObject2.printX();


cout << endl;


myObject1.setX(14);


myObject1.incrementCount();


myObject1.printX();


cout << endl;


myObject1.printCount();


cout << endl;


myObject2.printCount();


cout << endl;

Explanation / Answer


a.Write a C++ statement that initializes the member variable count to 0.


int myClass:: count=0;

b. Write a C++ statement that increments the value of count by 1

void myClass:: incrementCount()

{

count++;

}

c. Write a C++ statement that outputs the value of count.

void myClass:: printCount()

{

cout << "count=" << count;

}

myClass::printCount();

d.Write the definitions of the functions of the class myClass as described in its definition.


void myClass:: setX(int a)

{

x = a;

}

void myClass:: printX()

{

cout << "x=" <<x;


}

void myClass:: printCount()

{

cout << "count=" << count;

}

void myClass:: incrementCount()

{

count++;

}

myClass(int a=0)

{

x=a;

}


e.Write a C++ statement that declares myObject1 to be a myClass object and initializes its member variable x to 5

int main()

{

myClass myObject1;

myObject1.setX(5);

}

F. Write a C++ statement that declares myObject2 to be a myClass object and initializes its member variable x to 7

myClass myObject2;

myObject2.setX(7);

g.Which of the following statements are valid? (Assume that myObject1 and myObject2 are as declared in Parts e and f.)
myObject1.printCount(); //Line 1 VALID
myObject1.printX(); //Line 2 VALID

myClass.printCount(); //Line 3   INVALID
myClass.printX(); //Line 4 INVALID
myClass::count++; //Line 5 INVALID


h.Assume that myObject1 and myObject2 are as declared in Parts e and f. What is the output of the following C++ code?


myObject1.printX();
cout << endl;
myObject1.incrementCount();
myClass::incrementCount();
myObject1.printCount();
cout << endl;
myObject2.printCount();
cout << endl;

myObject2.printX();
cout << endl;
myObject1.setX(14);
myObject1.incrementCount();
myObject1.printX();
cout << endl;
myObject1.printCount();
cout << endl;
myObject2.printCount();
cout << endl;

OUTPUT:
x=5

count=2

count=2

x=7

x=14

count=3

count=3


you can have a look at the complete code here.


#include<iostream.h>

#include<stdlib.h>

class myClass

{

public:

void setX( int a); //Function to set the value of x.

//Postcondition: x = a;

void printX() const; //Function to output x.


static void printCount(); //Function to output count.

static void incrementCount(); //Function to increment count.

//Postcondition: count++;

myClass(int a = 0); //constructor with default parameters

//Postcondition x = a;

//If no value is specified for a, x = 0;

private:

int x;

static int count;

};

void myClass:: setX(int a)

{

x = a;

}

void myClass:: printX() const

{

cout << "x=" <<x;


}

myClass ::myClass(int a)

{

x=a;

}

int myClass:: count =0;

void myClass:: printCount()

{

cout << "count=" << count;

}

void myClass:: incrementCount()

{

count++;

}


int main()

{

myClass myObject1, myObject2;

myObject1.setX(5);

myObject2.setX(7);

myObject1.printX();

cout << endl;

myObject1.incrementCount();

myClass::incrementCount();

myObject1.printCount();

cout << endl;

myObject2.printCount();

cout << endl;

myObject2.printX();

cout << endl;

myObject1.setX(14);

myObject1.incrementCount();

myObject1.printX();

cout << endl;

myObject1.printCount();

cout << endl;

myObject2.printCount();

cout << endl;

system("PAUSE");

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote