#include "stdafx.h"
#include <iostream>
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 ht, pos wd) const;
Screen &move(pos r, pos c);
private:
pos cursor = 0;
pos height = 0 , width = 0;
std::string contents;
};
using namespace std;
int main()
{
system("pause");
return 0;
}
// C++primer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
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 ht, pos wd) const;
Screen &move(pos r, pos c);
public:
Screen &set(char);
Screen &set(pos, pos, char);
private:
pos cursor = 0;
pos height = 0 , width = 0;
std::string contents;
};
inline Screen &Screen::set(char c)
{
contents[cursor] = c;
return *this;
}
inline Screen &Screen::set(pos r, pos col, char ch)
{
contents[r*width + col] = ch;
return *this;
}
using namespace std;
int main()
{
Screen myScreen(5, 5, 'X');
myScreen.move(4, 0).set('#');
cout << endl;
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Screen {
unsigned height = 0, width = 0;
unsigned cursor = 0;
string contents;
public:
Screen() = default;
Screen(unsigned ht, unsigned wd) :height(ht), width(wd), contents(ht*wd, ' ') {}
Screen(unsigned ht, unsigned wd, char c) :height(ht), width(wd), contents(ht*wd, c) {}
Screen& move(unsigned r, unsigned c)
{
cursor = r*width + c;
return *this;
}
Screen& set(char ch)
{
contents[cursor] = ch;
return *this;
}
Screen& set(unsigned r, unsigned c, char ch)
{
contents[r*width + c] = ch;
return *this;
}
Screen& display()
{
cout << contents;
return *this;
}
};
int main()
{
Screen myScreen(5, 5, 'X');
myScreen.move(4, 0).set('#').display();
cout << endl;
myScreen.display(); //如果取消&符号,上一排输出的是副本,然后这就是新的就没有#
system("pause");
return 0;
}
-
C++
107 引用 • 153 回帖
C++ 是在 C 语言的基础上开发的一种通用编程语言,应用广泛。C++ 支持多种编程范式,面向对象编程、泛型编程和过程化编程。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于