diff options
Diffstat (limited to 'graphs/cpp/const')
| -rw-r--r-- | graphs/cpp/const/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | graphs/cpp/const/person.cc | 25 | ||||
| -rw-r--r-- | graphs/cpp/const/person.hh | 17 | ||||
| -rw-r--r-- | graphs/cpp/const/test_person.cc | 20 |
4 files changed, 68 insertions, 0 deletions
diff --git a/graphs/cpp/const/CMakeLists.txt b/graphs/cpp/const/CMakeLists.txt new file mode 100644 index 0000000..800759f --- /dev/null +++ b/graphs/cpp/const/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21.2) +project(const) + +add_compile_options(-Wall -Wextra -Werror -pedantic -std=c++20 -Wold-style-cast) + +add_executable(const test_person.cc person.cc)
\ No newline at end of file diff --git a/graphs/cpp/const/person.cc b/graphs/cpp/const/person.cc new file mode 100644 index 0000000..63e8d73 --- /dev/null +++ b/graphs/cpp/const/person.cc @@ -0,0 +1,25 @@ +// +// Created by martial.simon on 2/24/25. +// +#include "person.hh" + +Person::Person(const std::string& name, const unsigned int age) + : name_{ name } + , age_{ age } +{} +std::string Person::get_name() const +{ + return name_; +} +unsigned int Person::get_age() const +{ + return age_; +} +void Person::set_name(const std::string& name) +{ + name_ = name; +} +void Person::set_age(const unsigned int age) +{ + age_ = age; +} diff --git a/graphs/cpp/const/person.hh b/graphs/cpp/const/person.hh new file mode 100644 index 0000000..0b44308 --- /dev/null +++ b/graphs/cpp/const/person.hh @@ -0,0 +1,17 @@ +#pragma once + +#include <string> + +class Person +{ +public: + Person(const std::string& name, const unsigned int age); + std::string get_name() const; + unsigned int get_age() const; + void set_name(const std::string& name); + void set_age(unsigned int age); + +private: + std::string name_; + unsigned int age_; +}; diff --git a/graphs/cpp/const/test_person.cc b/graphs/cpp/const/test_person.cc new file mode 100644 index 0000000..f59286e --- /dev/null +++ b/graphs/cpp/const/test_person.cc @@ -0,0 +1,20 @@ +#include <iostream> + +#include "person.hh" + +int main() +{ + Person my_person("Gordon Freeman", 47); + + const Person& my_person2 = my_person; + + std::cout << "name: " << my_person2.get_name() + << ", years: " << my_person2.get_age() << std::endl; + + std::string name = "Alyx Vance"; + my_person.set_name(name); + my_person.set_age(24); + + std::cout << "name: " << my_person2.get_name() + << ", years: " << my_person2.get_age() << std::endl; +} |
