Commit 84639a57 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+21 −0
Changes for Inheritance and Polymorphism with C++ - Working Files/Chapter 1/0105 - Basics of templates - Example.cpp: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include <iostream>

template<typename T>
struct MyStruct
{
  T data;
};


template<typename U>
auto make_struct(U u)
{
  return MyStruct<U>{ u };
}

int main()
{
  auto f = make_struct(5);

  std::cout << f.data << '\n';
}
+25 −0
Changes for Inheritance and Polymorphism with C++ - Working Files/Chapter 2/0202 - Friends - Example.cpp: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
struct Base
{
  public:    int i;
  protected: int j;
  private:   int k;
};

struct Derived : Base
{
  public:    int l;
  protected: int m;
  private:   int n;  
};

int main()
{
  Derived d;
  
  d.i = 1;
  d.j = 2;
  d.k = 3;
  d.l = 4;
  d.m = 5;
  d.n = 6;
}
 No newline at end of file
+32 −0
Changes for Inheritance and Polymorphism with C++ - Working Files/Chapter 2/0203 - Local Example.cpp: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
class Other {
  int j = 3;
  friend class Test;
};

class Test {
  int i;

public:
  int get_a_value() const {
    struct Local {
      Local(Other &o)
        : m_o(o)
      { }

      int get_other_value() {
        return m_o.j;
      }

      Other &m_o;
    };

    Other o;
    Local i(o);
    return i.get_other_value();
  }
};

int main() {
  Test t;
  return t.get_a_value();
}
+16 −0
Changes for Inheritance and Polymorphism with C++ - Working Files/Chapter 2/0203 - Nested Example.cpp: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
class Test {
  int i = 0;

public:
  struct Nested {
    static int get_private_i(const Test &t) {
      return t.i;
    }
  };
};

int main()
{
  Test t;
  return Test::Nested::get_private_i(t);
}
+23 −0
Changes for Inheritance and Polymorphism with C++ - Working Files/Chapter 2/0204 - Virtual Functions And Abstract Base Classes - Example.cpp: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
class Base
{
public:
  virtual ~Base() = default;
  Base() = default;
  Base(const Base &) = default;
  Base(Base &&) = default;
  Base &operator=(const Base &) = default;
  Base &operator=(Base &&) = default;

  virtual void do_something() = 0;
};

class Derived : public Base
{
  void do_something() {}
};


int main()
{
  Derived d;
}
Loading