CPP04 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/05/20 16:29:42 by kamitsui #+# #+# */
9/* Updated: 2025/06/03 20:52:59 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
24#include "Animal.hpp"
25#include "Brain.hpp"
26#include "Cat.hpp"
27#include "Dog.hpp"
28#include "ScopedPointer.hpp"
29#include "WrongAnimal.hpp"
30#include "WrongCat.hpp"
31#include <cstdlib>
32#include <iostream>
33#include <new>
34
35static int arrayAnimals(void);
36static void deepCopyTests(void);
37static void stackTests(void);
38
43int main() {
44 const Animal *j = new Dog();
45 const Animal *i = new Cat();
46
47 delete j; // should not create a leak
48 delete i;
49
50 arrayAnimals();
51 deepCopyTests();
52 stackTests();
53
54 return 0;
55}
56
64static int arrayAnimals(void) {
65 std::cout << "\n--- Array of Animals ---" << std::endl;
66 // ScopedPointer の配列を使用する
67 // std::vector<ScopedPointer<Animal> > animals(10); // C++98でstd::vectorとScopedPointerを組み合わせるなら
68 // 配列の要素数が多い場合はヒープに確保
69 // ScopedPointer<Animal>* animals = 0; // C++98 nullptr
70 ScopedPointer<Animal> animals[10]; // スタック上の固定サイズ配列
71
72 try {
73 // --- 割り当て失敗のシミュレーション設定 (テスト用) ---
74 // g_alloc_counter = 0; // コメントアウト、グローバルに影響するため注意
75 // g_fail_at_alloc_count = -1; // コメントアウト、グローバルに影響するため注意
76
77 for (int k = 0; k < 10; ++k) {
78 if (k < 5) {
79 animals[k].reset(new Dog());
80 } else {
81 animals[k].reset(new Cat());
82 }
83 // ここで new が失敗すると std::bad_alloc がスローされ、
84 // それまでに成功した animals[0]...animals[k-1] のデストラクタが自動的に呼ばれる。
85 }
86
87 std::cout << "\n--- Making Sounds in Animal Array ---" << std::endl;
88 for (int k = 0; k < 10; ++k) {
89 // 防御的な null チェック (new が失敗した場合に備える)
90 if (animals[k].get()) {
91 std::cout << "Animal [" << k << "] (type: " << animals[k]->getType() << ") says: " << std::endl;
92 animals[k]->makeSound();
93 } else {
94 std::cerr << "Array element " << k << " is null, skipping sound." << std::endl;
95 }
96 }
97
98 // 明示的な delete は不要。スコープを抜ける際に自動的に解放される。
99 // for (int k = 0; k < 10; ++k) {
100 // delete animals[k];
101 // }
102
103 } catch (const std::bad_alloc &e) {
104 std::cerr << "Memory allocation failed in arrayAnimals! " << e.what() << std::endl;
105 // 例外捕捉後、既に割り当てた ScopedPointer のデストラクタが自動で呼ばれる。
106 return EXIT_FAILURE; // エラーとして終了
107 } catch (const std::exception &e) {
108 std::cerr << "An unexpected standard error occurred in arrayAnimals: " << e.what() << std::endl;
109 return EXIT_FAILURE;
110 } catch (...) {
111 std::cerr << "An unknown error occurred in arrayAnimals." << std::endl;
112 return EXIT_FAILURE;
113 }
114
115 // 関数終了時に animals 配列の ScopedPointer オブジェクトがスコープを抜ける。
116 // その際にそれぞれのデストラクタが呼ばれ、内部の Animal オブジェクトが解放される。
117 std::cout << "\n--- Array of Animals finished ---" << std::endl;
118 return 0;
119}
120
128static void deepCopyTests(void) {
129 std::cout << "\n--- Deep Copy Tests ---" << std::endl;
130
131 // Copy Constructor Test
132 Dog originalDog;
133 originalDog.getBrain()->setIdea(0, "Bone!");
134 Dog copiedDog = originalDog; // Using copy constructor
135
136 std::cout << "Original Dog's first idea: " << originalDog.getBrain()->getIdea(0) << std::endl;
137 std::cout << "Copied Dog's first idea: " << copiedDog.getBrain()->getIdea(0) << std::endl;
138
139 copiedDog.getBrain()->setIdea(0, "Squirrel!");
140 std::cout << "Original Dog's first idea after copied Dog changed: " << originalDog.getBrain()->getIdea(0)
141 << std::endl;
142 std::cout << "Copied Dog's first idea after copied Dog changed: " << copiedDog.getBrain()->getIdea(0) << std::endl;
143
144 // Copy Assignment Operator Test
145 Cat originalCat;
146 originalCat.getBrain()->setIdea(0, "Fish!");
147 Cat assignedCat;
148 assignedCat = originalCat; // Using copy assignment operator
149
150 std::cout << "Original Cat's first idea: " << originalCat.getBrain()->getIdea(0) << std::endl;
151 std::cout << "Assigned Cat's first idea: " << assignedCat.getBrain()->getIdea(0) << std::endl;
152
153 assignedCat.getBrain()->setIdea(0, "Nap!");
154 std::cout << "Original Cat's first idea after assigned Cat changed: " << originalCat.getBrain()->getIdea(0)
155 << std::endl;
156 std::cout << "Assigned Cat's first idea after assigned Cat changed: " << assignedCat.getBrain()->getIdea(0)
157 << std::endl;
158}
159
166static void stackTests(void) {
167 std::cout << "\n--- More Tests (Stack Objects) ---" << std::endl;
168 {
169 Dog stackDog;
170 stackDog.getBrain()->setIdea(50, "Walkies?");
171 std::cout << "Stack Dog's 50th idea: " << stackDog.getBrain()->getIdea(50) << std::endl;
172 } // stackDog and its Brain are destroyed here
173 {
174 Cat stackCat;
175 stackCat.getBrain()->setIdea(99, "Laser pointer!");
176 std::cout << "Stack Cat's 99th idea: " << stackCat.getBrain()->getIdea(99) << std::endl;
177 } // stackCat and its Brain are destroyed here
178}
The Animal class represents a generic animal.
Definition Animal.hpp:37
const std::string & getIdea(int index) const
Gets an idea from a specific index in the brain. Performs bounds checking. If the index is out of bou...
Definition Brain.cpp:92
void setIdea(int index, const std::string &idea)
Sets an idea at a specific index in the brain. Performs bounds checking to ensure the index is valid ...
Definition Brain.cpp:80
The Cat class represents a feline animal.
Definition Cat.hpp:33
Brain * getBrain() const
Gets a pointer to the Cat's Brain object.
Definition Cat.cpp:98
The Dog class represents a canine animal.
Definition Dog.hpp:33
Brain * getBrain() const
Gets a pointer to the Dog's Brain object.
Definition Dog.cpp:94
void reset(T *ptr=0)
T endl(T... args)
int main()
Main function of the program.
Definition main.cpp:35
Declares the abstract Animal base class.
Declares the Brain class.
Declares the Cat class.
Declares the Dog class.
Declares the WrongAnimal class.
Declares the WrongCat class.
ScopedPointer Class is Custom smart pointer with RAII pattern applied.
T what(T... args)