Microsoft API 和参考目录
http://msdn.microsoft.com/zh-cn/library----------------------------------------------------------------------------
1.11
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int v1 = 0, v2 = 0, min = 0, max = 0;
cin>>v1>>v2;
if(v1 > v2){
min = v2;
max = v1;
}else{
min = v1;
max = v2;
}
do{
cout<<min++<<' ';
}while(min < max);
system("PAUSE");
return 0;
}
2.3
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
unsigned u1 = 10, u2 = 42;
unsigned u3 = ~0;
unsigned u4 = u3+1 - 32;
cout<<u4<<endl;
cout<<u1 - u2 <<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
3.2
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string line, word;
/*while(getline(cin,line)){
cout<<line<<endl;
}*/
while(cin>>word){
cout<<word<<endl;
}
system("PAUSE");
return 0;
}
3.4
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string str1, str2;
cin>>str1>>str2;
if(str1 > str2){
cout<<str1<<endl;
}else if(str1 < str2){
cout<<str2<<endl;
}else{
cout<<"str1 == str2 "<<str1<<endl;
}
if(str1.size() > str2.size()){
cout<<"str1 is longer then str2 "<<str1<<endl;
}else{
cout<<"str1 is shorter then str2, or str1 is equl to str2 "<<str2<<endl;
}
system("PAUSE");
return 0;
}
3.5
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string result,temp;
while(cin>>temp){
//result += temp;
result += temp+" ";
}
cout<<result<<endl;
system("PAUSE");
return 0;
}
3.6
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string result;
cin>>result;
cout<<result<<endl;
for(char &c:result){
c = 'x';
}
cout<<result<<endl;
system("PAUSE");
return 0;
}
3.8
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string result;
decltype(result.size()) index = 0;
cin>>result;
cout<<result<<endl;
/*while(index < result.size()){
result[index++] = 'x';
}*/
for (index = 0; index < result.size(); index++)
{
result[index] = 'x';
}
cout<<result<<endl;
system("PAUSE");
return 0;
}
3.10
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string result,temp;
cin>>temp;
for(char &c:temp)
{
if(!ispunct(c)){
result += c;
}
}
cout<<result<<endl;
system("PAUSE");
return 0;
}
3.17
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
vector<string> ss;
string result;
while(cin>>result)
{
ss.push_back(result);
}
for(string &s : ss){
for(char &c : s){
c = toupper(c);
}
cout<<s<<endl;
}
system("PAUSE");
return 0;
}
3.20
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> tt;
int temp = 0;
decltype(tt.size()) i = 0;
while(cin>>temp){
tt.push_back(temp);
}
for (i = 0; i < tt.size() - 1; i++)
{
cout<<tt[i]+tt[i+1]<<endl;
}
cout<<"-------------------------"<<endl;
for (i = 0; i < tt.size()/2; i++)
{
cout<<tt[i]+tt[tt.size() - i - 1]<<endl;
}
if(tt.size()%2){
cout<<tt[tt.size()/2]<<endl;
}
system("PAUSE");
return 0;
}
3.22
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
string ss;
vector<string> vv;
vector<string>::iterator it;
while(cin>>ss){
vv.push_back(ss);
vv.push_back("");
}
for(it = vv.begin(); it != vv.end() && !it->empty(); it++)
{
for(char &c:(*it)){
c = toupper(c);
cout<<c;
}
}
cout<<endl;
it = vv.begin();
cout<<(*it)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
3.23
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> tt(10, 0);
vector<int>::iterator it = tt.begin();
int temp = 0;
while(it < tt.end() && cin>>temp){
(*it) = temp;
it++;
}
for(it = tt.begin(); it != tt.end(); it++){
(*it) *= 2;
}
for(it = tt.begin(); it != tt.end(); it++){
cout<<*it<<' ';
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
3.24
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> tt;
int temp = 0;
vector<int>::iterator it;
while(cin>>temp){
tt.push_back(temp);
}
for(it = tt.begin(); it != tt.end() - 1; it++){
cout<<(*it)+(*(it+1))<<" ";
}
cout<<endl;
auto start = tt.begin();
auto end = tt.end() - 1;
while(start <= end){
if(start!=end){
cout<<(*start)+(*end)<<" ";
}else{
cout<<(*start);
}
start++;
end--;
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
3.35
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int a[5] = {1,2,3,4,5};
int *p = &a[0];
for(int i = 0; i != 5; i++){
*(p+i) = 0;
}
for(int i = 0; i != 5; i++){
cout<<a[i]<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
3.43
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int ia[3][4] = {
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
/*for(int (&row)[4]:ia){
for(int &col:row){
cout<<col<<" ";
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
cout<<ia[i][j]<<" ";
}
}*/
for(int (*p)[4] = &ia[0]; p != ia + 3; p++){
for(int *q = &(*p)[0]; q != *p+4; q++){
cout<<*q<<" ";
}
}
cout<<endl;
system("PAUSE");
return 0;
}
4.21
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> tt;
int temp = 0;
while(cin >> temp){
tt.push_back(temp);
}
for(int &t:tt){
(t%2)? t*=2 :t;
}
for(const int &t:tt){
cout<<t<<" ";
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
4.22
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int g;
cin>>g;
cout<<((g > 90) ? "high pass": (g >= 60 && g < 75) ? "low pass" : (g < 60) ? "fail" : "pass")<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
4.25
提示:char类型会先提升为int类型占32位,然后再取反。
5.14
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
string s, crtStr, maxStr;
int crtCnt = 1, maxCnt = 1;
while(cin>>s){
if(crtStr != s){
if(crtCnt > maxCnt){
maxCnt = crtCnt;
maxStr = crtStr;
}
crtStr = s;
crtCnt = 1;
}else{
crtCnt++;
}
}
cout<< maxStr << " is occur " << maxCnt << " times"<<endl;
system("PAUSE");
return 0;
}
5.17
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> v1(3, 1);
vector<int> v2;
int i; bool b = true;
decltype(v1.size()) shortSize;
cout<<"input v2: "<<endl;
while(cin>>i){
v2.push_back(i);
}
(v1.size() < v2.size()) ? shortSize = v1.size() : shortSize = v2.size();
for (decltype(shortSize) j = 0; j != shortSize; j++)
{
if(v1[j] != v2[j]){
b = false;
}
}
if(b){
if(v1.size() < v2.size())
cout << "v1 is the child of v2"<<endl;
else
cout << "v2 is the child of v1"<<endl;
}else{
cout<< " these vectors are independ" << endl;
}
system("PAUSE");
return 0;
}
5.19
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
string s1, s2;
do
{
cout<<"input tow strings "<<endl;
cin>>s1>>s2;
if(s1.size() < s2.size())
cout<<"the shorter one is s1" << endl;
else
cout<<"the shorter one is s2" << endl;
} while (true);
system("PAUSE");
return 0;
}
5.20
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
string str, crtStr;
bool b = false;
while(cin>>str){
if(crtStr != str){
crtStr = str;
}else{
b = true;
break;
}
}
if(b){
cout<< crtStr << endl;
}else{
cout<< "there is no sequential word." << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
5.21
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
string str, crtStr;
bool b = false;
while(cin>>str){
if(crtStr != str){
crtStr = str;
}else{
cout<< (*str.begin()) <<" "<<toupper(*str.begin())<<endl;
if(static_cast<int>(*str.begin()) == toupper(*str.begin())){
b = true;
break;
}
}
}
if(b){
cout<< crtStr << endl;
}else{
cout<< "there is no sequential word." << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
5.25
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int a,b;
cin>>a >>b;
try{
if(b==0) throw runtime_error("b is zero");
cout<<static_cast<double>(a)/b<<endl;
}catch(runtime_error err){
cout<<err.what() << "\n Try Again."<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
6.4
#include <cstdlib>
#include <iostream>
using namespace std;
unsigned int f(unsigned int &i)
{
unsigned sum = 1;
while(i > 1){
sum *= i--;
}
return sum;
}
int main(int argc, char *argv[])
{
unsigned i;
cin>>i;
cout<<f(i)<<endl;
system("PAUSE");
6.5
#include <cstdlib>
#include <iostream>
using namespace std;
unsigned fabs(const int &i)
{
return (i > 0) ? i : -i;
}
int main(int argc, char *argv[])
{
int i;
cin>>i;
cout<<fabs(i)<<endl;
system("PAUSE");
return 0;
}
6.7
#include <cstdlib>
#include <iostream>
using namespace std;
unsigned f()
{
static int cnt;
return cnt++;
}
int main(int argc, char *argv[])
{
int i = 10;
while(i > 0){
cout<<f()<<endl;
i--;
}
system("PAUSE");
return 0;
}
6.8
#ifndef CHAPTER_6_H
#define CHAPTER_6_H
unsigned f();
#endif
#include <cstdlib>
#include <iostream>
#include "Chapter6.h"
using namespace std;
int main(int argc, char *argv[])
{
int i = 10;
while(i > 0){
cout<<f()<<endl;
i--;
}
system("PAUSE");
return 0;
}
unsigned f()
{
static int cnt;
return cnt++;
}
6.10
#include <cstdlib>
#include <iostream>
using namespace std;
void f(int* const p,int* const q)
{
int temp = *p;
*p = *q;
*q = temp;
}
int main(int argc, char *argv[])
{
int a = 1, b = 5;
f(&a, &b);
cout<<a<<" "<<b<<endl;
system("PAUSE");
return 0;
}
6.12
#include <cstdlib>
#include <iostream>
using namespace std;
void f(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
int main(int argc, char *argv[])
{
int a = 1, b = 5;
f(a, b);
cout<<a<<" "<<b<<endl;
system("PAUSE");
return 0;
}
6.22
#include <cstdlib>
#include <iostream>
using namespace std;
void f(const int *&p, const int *&q)
{
const int *temp = p;
p = q;
q = temp;
}
int main(int argc, char *argv[])
{
int a = 1, b = 5;
const int *p = &a, *q = &b;
cout<<p<<" "<<q<<endl;
f(p, q);
cout<<p<<" "<<q<<endl;
system("PAUSE");
return 0;
}
6.32
合法,因为返回的是左值。
#include <cstdlib>
#include <iostream>
using namespace std;
int &get(int *arry, int index)
{
return arry[index];
}
int main(int argc, char *argv[])
{
int ia[10];
for (int i = 0; i != 10; i++)
{
get(ia, i) = i;
}
for (int *i = begin(ia); i != end(ia); i++)
{
cout<<*i<<endl;
}
system("PAUSE");
return 0;
}
6.33
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
void f(vector<int> t, vector<int>::size_type i)
{
if(i == 1){
cout<< t[0] << " ";
}else{
f(t, i-1);
cout<<t[i-1]<<" ";
}
}
void set(vector<int> &t)
{
for (int i = 0; i < 3; i++)
{
t.push_back(i);
}
}
int main(int argc, char *argv[])
{
vector<int> v;
set(v);
f(v, v.size());
cout<<endl;
system("PAUSE");
return 0;
}
6.36 6.37
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
/*string (&fun(string (&max)[10]))[10]
{
max[3] = "9";
return max;
}*/
/*typedef string arrT[10];
//using arrT = string[10];
arrT& fun(string (&max)[10]){
max[3] = "9";
return max;
}*/
/*auto fun(string(&max)[10])->string(&)[10]
{
max[3] = "9";
return max;
}*/
string a[10] = {"1","2","3","4","5","6","7","8","9","0"};
decltype(a) & fun(string(&max)[10])
{
max[3] = "9";
return max;
}
int main(int argc, char *argv[])
{
string arr[10] = {"1","2","3","4","5","6","7","8","9","0"};
cout<<arr[3]<<endl;
fun(arr);
cout<<arr[3]<<endl;
system("PAUSE");
return 0;
}
6.54 6.55 6.56
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int add(const int &a,const int &b)
{
return a+b;
}
int subtract(const int &a,const int &b)
{
return a - b;
}
int multi(const int &a,const int &b)
{
return a*b;
}
int div(const int &a,const int &b)
{
return a/b;
}
int main(int argc, char *argv[])
{
vector<int (*)(const int&,const int&)> tt;
tt.push_back(add);
tt.push_back(subtract);
tt.push_back(multi);
tt.push_back(div);
for(auto i:tt){
cout<<i(6,3)<<endl;
}
system("PAUSE");
return 0;
}
7.9
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
struct Person
{
string name;
string address;
string getName() const {return name;}
string getAddress() const {return address;}
};
istream& read(istream &is, Person &p)
{
cout<<"input name and address:";
is >> p.name >> p.address;
return is;
}
ostream& print(ostream &os, const Person &p)
{
os<<"name is: "<< p.name <<" adderss is: "<< p.address;
return os;
}
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}
7.15
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
struct Person
{
string name;
string address;
Person(){};
Person(const string &n):name(n){};
Person(const string &n, const string &a):name(n),address(a){};
Person(istream &is);
string getName() const {return name;}
string getAddress() const {return address;}
};
istream& read(istream &is, Person &p)
{
cout<<"input name and address:";
is >> p.name >> p.address;
return is;
}
ostream& print(ostream &os, const Person &p)
{
os<<"name is: "<< p.name <<" adderss is: "<< p.address;
return os;
}
Person::Person(istream &is)
{
read(is, *this);
}
int main(int argc, char *argv[])
{
system("PAUSE");
return 0;
}
7.22
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Person
{
friend istream& read(istream &is, Person &p);
friend ostream& print(ostream &os, const Person &p);
private:
string name;
string address;
public:
Person(){};
Person(const string &n):name(n){};
Person(const string &n, const string &a):name(n),address(a){};
Person(istream &is);
string getName() const {return name;}
string getAddress() const {return address;}
};
istream& read(istream &is, Person &p)
{
cout<<"input name and address:";
is >> p.name >> p.address;
return is;
}
ostream& print(ostream &os, const Person &p)
{
os<<"name is: "<< p.name <<" adderss is: "<< p.address;
return os;
}
Person::Person(istream &is)
{
read(is, *this);
}
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}
7.23 7.24
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Screen
{
public:
typedef string::size_type pos;
Screen(){};
Screen(pos h, pos w):cursor(0), height(h), width(w),contents(h*w, ' '){};
Screen(pos h, pos w, char c):cursor(0),height(h), width(w),contents(h*w, c){};
char get() const
{
return contents[cursor];
}
inline char get(pos ht, pos wd) const;
Screen &move(pos r, pos c);
private:
pos cursor;
pos height, width;
string contents;
};
inline Screen &Screen::move(pos r, pos c)
{
cursor = r*width + c;
return *this;
}
char Screen::get(pos ht, pos wd) const
{
return contents[ht*width+wd];
}
class Window_mgr
{
public:
Window_mgr()
{
screens.push_back(Screen(24,80));
};
private:
vector<Screen> screens;
};
int main(int argc, char *argv[])
{
system("PAUSE");
return 0;
}
7.27
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Screen
{
public:
typedef string::size_type pos;
Screen(){};
Screen(pos h, pos w):cursor(0), height(h), width(w),contents(h*w, ' '){};
Screen(pos h, pos w, char c):cursor(0),height(h), width(w),contents(h*w, c){};
char get() const
{
return contents[cursor];
}
inline char get(pos ht, pos wd) const;
Screen &move(pos r, pos c);
Screen &set(char);
Screen &set(pos, pos, char);
Screen &display(ostream &os)
{
do_display(os);
return *this;
}
const Screen &display(ostream &os) const
{
do_display(os);
return *this;
}
private:
pos cursor;
pos height, width;
string contents;
void do_display(ostream &os) const
{
os<<contents;
}
};
inline Screen &Screen::move(pos r, pos c)
{
cursor = r*width + c;
return *this;
}
char Screen::get(pos ht, pos wd) const
{
return contents[ht*width+wd];
}
inline Screen &Screen::set(char ch)
{
contents[cursor] = ch;
return *this;
}
inline Screen &Screen::set(pos r, pos c, char ch)
{
contents[r*width+c] = ch;
return *this;
}
class Window_mgr
{
public:
Window_mgr()
{
screens.push_back(Screen(24,80));
};
private:
vector<Screen> screens;
};
int main(int argc, char *argv[])
{
Screen myScreen(5,5,'X');
myScreen.move(4,0).set('#').display(cout);
cout<<endl;
myScreen.display(cout);
cout<<endl;
system("PAUSE");
return 0;
}
7.31
#include <cstdlib>
#include <iostream>
using namespace std;
class Y;
class X
{
Y *y;
};
class Y
{
X x;
};
int main(int argc, char *argv[])
{
X x;
Y y;
system("PAUSE");
return 0;
}
7.34 用VS2012 编译不能通过,用GCC 编译可以通过。
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Screen;
class Window_mgr
{
public:
typedef vector<Screen>::size_type ScreenIndex;
//using ScreenIndex = vector<Screen>::size_type;
void clear(ScreenIndex);
Window_mgr()
{
//screens.push_back(Screen(24,80));
};
private:
vector<Screen> screens;
};
class Screen
{
//friend class Window_mgr;
friend void Window_mgr::clear(ScreenIndex);
public:
typedef string::size_type pos;
Screen(){};
Screen(pos h, pos w):cursor(0), height(h), width(w),contents(h*w, ' '){};
Screen(pos h, pos w, char c):cursor(0),height(h), width(w),contents(h*w, c){};
char get() const
{
return contents[cursor];
}
inline char get(pos ht, pos wd) const;
Screen &move(pos r, pos c);
Screen &set(char);
Screen &set(pos, pos, char);
Screen &display(ostream &os)
{
do_display(os);
return *this;
}
const Screen &display(ostream &os) const
{
do_display(os);
return *this;
}
private:
pos cursor;
pos height, width;
string contents;
void do_display(ostream &os) const
{
os<<contents;
}
};
void Window_mgr::clear(ScreenIndex i)
{
Screen &s = screens[i];
//screens[i].contents = string(s.height*s.width, ' ');
s.contents = string(s.height*s.width, ' ');
};
inline Screen &Screen::move(pos r, pos c)
{
cursor = r*width + c;
return *this;
}
char Screen::get(pos ht, pos wd) const
{
return contents[ht*width+wd];
}
inline Screen &Screen::set(char ch)
{
contents[cursor] = ch;
return *this;
}
inline Screen &Screen::set(pos r, pos c, char ch)
{
contents[r*width+c] = ch;
return *this;
}
int main(int argc, char *argv[])
{
/* Screen myScreen(5,5,'X');
myScreen.move(4,0).set('#').display(cout);
cout<<endl;
myScreen.display(cout);
cout<<endl;*/
system("PAUSE");
return 0;
}
7.33
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Screen;
class Window_mgr
{
public:
typedef vector<Screen>::size_type ScreenIndex;
//using ScreenIndex = vector<Screen>::size_type;
void clear(ScreenIndex);
Window_mgr()
{
// screens.push_back(Screen(24,80));
};
void addScreen(Screen &s)
{
screens.push_back(s);
}
private:
vector<Screen> screens;
};
class Screen
{
friend class Window_mgr;
//friend void Window_mgr::clear(Screen &);
public:
typedef string::size_type pos;
Screen(){};
Screen(pos h, pos w):cursor(0), height(h), width(w),contents(h*w, ' '){};
Screen(pos h, pos w, char c):cursor(0),height(h), width(w),contents(h*w, c){};
char get() const
{
return contents[cursor];
}
inline char get(pos ht, pos wd) const;
Screen &move(pos r, pos c);
Screen &set(char);
Screen &set(pos, pos, char);
Screen &display(ostream &os)
{
do_display(os);
return *this;
}
const Screen &display(ostream &os) const
{
do_display(os);
return *this;
}
pos size() const;
private:
pos cursor;
pos height, width;
string contents;
void do_display(ostream &os) const
{
os<<contents;
}
};
void Window_mgr::clear(ScreenIndex i)
{
Screen &s = screens[i];
//screens[i].contents = string(s.height*s.width, ' ');
s.contents = string(s.height*s.width, ' ');
};
inline Screen &Screen::move(pos r, pos c)
{
cursor = r*width + c;
return *this;
}
char Screen::get(pos ht, pos wd) const
{
return contents[ht*width+wd];
}
inline Screen &Screen::set(char ch)
{
contents[cursor] = ch;
return *this;
}
inline Screen &Screen::set(pos r, pos c, char ch)
{
contents[r*width+c] = ch;
return *this;
}
inline Screen::pos Screen::size() const
{
return this->height*this->width;
};
int main(int argc, char *argv[])
{
Screen myScreen(5,5,'X');
myScreen.move(4,0).set('#').display(cout);
cout<<endl;
myScreen.display(cout);
cout<<myScreen.size()<<endl;
system("PAUSE");
return 0;
}
7.36
#include <cstdlib>
#include <iostream>
using namespace std;
struct X
{
X(int i, int j):base(i), rem(base % j){}
int base, rem;
};
int main(int argc, char *argv[])
{
X x(5,2);
cout<<x.rem<<" "<<x.base;
system("PAUSE");
return 0;
}
7.39
合法,它会寻找最佳匹配项。
7.42 先执行“struct....“的内容,再显示"default"。(从此开始使用VS2013 IDE)
#include <cstdlib>
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass(int i, int j, int k) :a(i), b(j), c(k)
{ cout<<"struct " << "a " << a << " b " << b << " c " << c << endl; }
MyClass() :MyClass(1, 2, 3){ cout << "default" << endl; }
private:
int a, b, c;
};
int main(int argc, char *argv[])
{
MyClass mc;
system("PAUSE");
return 0;
}