Consider the following declarations:
class myClass
{
public:
void func();
void print() const;
myClass ();
myClass (int, double);
private:
int u;
double w;
};
myClass x;
3.1. How many members does class myClass have? (2)
3.2. How many private members does class myClass have? (2)
3.3. How many constructors does class myClass have? (2)
3.4. Write the definition of the member function func so that u is set to 10 and w is set to 15.3. (2)
3.5. Write the definition of the member function print that prints the contents of u and w. (2)
3.6. Write the definition of the default constructor of the class myClass so that the private data members are initialized to 0 (2)
Page 5 of 5
3.7. Write a C++ statement that prints the values of the data members of the object x. (3)
3.8. Write a C++ statement that declares an object t of the type myClass, and initializes the data members of t to 20 and 35.0, respectively.
1
Expert's answer
2014-03-31T14:24:30-0400
Answer on Question#40843 – Programming – C++
Question: Consider the following declarations: class myClass { public: void func(); void print() const; myClass (); myClass (int, double); private: int u; double w; }; myClass x; 3.1. How many members does class myClass have? (2) 3.2. How many private members does class myClass have? (2) 3.3. How many constructors does class myClass have? (2) 3.4. Write the definition of the member function func so that u is set to 10 and w is set to 15.3. (2) 3.5. Write the definition of the member function print that prints the contents of u and w. (2) 3.6. Write the definition of the default constructor of the class myClass so that the private data members are initialized to 0 (2) Page 5 of 5 3.7. Write a C++ statement that prints the values of the data members of the object x. (3) 3.8. Write a C++ statement that declares an object t of the type myClass, and initializes the data members of t to 20 and 35.0, respectively.
Answer: 3.1. There are 6 members in myClass class. They are: func() print() myClass () myClass (int, double) u w 3.2. There are two private members in myClass class. They are: u w 3.3. Class myClass has two constructors. They are: myClass (); myClass (int, double);
3.4. void func(){ u = 10; w = 15.3; } 3.5. void print() const{ cout<<"u="<<u<<endl<<"w="<<w<<endl; } 3.6. myClass(){ u = 0; w = 0; } 3.7. x.print(); 3.8. myClass t(20, 35.0);
Addition: It’s listing of the program with myClass class realization.
#include <iostream> using namespace std;
class myClass { public:
//the definition of the member function func //so that u is set to 10 and w is set to 15.3 void func(){ u = 10; w = 15.3; }
//the definition of the member function print that prints the contents of u and w void print() const{ cout<<"u="<<u<<endl<<"w="<<w<<endl; }
//the definition of the default constructor of the class myClass //so that the private data members are initialized to 0 myClass (){ u = 0; w = 0; }
myClass (int a, double b){ u = a; w = b; } private: int u; double w; };
myClass x;
int main(int argc, char *argv[]) { //statement that prints the values of the data members of the object x x.print();
//statement that declares an object t of the type myClass, and initializes the data members of t to 20 and 35.0 myClass t(20, 35.0);
Comments
Dear Vicy, You're welcome. We are glad to be helpful. If you liked our service please press like-button beside answer field. Thank you!
Thank You So Much For The Answer God Bless Y'ALL
Leave a comment