[ Viewing Hints ] [ Book Home Page ] [ Free Newsletter ]
[ Seminars ] [ Seminars on CD ROM ] [ Consulting ]
Annotated Solution Guide
Revision 1.0
for Thinking in C++, 2nd edition, Volume 1
by Chuck Allison
©2001 MindView, Inc. All Rights Reserved.
[ Previous Chapter ] [ Table of Contents ] [ Next Chapter ]
Chapter 4
4-1
In the Standard C library, the function puts( ) prints a char array to the console (so you can say puts("hello")). Write a C program that uses puts( )but does not include <stdio.h>or otherwise declare the function. Compile this program with your C compiler. (Some C++ compilers are not distinct from their C compilers; in this case you may need to discover a command-line flag that forces a C compilation.) Now compile it with the C++ compiler and note the difference.
(Left to the reader)
4-2
Create a struct declaration with a single member function, then create a definition for that member function. Create an object of your new data type, and call the member function.
(see the next exercise)
4-3
Change your solution to Exercise 2 so the struct is declared in a properly “guarded” header file, with the definition in one cpp file and your main( ) in another.
Solution:
//: S04:MyStruct.h
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
struct MyStruct {
    void f();
};
#endif ///:~
//: S04:MyStruct.cpp {O}
#include "MyStruct.h"
#include <iostream>
using namespace std;
void MyStruct::f() {
    cout << "MyStruct::f\n";
} ///:~
//: S04:Exercise3.cpp
//{L} MyStruct
#include "MyStruct.h"
int main() {
    MyStruct m;
    m.f();
}
///:~
The #ifndef statement in MyStruct.h guarantees that the file will not be included more than once in any compilation, which isn’t an issue here, but in large projects it’s not unusual for header files to be logically included multiple times. The defined preprocessor operator provides an alternate means of checking for defined preprocessor symbols, as follows:
#if !defined(MYSTRUCT_H)
This form allows multiple conditions to be tested with logical connectives such as ||and &&.
4-4
Create a struct with a single int data member, and two global functions, each of which takes a pointer to that struct. The first function has a second int argument and sets the struct’s int to the argument value, the second displays the int from the struct. Test the functions.
(see the )
4-5
Repeat Exercise 4 but move the functions so they are member functions of the delete instruct, and test again.
Solution:
//: S04:GetSet.cpp
#include <iostream>
using namespace std;
struct HasInt {
    int x;
    void setInt(int newX) {
        x = newX;
    }
    int getInt() {
        return x;
    }
};
int main() {
    HasInt h;
    h.setInt(5);
    cout << h.getInt() << endl; // 5
}
///:~
It is very common for a class to have such get- and set-members like this one does. In this example I could have separated the class definition and the member function implementation like I did in exercise number 3 above, but I didn’t for two reasons: 1) I’m lazy, and 2) I wanted to remind you that when you define member function bodies in situ like this (i.e., inside the class definition), they are implicitly inline functions.
4-6
Create a class that (redundantly) performs data member selection and a member function call using the this keyword (which refers to the address of the current object).
Solution:
//: S04:UsesThis.cpp
#include <iostream>
using namespace std;
struct HasInt {
    int x;
    void setInt(int x) {
        this->x = x;
    }
    int getInt() {
        return this->x;
    }
    void display() {
        cout << this->getInt() << endl;
    }
};
int main() {
    HasInt h;
    h.setInt(5);
    h.display(); // 5
}
///:~
This is a variation on the previous exercise. The use of this is actually necessary in HasInt::setInt to disambiguate the parameter x from the member x.
4-7
Make a Stash that holds doubles. Fill it with 25 double values, then print them out to the console.
(Left to the reader)
4-8
Repeat Exercise 7 with Stack.
(Left to the reader)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。