[ 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 Rerved.
[ 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 us puts( )but does not include <stdio.h>or otherwi declare the function. Compile this program with your C compiler. (Some C++ compilers are not distinct from their C compilers; in this ca 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.
(e the next exerci)
4-3
Change your solution to Exerci 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:Exerci3.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 cond int argument and ts the struct’s int to the argument value, the cond displays the int from the struct. Test the functions.
(e the )
4-5
Repeat Exerci 4 but move the functions so they are member functions of the struct, and test again.
Solution:
//: S04:GetSet.cpp
#include <iostream>
using namespace std;
struct HasInt {
int合租屋恋人 x;
void tInt(int newX) {
x = newX;
}
int getInt() {
return x;
}
};
int main() {
HasInt h;
h.tInt(5);
cout << h.getInt() << endl; // 5
}
///:~
It is very common for a class to have such get- and t-members like this one does. In this example I could have parated the class definition and the member function implementation like I did in exerci 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 lection and a member function call using the 单打独斗this keyword (which refers to the address of the current object).
Solution:
//: S04:UsThis.cpp
#include <iostream>
using namespace std;
struct HasInt {
int x;
void tInt(int x) {
this->x = x;
}
int getInt() {
return this->x;
}
void display() {
cout << this寒来暑往的意思->getInt() << endl;
}
};
int main() {
HasInt h;
h.tInt(5);
h.display(); // 5
}
///:~
This is a variation on the previous exerci. The u of this is actually necessary in HasInt::tInt 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 Exerci 7 with Stack.
(Left to the reader)