Skip to content

Commit

Permalink
changed ex7_23_24_25.cpp to ex7_23.h/cpp and ex7_24.h
Browse files Browse the repository at this point in the history
1. `const pos h`'s `const` is unnecessary.
2. the second contructs get contents by a `while` loop. unnecessary.
3. the third contructs may not get the point of question. (**a character**)
4. we should add inline member functions in this exercise.
  • Loading branch information
pezy committed Nov 14, 2014
1 parent 6740a89 commit 03265a9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 56 deletions.
7 changes: 6 additions & 1 deletion ch07/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,10 @@ the interface should be defined as public, the data shouldn't expose to outside
- code verbosity, declarations inside the class, outside the class.
## [Exercise 7.21](ex7_21.h)
## [Exercise 7.22](ex7_22.h)
## Exercise 7.23 [Header](ex7_23.h)|[CPP](ex7_23.cpp)
## [Exercise 7.24](ex7_24.h)
## Exercise 7.25
The class below can rely on it. It goes in Section 7.1.5:"..the synthesized versions are unlikely to work correctly for classes that allocate resources that reside outside the class objects themselves. " and "Moreover, the synthesized versions for copy, assignment, and destruction work correctly for classes that have vector or string members. " Hence the class below which used only built-in type and strings can rely on the default version of copy and assignment. (@Mooophy)
23 changes: 23 additions & 0 deletions ch07/ex7_23.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// ex7_23.cpp
// Exercise 7.23
//
// Created by pezy on 11/14/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

//#include "ex7_23.h"
#include "ex7_24.h"

char Screen::get(pos r, pos c) const
{
pos row = r * width;
return contents[row + c];
}

Screen& Screen::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
31 changes: 31 additions & 0 deletions ch07/ex7_23.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// ex7_23.h
// Exercise 7.23
//
// Created by pezy on 11/14/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#ifndef CP5_ex7_23_h
#define CP5_ex7_23_h

#include <string>

class Screen {
public:
using pos = std::string::size_type;

Screen() = default;
Screen(pos ht, pos wd, char c):height(ht),width(wd),contents(ht*wd, c){}

char get() const { return contents[cursor]; }
inline char get(pos r, pos c) const;
inline Screen& move(pos r, pos c);

private:
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
};

#endif
55 changes: 0 additions & 55 deletions ch07/ex7_23_24_25.cpp

This file was deleted.

32 changes: 32 additions & 0 deletions ch07/ex7_24.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// ex7_24.cpp
// Exercise 7.24
//
// Created by pezy on 11/14/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#ifndef CP5_ex7_24_h
#define CP5_ex7_24_h

#include <string>

class Screen {
public:
using pos = std::string::size_type;

Screen() = default; // 1
Screen(pos ht, pos wd, pos n):height(ht),width(wd),contents(n, ' '){} // 2
Screen(pos ht, pos wd, char c):height(ht),width(wd),contents(ht*wd, c){} // 3

char get() const { return contents[cursor]; }
inline char get(pos r, pos c) const;
inline Screen& move(pos r, pos c);

private:
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
};

#endif

0 comments on commit 03265a9

Please sign in to comment.