15 marks] Question 1: Consider the following declarations: class xClass public:
ID: 3583753 • Letter: 1
Question
15 marks] Question 1: Consider the following declarations: class xClass public: void func0; void print 0 const; Class 0; Class (nt, double) private: int u double w. and assume that the following statement is in a user program: xClass set to 10 and w is set to 153. write the definition of the func so that u is the contents of and w. IL write the definition or the mem tanction print that prints u member Write the defiait of the default oonitructor of the daw vclass so that the private variables are initialized 0. statement that prints the values of the mrmber variables of the object D, write a C++ statement that declares an ubject t of type tclass and initialiues the member E Write a C++ respectively. variables of t to 10 and 350, Question 2: Consider the following definition of the class myClass: 17 mar class myciasv public: void sex (nt a), liFunction to set the value ofx. Postcondition void print X0 const; ''Punction to output kudic void printcount0.Explanation / Answer
Question 1:
A.
//function declaration inside class
void func(int , double);
//function defintion outside class
void xClass :: func(int x,double y)
{
u = x;
w = y;
}
//calling the function from main
xClass x;
x.func(10, 15.3);
--------------
B.
//function declaration inside class
void Print();
//function definition outside class
void xClass :: Print()
{
cout <<"u = "<<u<<"w= "<<w<<endl;
}
//function called from main
x.Print();
-----------------------------
C.
//function declaration and definition inside class
xClass()
{
u=0;
w=0;
}
//the default constructor is called after the object is created i.e. when xClass x; will be executed the xClass() function will then be excuted and the values will be then initialized to 0
------------------------------
D.
since the data members are private , we can not directly access them !
so we must use member funcitons
first, let us assign the values of the data members to some temporary varialbles, then we'll use the variables to print the values ( this is the getter method )
//defining the get function inside the class
int Get1 ()
{
return u;
}
double Get2 ()
{
return w;
}
//calling them from main
cout<<"u = "<<x.Get1()<<"w="<<x.Get2()<<endl;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Question 2:
A.
since it is private we can not directly assign the value. we''ll use the setter method, similar to the one above, to take the value from a temporary variable to the member function.
//inside the class
void setCount (int i)
{
count = i;
}
//from main
myClass temp;
int i =0 ;
temp.setCount(i);
//value set to 0
-----
B.
//inside class
void incrementCount()
{
count++;
}
//from main
temp.incrementCount();
//increments value by 1
------------
C.
to output the value of counnt we will use the getter method used in Q1.
//inside claass
void GetCount ()
{
return count;
}
//from main
cour <<" value of count for our temp object is" << temp.getCount;
----
D.
this part, just look at the comments in the code given , the defintions are given in the comments only.
---------
E.
c++ statement to create a myclass object is
myClass myObject1;
myObject1.setX(5); //puts the value of x as 5 for my object
----------
F.
myClass myObject2;
myObject2.setX(7); //puts the value of x as 7 for my object
-------------------------
Example :
here if we print the following ,
cout << myObject1.x;
cout << myObject2.x;
so we'll get 5 in the first and 7 in the second. this tells us that x is member variable and is different for different objects.
--------------------------------------------------------
thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.