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 23:50:36 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
24#include "Animal.hpp" // Now abstract
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
34static int polymorphismWithConcreateClasses(void);
35static int arrayAnimals(void);
36static void deepCopyTests(void);
37static void stackTests(void);
38
47int main() {
48 polymorphismWithConcreateClasses();
49 arrayAnimals();
50 deepCopyTests();
51 stackTests();
52
53 std::cout << "\n--- Attempting to Instantiate Animal (Should Fail at Compile Time) ---" << std::endl;
54 // The following line will now cause a compile-time error:
55 // Animal basicAnimal;
56
57 return 0;
58}
59
66static int polymorphismWithConcreateClasses(void) {
67 std::cout << "--- Testing Polymorphism with Concrete Classes ---" << std::endl;
70
71 try {
72 j.reset(new Dog()); // new が失敗したら std::bad_alloc をスロー
73 i.reset(new Cat()); // new が失敗したら std::bad_alloc をスロー
74
75 // 防御的 null チェック
76 if (j.get()) {
77 std::cout << j->getType() << " " << std::endl;
78 j->makeSound();
79 }
80 if (i.get()) {
81 std::cout << i->getType() << " " << std::endl;
82 i->makeSound();
83 }
84
85 // 明示的な delete は不要。ScopedPointer のデストラクタがスコープを抜けるときに呼ばれる。
86
87 } catch (const std::bad_alloc &e) {
88 std::cerr << "Memory allocation failed in polymorphismWithConcreteClasses! " << e.what() << std::endl;
89 return EXIT_FAILURE;
90 } catch (const std::exception &e) {
91 std::cout << "An unexpected standard error occurred in polymorphismWithConcreteClasses: " << e.what()
92 << std::endl;
93 return EXIT_FAILURE;
94 } catch (...) {
95 std::cout << "An unknown error occurred in polymorphismWithConcreteClasses." << std::endl;
96 return EXIT_FAILURE;
97 }
98
99 std::cout << "--- Polymorphism with Concrete Classes finished ---" << std::endl;
100 return 0;
101}
102
109static int arrayAnimals(void) {
110 std::cout << "\n--- Array of Animals (Concrete Types) ---" << std::endl;
111
112 // ScopedPointer の配列を使用する
113 // スタック上の固定サイズ配列なので、配列自体はnewされない
114 ScopedPointer<Animal> animals[10];
115
116 try {
117 for (int k = 0; k < 10; ++k) {
118 if (k < 5) {
119 animals[k].reset(new Dog());
120 } else {
121 animals[k].reset(new Cat());
122 }
123 // ここで new が失敗すると std::bad_alloc がスローされ、
124 // それまでに成功した animals[0]...animals[k-1] のデストラクタが自動的に呼ばれる。
125 }
126
127 std::cout << "\n--- Making Sounds in Animal Array ---" << std::endl;
128 for (int k = 0; k < 10; ++k) {
129 // 防御的な null チェック
130 if (animals[k].get()) {
131 std::cout << "Animal [" << k << "] (type: " << animals[k]->getType() << ") says: ";
132 animals[k]->makeSound();
133 } else {
134 std::cerr << "Array element " << k << " is null, skipping makeSound() call." << std::endl;
135 }
136 }
137
138 // 明示的な delete は不要。スコープを抜ける際に自動的に解放される。
139
140 } catch (const std::bad_alloc &e) {
141 std::cerr << "Memory allocation failed in arrayAnimals! " << e.what() << std::endl;
142 // 例外捕捉後、既に割り当てた ScopedPointer のデストラクタが自動で呼ばれる。
143 return EXIT_FAILURE;
144 } catch (const std::exception &e) {
145 std::cerr << "An unexpected standard error occurred in arrayAnimals: " << e.what() << std::endl;
146 return EXIT_FAILURE;
147 } catch (...) {
148 std::cerr << "An unknown error occurred in arrayAnimals." << std::endl;
149 return EXIT_FAILURE;
150 }
151
152 std::cout << "\n--- Array of Animals finished ---" << std::endl;
153 return 0;
154}
155
162static void deepCopyTests(void) {
163 std::cout << "\n--- Deep Copy Tests (Still Valid) ---" << std::endl;
164
165 // Copy Constructor Test
166 Dog originalDog;
167 originalDog.getBrain()->setIdea(0, "Bone!");
168 Dog copiedDog = originalDog;
169
170 std::cout << "Original Dog's first idea: " << originalDog.getBrain()->getIdea(0) << std::endl;
171 std::cout << "Copied Dog's first idea: " << copiedDog.getBrain()->getIdea(0) << std::endl;
172
173 copiedDog.getBrain()->setIdea(0, "Squirrel!");
174 std::cout << "Original Dog's first idea after copied Dog changed: " << originalDog.getBrain()->getIdea(0)
175 << std::endl;
176 std::cout << "Copied Dog's first idea after copied Dog changed: " << copiedDog.getBrain()->getIdea(0) << std::endl;
177
178 // Copy Assignment Operator Test
179 Cat originalCat;
180 originalCat.getBrain()->setIdea(0, "Fish!");
181 Cat assignedCat;
182 assignedCat = originalCat;
183
184 std::cout << "Original Cat's first idea: " << originalCat.getBrain()->getIdea(0) << std::endl;
185 std::cout << "Assigned Cat's first idea: " << assignedCat.getBrain()->getIdea(0) << std::endl;
186
187 assignedCat.getBrain()->setIdea(0, "Nap!");
188 std::cout << "Original Cat's first idea after assigned Cat changed: " << originalCat.getBrain()->getIdea(0)
189 << std::endl;
190 std::cout << "Assigned Cat's first idea after assigned Cat changed: " << assignedCat.getBrain()->getIdea(0)
191 << std::endl;
192}
193
200static void stackTests(void) {
201 std::cout << "\n--- More Tests (Stack Objects) ---" << std::endl;
202 {
203 Dog stackDog;
204 stackDog.getBrain()->setIdea(50, "Walkies?");
205 std::cout << "Stack Dog's 50th idea: " << stackDog.getBrain()->getIdea(50) << std::endl;
206 } // stackDog and its Brain are destroyed here
207 {
208 Cat stackCat;
209 stackCat.getBrain()->setIdea(99, "Laser pointer!");
210 std::cout << "Stack Cat's 99th idea: " << stackCat.getBrain()->getIdea(99) << std::endl;
211 } // stackCat and its Brain are destroyed here
212}
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 * get() const
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)