4-1
在标准C库中,函数puts()能显示字符数组到控制台上(所以能写puts(“hello”))。试写一个C语言程序,这个程序使用puts(),但不包含<stdio.h>,也不声明这个函数。用C编译器编译这个程序。(有些C++编译器并不与它们的C编译器分开;在这种情况下,可能需要使用一个强制C编译的命令行标记。)然后再用C++编译器对它编译,注意它们之间的区别。
CPP文件:
int main() {
puts("hello");
}
我用g++编译,只是出现一个警告:
4-1.c: In function 'main':
4-1.c:2:2: warning: implicit declaration of function 'puts' [-Wimplicit-function-declaration]
2 | puts("hello");
| ^~~~
按道理,应该给出错误提示才对,不知道怎么回事?按照作者的文章里的讲解,这时候应该是编译错误才对,因为使用了未声明的函数。不过这个不重要,我们知道就好了。
4-2
创建一个struct声明,它有单个成员函数,然后为这个成员函数创建定义。创建这个新数据类型的对象,再调用这个成员函数。
#include <iostream>
using namespace std;
struct MyStruct {
void f();
};
void MyStruct::f() {
cout << "void MyStruct::f() 被调用" << endl;
}
int main() {
MyStruct myStruct;
myStruct.f();
}
输出:
void MyStruct::f() 被调用
4-3
改变练习2的答案,使得struct在合适的“防护”头文件中声明,同时,它的定义在一个cpp文件中,main()在另一个文件中。
MyStruct.h
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
struct MyStruct {
void f();
};
#endif
MyStruct.cpp
#include "MyStruct.h"
#include <iostream>
using namespace std;
void MyStruct::f() {
cout << "void MyStruct::f() 被调用" << endl;
}
4-3.cpp
#include "MyStruct.h"
int main() {
MyStruct myStruct;
myStruct.f();
}
输出:
void MyStruct::f() 被调用
4-4
创建一个struct,它有一个int数据成员,再创建两个全局函数,每个函数都接受一个指向该struct的指针。第一个函数有第二个int参数,并设置这个struct的int为它的参数值,第二个函数显示来自这个struct的int。测试这两个函数。
#include <iostream>
using namespace std;
struct S {
int a;
};
void setS(S *ps, int a) {
ps->a = a;
}
void display(S *ps) {
cout << ps->a << endl;
}
int main() {
S s;
setS(&s, 1);
display(&s);
}
4-5
重写练习4,将两个函数改为这个struct的成员函数,再次测试。
#include <iostream>
using namespace std;
struct S {
int a;
void set(int a);
void display();
};
void S::set(int a) {
this->a = a;
}
void S::display() {
cout << a << endl;
}
int main() {
S s;
s.set(1);
s.display();
}
4-6
创建一个类,它使用this关键字(冗余地)执行数据成员选择和成员函数调用。(this表示当前对象的地址)。
#include <iostream>
using namespace std;
struct S {
int a;
void set(int a);
void display();
};
void S::set(int a) {
this->a = a;
cout << "当前a为:";
this->display();
}
void S::display() {
cout << a << endl;
}
int main() {
S s;
s.set(1);
}
输出:
当前a为:1
4-7
让Stash存放double,存入25个double值,然后把它们显示到控制台上。
#include "CppLib.h"
#include <iostream>
using namespace std;
int main() {
Stash doubleStash;
doubleStash.initialize(sizeof(double));
double k = 1.0;
for(int i = 0; i < 10; i++, k+=0.1)
doubleStash.add(&k);
for(int j = 0; j < doubleStash.count(); j++)
cout << "doubleStash.fetch(" << j << ") = "
<< *(double*)doubleStash.fetch(j)
<< endl;
doubleStash.cleanup();
}
输出:
doubleStash.fetch(0) = 1
doubleStash.fetch(1) = 1.1
doubleStash.fetch(2) = 1.2
doubleStash.fetch(3) = 1.3
doubleStash.fetch(4) = 1.4
doubleStash.fetch(5) = 1.5
doubleStash.fetch(6) = 1.6
doubleStash.fetch(7) = 1.7
doubleStash.fetch(8) = 1.8
doubleStash.fetch(9) = 1.9
freeing storage
4-8
用Stack重写练习7。
#include "Stack.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
Stack doubleStack;
doubleStack.initialize();
double d[10];
for (int i = 0; i < 10; i++) {
d[i] = i + 0.1;
doubleStack.push(&d[i]);
}
// Pop the lines from the Stack and print them:
double *pd;