Skip to content

Commit

Permalink
cpp code example for exercise in "producing libraries" session
Browse files Browse the repository at this point in the history
  • Loading branch information
code4yonglei committed Sep 4, 2024
1 parent 3d817ae commit 1def96b
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 0 deletions.
14 changes: 14 additions & 0 deletions content/code/01_libraries-cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# FIXME set minimum cmake version
cmake_minimum_required(...)

# FIXME declare project giving project name and language
project(... LANGUAGES ...)

# FIXME create the message library from the source files
add_library(...)

# FIXME create hello-world executable from hello-world.cpp
add_executable(hello-world ...)

# FIXME link hello-world to the message library
target_link_libraries(hello-world ...)
11 changes: 11 additions & 0 deletions content/code/01_libraries-cxx/Message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "Message.hpp"

#include <iostream>
#include <string>

std::ostream &Message::printObject(std::ostream &os) {
os << "This is my very nice message: " << std::endl;
os << message_;

return os;
}
17 changes: 17 additions & 0 deletions content/code/01_libraries-cxx/Message.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <iosfwd>
#include <string>

class Message {
public:
Message(const std::string &m) : message_(m) {}

friend std::ostream &operator<<(std::ostream &os, Message &obj) {
return obj.printObject(os);
}

private:
std::string message_;
std::ostream &printObject(std::ostream &os);
};
16 changes: 16 additions & 0 deletions content/code/01_libraries-cxx/hello-world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Message.hpp"

#include <cstdlib>
#include <iostream>

int main() {
Message say_hello("Hello, CMake World!");

std::cout << say_hello << std::endl;

Message say_goodbye("Goodbye, CMake World");

std::cout << say_goodbye << std::endl;

return EXIT_SUCCESS;
}
15 changes: 15 additions & 0 deletions content/code/01_libraries-cxx/solution/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# set minimum cmake version
cmake_minimum_required(VERSION 3.18)

# project name and language
project(libraries LANGUAGES CXX)

# generate a library from sources
add_library(message
Message.hpp
Message.cpp
)

add_executable(hello-world hello-world.cpp)

target_link_libraries(hello-world PRIVATE message)
11 changes: 11 additions & 0 deletions content/code/01_libraries-cxx/solution/Message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "Message.hpp"

#include <iostream>
#include <string>

std::ostream &Message::printObject(std::ostream &os) {
os << "This is my very nice message: " << std::endl;
os << message_;

return os;
}
17 changes: 17 additions & 0 deletions content/code/01_libraries-cxx/solution/Message.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <iosfwd>
#include <string>

class Message {
public:
Message(const std::string &m) : message_(m) {}

friend std::ostream &operator<<(std::ostream &os, Message &obj) {
return obj.printObject(os);
}

private:
std::string message_;
std::ostream &printObject(std::ostream &os);
};
16 changes: 16 additions & 0 deletions content/code/01_libraries-cxx/solution/hello-world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Message.hpp"

#include <cstdlib>
#include <iostream>

int main() {
Message say_hello("Hello, CMake World!");

std::cout << say_hello << std::endl;

Message say_goodbye("Goodbye, CMake World");

std::cout << say_goodbye << std::endl;

return EXIT_SUCCESS;
}

0 comments on commit 1def96b

Please sign in to comment.