File tree 9 files changed +130
-2
lines changed
9 files changed +130
-2
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream> // 包含输入输出流库
2
+ using namespace std ; // 使用标准命名空间
3
+
4
+ // 定义一个名为 Numbers 的类
5
+ class Numbers {
6
+ private:
7
+ int * data; // 指向整数数组的指针
8
+ int size; // 数组的大小
9
+ public:
10
+ // 构造函数,接受一个整数参数 size
11
+ Numbers (int size) : size(size) {
12
+ data = new int [size]; // 动态分配内存
13
+ for (int i = 0 ; i < size; i++) {
14
+ data[i] = i + 1 ; // 初始化数组,值为 1 到 size
15
+ }
16
+ }
17
+ // 析构函数,释放动态分配的内存
18
+ ~Numbers () {
19
+ delete[] data; // 释放内存
20
+ }
21
+ // 打印数组中的所有元素
22
+ void print () {
23
+ for (int i = 0 ; i < size; i++) {
24
+ cout << data[i] << endl; // 输出每个元素的值
25
+ }
26
+ }
27
+ };
28
+
29
+ int main () {
30
+ Numbers numbers (5 ); // 创建一个 Numbers 对象,数组大小为 5
31
+ numbers.print (); // 调用 print 方法,打印数组中的所有元素
32
+ return 0 ; // 返回 0,表示程序成功结束
33
+ }
Original file line number Diff line number Diff line change 3
3
4
4
def compile_and_measure (program_name , language ):
5
5
if language == "c" :
6
- compile_command = f"gcc -O2 { program_name } .c -o { program_name } "
6
+ compile_command = f"gcc -O3 { program_name } .c -o { program_name } "
7
7
elif language == "cpp" :
8
- compile_command = f"g++ -O2 { program_name } .cpp -o { program_name } "
8
+ compile_command = f"g++ -O3 { program_name } .cpp -o { program_name } "
9
9
10
10
# Measure compile time
11
11
compile_start = time .time ()
Original file line number Diff line number Diff line change
1
+ #ifndef ANIMAL_H
2
+ #define ANIMAL_H
3
+
4
+ #include < iostream>
5
+ #include < string>
6
+
7
+ class Animal {
8
+ protected:
9
+ std::string name;
10
+ int age;
11
+
12
+ public:
13
+ Animal (std::string n, int a) : name(n), age(a) {}
14
+
15
+ virtual void makeSound () const = 0; // 纯虚函数,使Animal成为抽象基类
16
+
17
+ virtual void showInfo () const {
18
+ std::cout << " Name: " << name << " , Age: " << age << std::endl;
19
+ }
20
+
21
+ virtual ~Animal () {} // 虚析构函数,确保派生类的正确析构
22
+ };
23
+
24
+ #endif
Original file line number Diff line number Diff line change
1
+ #ifndef CAT_H
2
+ #define CAT_H
3
+
4
+ #include " Animal.h"
5
+
6
+ class Cat : public Animal {
7
+ public:
8
+ Cat (std::string n, int a) : Animal(n, a) {}
9
+
10
+ void makeSound () const override {
11
+ std::cout << name << " says Meow!" << std::endl;
12
+ }
13
+ };
14
+
15
+ #endif
Original file line number Diff line number Diff line change
1
+ #ifndef DOG_H
2
+ #define DOG_H
3
+
4
+ #include " Animal.h"
5
+
6
+ class Dog : public Animal {
7
+ public:
8
+ Dog (std::string n, int a) : Animal(n, a) {}
9
+
10
+ void makeSound () const override {
11
+ std::cout << name << " says Woof!" << std::endl;
12
+ }
13
+ };
14
+
15
+ #endif
Original file line number Diff line number Diff line change
1
+ #ifndef MONKEY_H
2
+ #define MONKEY_H
3
+
4
+ #include " Animal.h"
5
+
6
+ class Monkey : public Animal {
7
+ public:
8
+ Monkey (std::string n, int a) : Animal(n, a) {}
9
+
10
+ void makeSound () const override {
11
+ std::cout << name << " says Ooh ooh aah aah!" << std::endl;
12
+ }
13
+ };
14
+
15
+ #endif
Original file line number Diff line number Diff line change
1
+ #include " Cat.h"
2
+ #include " Dog.h"
3
+ #include " Monkey.h"
4
+ #include < vector>
5
+
6
+ int main () {
7
+ std::vector<Animal*> zoo;
8
+
9
+ zoo.push_back (new Cat (" Whiskers" , 3 ));
10
+ zoo.push_back (new Dog (" Fido" , 5 ));
11
+ zoo.push_back (new Monkey (" George" , 7 ));
12
+ zoo.push_back (new Monkey (" Mike" , 3 ));
13
+
14
+ for (auto animal : zoo) {
15
+ animal->showInfo ();
16
+ animal->makeSound ();
17
+ }
18
+
19
+ // 清理
20
+ for (auto animal : zoo) {
21
+ delete animal;
22
+ }
23
+ zoo.clear ();
24
+
25
+ return 0 ;
26
+ }
You can’t perform that action at this time.
0 commit comments