c_cpp 适用于C程序员的C ++第2周

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 适用于C程序员的C ++第2周相关的知识,希望对你有一定的参考价值。

#include <iostream>

using std::cout;
using std::endl;
using std::ostream;
using std::string;

#define DEBUG false
void log(string s) {
  DEBUG && (cout << "[" << __FILE__ << "] " << s << endl);
}

class Point
{
public:
  // Constructor
  Point(double aX, double aY)
    : mX(aX)
    , mY(aY)
  {
  }

  // Deconstructor
  ~Point()
  {
  }

  friend Point operator+(const Point& aP1, const Point& aP2)
  {
    return Point(aP1.mX + aP2.mX, aP1.mY + aP2.mY);
  }

  // Overwrite the output stream
  friend ostream& operator<<(ostream& out, const Point& p)
  {
    out << "(" << p.mX << ", " << p.mY << ")";
    return out;
  }

private:
  double mX;
  double mY;
};

int main() {
  Point a(1.2, 3.4);
  cout << a << endl;  // (1.2, 3.4)

  Point b(5.6, 7.8);
  cout << b << endl;  // (5.6, 7.8)

  Point c = a + b;
  cout << c << endl;  // (6.8, 11.2)

  return 0;
}
/*
 * Compile this file by C++11, e.g., g++ --std=c++11 <filename.cpp>
 */
#include <cstdint>  // For using fixed width integer types and part of
                    // C numeric limits interface:
                    // std::int8_t
#include <iostream> // For input and output fundamentals:
                    // std::cout
                    //      endl
                    //      ostream
#include <string>   // For C++ standard string classes and templates:
                    // std::string

using std::int8_t;  // used as the underlying type of enum.

using std::cout;
using std::endl;
using std::ostream; // for << operator overloading.

using std::string;

#define DEBUG false
void log(string s) {
  DEBUG && (cout << "[" << __FILE__ << "] " << s << endl);
}

// A scoped enumerations DAY whose underlying type is int8_t.
// (Unscoped enumeration has no keyword `class`.)
enum class DAY:int8_t {
  SUNDAY,
  MONDAY,
  TUESDAY,
  WEDNESDAY,
  THURSDAY,
  FRIDAY,
  SATURDAY
};

// Overwrite the increment of enum DAY.
DAY& operator++(DAY& d) // Prefix operator: ++DAY
{
  log("Prefix ++");
  // Wrap around to 0 if the result after increment reaches 7.
  d = static_cast<DAY>((static_cast<int>(d) + 1) % 7);
  return d;
}

DAY operator++(DAY& d, int) // Postfix operator: DAY++
{
  log("Postfix ++");
  // Create a temporary variable with our current value.
  DAY beforeOperation = d;

  // Use prefix operator to increase.
  ++d;

  // Return temporary result.
  return beforeOperation;
}

// Overwrite the decrement of enum DAY.
DAY& operator--(DAY& d) // Prefix operator: --DAY
{
  log("Prefix --");
  // Wrap round to 6 if the result after decrement is -1.
  d = static_cast<DAY>((static_cast<int>(d) + 6) % 7);
  return d;
}

DAY operator--(DAY& d, int) // Postfix operator: DAY--
{
  log("Postfix --");

  // Create a temporary variable with our current value.
  DAY beforeOperation = d;

  // Use prefix operator to decrease.
  --d;

  // Return temporary result.
  return beforeOperation;
}

// Overwrite the output stream of enum DAY.
ostream& operator<<(ostream& out, const DAY& d)
{
  std::string name[] = {
    "Sunday",
    "Monday",
    "Tuesday",
    "Wedensday",
    "Thursday",
    "Friday",
    "Saturday"
  };
  out << name[static_cast<int>(d)];
  return out;
}

int main() {
  DAY day = DAY::SATURDAY;
  cout << day << endl;    // output: Saturday ( day is Saturday )
  cout << ++day << endl;  // output: Sunday   ( day is Sunday )
  cout << day++ << endl;  // output: Sunday   ( day is Monday )
  cout << day << endl;    // output: Monday   ( day is Monday )
  cout << day-- << endl;  // output: Monday   ( day is Sunday )
  cout << day << endl;    // output: Sunday   ( day is Sunday )
  cout << --day << endl;  // output: Saturday ( day is Saturday )
  return 0;
}

以上是关于c_cpp 适用于C程序员的C ++第2周的主要内容,如果未能解决你的问题,请参考以下文章

《Java程序设计》第3周学习总结

编程语言中const是啥意思,用来干啥的,怎么用(语法),适用于哪几种语言

c_cpp 第1周马里奥2

201671010111 2016-2017-2《Java程序设计》再看java

20165318 2017-2018-2 《Java程序设计》第二周学习总结

# 20175329 2018-2019-2 《Java程序设计》 第二周学习总结