7.15 HOMEWORK


7.15 HOMEWORK

  1. Why does this C++ program not compile?

          class X {          int i;      public:          X( int ii ) { i = ii; }      };      int main() { X xarray[10]; } 
  2. Is this C++ program legal?

          class X {         int i;      };      int main() { X xarray[10]; } 
  3. The class X does not possess a no-arg constructor, either user-defined or system-supplied. Nonetheless, main declares a vector as shown. Will this C++ program compile?

        #include <vector>    using namespace std;    class X {        int i;    public:        X( int ii ) { i = ii; }    };    int main() { vector<X> xvec; } 
  4. This C++ program does not compile. Why?

        #include <vector>    using namespace std;    class X {       int i;    public:        X( int ii ) { i = ii; }    };    int main() { vector<X> xvec(100); } 
  5. Will C++ this program compile and execute?

         #include <vector>     using namespace std;     class X { int i; };       int main() { vector<X> xvec; } 
  6. Will this C++ program compile? If it does compile, will it do what the programmer mostly likely wanted it to do?

         #include <string>     using namespace std;             class User {         string name;     };     class UserGroup {         User chief;         User uList[10];     };          int main() { UserGroup ug(); } 
  7. What is the output of this C++ program?

         #include <iostream>     #include <string>     using namespace std;     class User {     public:         string name;     };     class UserGroup {     public:         User chief;         User uList[10];     };     int main()     {         UserGroup ug;         cout << ug.chief.name;         cout << ug.uList[0].name;     } 
  8. It was mentioned in Section 7.2 that when a programmer does not provide a class with any constructors, the class does not get a system-supplied default no-arg constructor if it has a const data member. In the following program, the class Y has not been provided with any constructors at all and it has a const data member. Class X has been provided with a no-arg constructor as shown. The program compiles and runs without errors. Why?

         #include <iostream>     using namespace std;          class Y {     public:          const int yval;     };     class X {     public:          Y y;          int xval;          X() {}     };     int main() {          X x;          cout<<x.y.yval<<endl;          return 0;     } 




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net