├── lesson01 ├── CPP1.askisi1.cpp ├── CPP1.askisi2.cpp ├── CPP1.askisi3.cpp ├── CPP1.bool.cpp ├── CPP1.cin.cpp ├── CPP1.endl.cpp ├── CPP1.helloworld.cpp ├── CPP1.helloworld2.cpp ├── CPP1.helloworld3.cpp ├── CPP1.overloading.cpp ├── CPP1.strings.cpp ├── askisi1(arxiko).c ├── cpp1(4sl).pdf └── cpp1.pdf ├── lesson02 ├── CPP2.accessors.cpp ├── CPP2.askisi1.cpp ├── CPP2.askisi2.cpp ├── CPP2.askisi3.cpp ├── CPP2.class.cpp ├── CPP2.class_functions.cpp ├── CPP2.class_methods.cpp ├── CPP2.constructor.cpp ├── CPP2.destructor.cpp ├── CPP2.private.cpp ├── CPP2.private_public.cpp ├── CPP2.public.cpp ├── cpp2(4sl).pdf └── cpp2.pdf ├── lesson03 ├── CPP3.1d_dynamic_array.cpp ├── CPP3.2d_dynamic_array.cpp ├── CPP3.askisi1.cpp ├── CPP3.askisi2.cpp ├── CPP3.assignment_problem.cpp ├── CPP3.class_with_pointer.cpp ├── CPP3.constructor_destructor.cpp ├── CPP3.dereferencing.cpp ├── CPP3.dynamic_object.cpp ├── CPP3.dynamic_variable.cpp ├── CPP3.local_variables.cpp ├── CPP3.new_int.cpp ├── CPP3.pointers.cpp ├── CPP3.pointers_and_objects.cpp ├── CPP3.sizeof.cpp ├── cpp3(4sl).pdf └── cpp3.pdf ├── lesson04 ├── cpp4(4sl).pdf ├── cpp4.askisi1.cpp ├── cpp4.askisi2.cpp ├── cpp4.askisi3.cpp ├── cpp4.bypointer.cpp ├── cpp4.byreference.cpp ├── cpp4.byvalue.cpp ├── cpp4.copy_arg_by_value.cpp ├── cpp4.copy_constructor_arg_by_value.cpp ├── cpp4.copy_constructor_object_declaration.cpp ├── cpp4.copy_constructor_return_ob.cpp ├── cpp4.pdf ├── cpp4.reference.cpp └── cpp4.reference_mistake.cpp ├── lesson05 ├── CPP5.array_of_objects.cpp ├── CPP5.array_of_objects_noinit.cpp ├── CPP5.array_of_pointers_to_objects.cpp ├── CPP5.class_with_array.cpp ├── CPP5.class_with_objects.cpp ├── CPP5.construction_with_initialization_syntax.cpp ├── CPP5.default_parameters.cpp ├── CPP5.dynamic_array_of_objects.cpp ├── CPP5.inline_function.cpp ├── CPP5.name_collisions.cpp ├── CPP5.this.cpp ├── CPP5.this_constructor.cpp ├── cpp5(4sl).pdf ├── cpp5.askisi1.cpp ├── cpp5.askisi2.cpp ├── cpp5.askisi3.cpp ├── cpp5.const_object.cpp ├── cpp5.pdf ├── cpp5.pointer_to_const_object.cpp └── cpp5.reference_to_const_object.cpp ├── lesson06 ├── cpp6(4sl).pdf ├── cpp6.array_with_index_overloading.cpp ├── cpp6.askisi1.1.cpp ├── cpp6.askisi1.2.cpp ├── cpp6.askisi1.3.cpp ├── cpp6.askisi2.cpp ├── cpp6.askisi3.cpp ├── cpp6.operator_equal.cpp ├── cpp6.operator_minus.cpp ├── cpp6.operator_plus.cpp ├── cpp6.operator_plus_int.cpp ├── cpp6.operator_plusplus_post.cpp ├── cpp6.operator_plusplus_pre.cpp └── cpp6.pdf ├── lesson07 ├── Cpp7(4sl).pdf ├── Cpp7.pdf ├── STRING.cpp ├── STRING.h ├── cpp7.askisi1.cpp ├── cpp7.askisi2.1.cpp ├── cpp7.askisi2.2.cpp ├── cpp7.askisi3.cpp ├── cpp7.askisi4.1.cpp ├── cpp7.askisi4.2.cpp ├── cpp7.askisi4.3.cpp ├── cpp7.friend_class_ex1.cpp ├── cpp7.friend_class_linked_list.cpp ├── cpp7.friend_function.cpp ├── cpp7.friend_method.cpp ├── cpp7.friend_operator_plus.cpp └── cpp7.friend_operator_print.cpp ├── lesson08 ├── cpp8(4sl).pdf ├── cpp8.askisi1.cpp ├── cpp8.askisi2.cpp ├── cpp8.lifespan.cpp ├── cpp8.pdf ├── cpp8.static_local_variables.cpp ├── cpp8.static_members.cpp └── cpp8.static_methods.cpp ├── lesson09 ├── cpp9(4sl).pdf ├── cpp9.askisi1.cpp ├── cpp9.askisi2.cpp ├── cpp9.pdf ├── cpp9.string.cpp ├── cpp9.string_add_rem_upd.cpp ├── cpp9.string_capacity.cpp ├── cpp9.string_find.cpp └── cpp9.string_operators.cpp ├── lesson10 ├── cpp10(4sl).pdf ├── cpp10.askisi1.1-1.2.cpp ├── cpp10.askisi1.3-1.4.cpp ├── cpp10.askisi2.1-2.2.cpp ├── cpp10.askisi2.3-2.4.cpp ├── cpp10.inheritance_constructors.cpp ├── cpp10.inheritance_destructors.cpp ├── cpp10.pdf ├── cpp10.protected_members.cpp └── cpp10.public_inheritance.cpp ├── lesson11 ├── cpp11(4sl).pdf ├── cpp11.abstract_class.cpp ├── cpp11.askisi1.1.cpp ├── cpp11.askisi1.2.cpp ├── cpp11.askisi2.1.cpp ├── cpp11.destructor_base_pointer.cpp ├── cpp11.destructor_derived_pointer.cpp ├── cpp11.method_overriding.cpp ├── cpp11.pdf ├── cpp11.pointer_to_base_class1.cpp ├── cpp11.pointer_to_base_class2.cpp ├── cpp11.virtual_destructor_derived_pointer.cpp ├── cpp11.virtual_method.cpp ├── cpp11.virtual_method_object.cpp └── cpp11.virtual_method_reference.cpp ├── lesson12 ├── cpp12(4sl).pdf ├── cpp12.askisi1.1.cpp ├── cpp12.askisi1.2.cpp ├── cpp12.askisi2.1.cpp ├── cpp12.askisi2.2.cpp ├── cpp12.diamond_problem.cpp ├── cpp12.mi_constructors.cpp ├── cpp12.multiple_inheritance.cpp ├── cpp12.pdf ├── cpp12.use_of_resolution_scope_operator.cpp └── cpp12.virtual_class.cpp └── lesson13 ├── cpp13(4sl).pdf ├── cpp13.askisi1.cpp ├── cpp13.askisi2.cpp ├── cpp13.pdf ├── cpp13.swap.cpp ├── cpp13.template.cpp ├── cpp13.template_non_type_param.cpp ├── cpp13.template_with_friend.cpp └── cpp13.template_with_static.cpp /lesson01/CPP1.askisi1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char name[80]; 7 | char surname[80]; 8 | int age; 9 | 10 | cout<<"Dwse to onoma: "; 11 | cin>>name; 12 | cout<<"Dwse to epwnimo: "; 13 | cin>>surname; 14 | cout<<"Dwse ilikia: "; 15 | cin>>age; 16 | 17 | cout< 2 | using namespace std; 3 | 4 | double average (int a, int b); 5 | double average (double a, double b); 6 | 7 | int main() 8 | { 9 | int int_a, int_b; 10 | double double_a, double_b; 11 | int in; 12 | 13 | cout<<"1-akeraioi, 2-pragmatikoi: "; 14 | cin>>in; 15 | 16 | if (in==1) 17 | { 18 | cout<<"Dwse 1o akeraio: "; 19 | cin>>int_a; 20 | cout<<"Dwse 2o akeraio: "; 21 | cin>>int_b; 22 | cout<<"Mesos oros: "<>double_a; 28 | cout<<"Dwse 2o pragmatiko: "; 29 | cin>>double_b; 30 | cout<<"Mesos oros: "< 4 | using namespace std; 5 | 6 | #define N 5 7 | 8 | int main() 9 | { 10 | int i; 11 | int A[N]= {5, 3, 2, 4, 8 }; // Pinakas dedomenwn 12 | int x=7; // Pros anazitisi stoixeio 13 | bool found; 14 | 15 | /* Psaxnoume gia to stoixeio x ston pinaka A */ 16 | found = false; 17 | for (i=0; i 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int x; 9 | 10 | cout<<"Dwse x: "; 11 | cin>>x; 12 | cout<<"To x einai: "< 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int x = 5; 9 | double y = 5.01; 10 | 11 | cout<<"To x einai "< 4 | 5 | int main() 6 | { 7 | std::cout<<"Hello World!"; 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /lesson01/CPP1.helloworld2.cpp: -------------------------------------------------------------------------------- 1 | /* CPP1.helloworld2.cpp using std::cout */ 2 | #include 3 | using std::cout; 4 | 5 | int main() 6 | { 7 | cout<<"Hello World!"; 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /lesson01/CPP1.helloworld3.cpp: -------------------------------------------------------------------------------- 1 | /* CPP1.helloworld3.cpp Ενσωμάτωση όλης της std*/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | cout<<"Hello World!"; 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /lesson01/CPP1.overloading.cpp: -------------------------------------------------------------------------------- 1 | /* CPP1.overloading.cpp Υπερφόρτωση Συναρτήσεων */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int sum(int x, int y); 7 | double sum(double x, double y); 8 | 9 | int main() 10 | { 11 | cout< 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | char str1[80]; 10 | char str2[80]; 11 | 12 | cout<<"Dwse string: "; 13 | cin>>str1; 14 | strcpy(str2,str1); 15 | cout< 4 | 5 | #define N 5 6 | 7 | main() 8 | { 9 | int i; 10 | int A[N]= {5, 3, 2, 4, 8 }; // Pinakas dedomenwn 11 | int x=2; // Pros anazitisi stoixeio 12 | 13 | /* Psaxnoume gia to stoixeio x ston pinaka A */ 14 | for (i=0; i 4 | using namespace std; 5 | 6 | class dog 7 | { 8 | public: 9 | dog(int in_mood); 10 | ~dog(); 11 | void set_mood(int in_mood); 12 | int get_mood() const; 13 | void bark(); 14 | void report_mood(); 15 | private: 16 | int mood; 17 | }; 18 | 19 | 20 | int main() 21 | { 22 | dog piko(6); 23 | 24 | cout<<"Piko, how are you today?"<10) 57 | cout<<"I am cool"; 58 | else 59 | cout<<"I am furious"; 60 | } 61 | -------------------------------------------------------------------------------- /lesson02/CPP2.askisi1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class dummy { 5 | public: 6 | dummy(); 7 | dummy(int in_x); 8 | ~dummy(); 9 | void set_x(int in_x); 10 | int get_x() const; 11 | private: 12 | int x; 13 | }; 14 | 15 | 16 | int main() 17 | { 18 | dummy ob1(10); 19 | dummy ob2; 20 | 21 | return 0; 22 | } 23 | 24 | dummy::dummy() 25 | { 26 | x = 0; 27 | } 28 | 29 | dummy::dummy(int in_x) 30 | { 31 | x = in_x; 32 | } 33 | 34 | dummy::~dummy() 35 | { 36 | cout<<"Destructing with x= "< 2 | using namespace std; 3 | 4 | class point { 5 | public: 6 | point(); 7 | point(double in_x, double in_y); 8 | void set_x(double in_x); 9 | void set_y(double in_y); 10 | double get_x() const; 11 | double get_y() const; 12 | void print(); 13 | private: 14 | double x; 15 | double y; 16 | }; 17 | 18 | 19 | int main() 20 | { 21 | point ob1(1.1, 2.2); 22 | point ob2(1.0, 2.0); 23 | point ob3; 24 | 25 | cout<<"1o simeio: "; 26 | ob1.print(); 27 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class wizard { 6 | public: 7 | wizard(int in_age, char *in_beard, int in_mana); 8 | void fireball(); 9 | void lightning(); 10 | void wait(); 11 | private: 12 | int age; 13 | char beard[80]; 14 | int mana; 15 | }; 16 | 17 | 18 | 19 | 20 | int main() 21 | { 22 | char str[80]="grey"; 23 | wizard gandalf(2019, str, 100); 24 | 25 | gandalf.fireball(); 26 | gandalf.wait(); 27 | gandalf.lightning(); 28 | gandalf.wait(); 29 | gandalf.wait(); 30 | gandalf.fireball(); 31 | 32 | } 33 | 34 | 35 | 36 | 37 | wizard::wizard(int in_age, char *in_beard, int in_mana) 38 | { 39 | age = in_age; 40 | strcpy(beard, in_beard); 41 | mana = in_mana; 42 | } 43 | 44 | void wizard::fireball() 45 | { 46 | if (mana >= 30) 47 | { 48 | mana-=30; 49 | cout<=90) 58 | { 59 | mana-=90; 60 | cout< 4 | using namespace std; 5 | 6 | class dog 7 | { 8 | public: 9 | void init(int in_mood) 10 | { 11 | mood=in_mood; 12 | } 13 | void bark() 14 | { 15 | cout<<"woof!"<10) 20 | cout<<"I am cool"; 21 | else 22 | cout<<"I am furious"; 23 | } 24 | private: 25 | int mood; 26 | }; 27 | 28 | 29 | int main() 30 | { 31 | dog piko; 32 | 33 | piko.init(6); 34 | 35 | cout<<"Piko, how are you today?"< 4 | using namespace std; 5 | 6 | class dog 7 | { 8 | public: 9 | void init(int in_mood); 10 | void bark(); 11 | void report_mood(); 12 | private: 13 | int mood; 14 | }; 15 | 16 | 17 | int main() 18 | { 19 | dog piko; 20 | 21 | piko.init(6); 22 | 23 | cout<<"Piko, how are you today?"<10) 42 | cout<<"I am cool"; 43 | else 44 | cout<<"I am furious!"; 45 | } 46 | -------------------------------------------------------------------------------- /lesson02/CPP2.class_methods.cpp: -------------------------------------------------------------------------------- 1 | /* CPP2.class_methods.cpp Ορισμός των μεθόδων έξω από τις κλάσεις */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | class dog 7 | { 8 | public: 9 | void init(int in_mood); 10 | void bark(); 11 | void report_mood(); 12 | private: 13 | int mood; 14 | }; 15 | 16 | 17 | int main() 18 | { 19 | dog piko; 20 | 21 | piko.init(6); 22 | 23 | cout<<"Piko, how are you today?"<10) 42 | cout<<"I am cool"; 43 | else 44 | cout<<"I am furious!"; 45 | } 46 | -------------------------------------------------------------------------------- /lesson02/CPP2.constructor.cpp: -------------------------------------------------------------------------------- 1 | /* CPP2.constructor.cpp */ 2 | #include 3 | using namespace std; 4 | 5 | class dog 6 | { 7 | public: 8 | dog(int in_mood); 9 | void bark(); 10 | void report_mood(); 11 | private: 12 | int mood; 13 | }; 14 | 15 | int main() 16 | { 17 | dog piko(6); 18 | 19 | cout<<"Piko, how are you today?"<10) 40 | cout<<"I am cool"; 41 | else 42 | cout<<"I am furious"; 43 | } 44 | -------------------------------------------------------------------------------- /lesson02/CPP2.destructor.cpp: -------------------------------------------------------------------------------- 1 | /* CPP2.destructor.cpp */ 2 | #include 3 | using namespace std; 4 | 5 | class dog 6 | { 7 | public: 8 | dog(int in_mood); 9 | ~dog(); 10 | void bark(); 11 | void report_mood(); 12 | private: 13 | int mood; 14 | }; 15 | 16 | 17 | int main() 18 | { 19 | dog piko(6); 20 | 21 | cout<<"Piko, how are you today?"<10) 46 | cout<<"I am cool"; 47 | else 48 | cout<<"I am furious"; 49 | } 50 | -------------------------------------------------------------------------------- /lesson02/CPP2.private.cpp: -------------------------------------------------------------------------------- 1 | /* CPP2.private.cpp Ιδιωτικά Μέλη της κλάσης */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | class cow 7 | { 8 | public: 9 | int weight; 10 | void express () 11 | { 12 | if (hunger>10) 13 | cout<<"MmmmmmmmMMMmmM"; 14 | } 15 | int set_hunger (int h) 16 | { 17 | hunger = h; 18 | } 19 | private: 20 | int hunger; 21 | }; 22 | 23 | 24 | int main() 25 | { 26 | cow Molly; 27 | Molly.weight = 500; 28 | Molly.set_hunger(49); 29 | Molly.express(); 30 | } 31 | -------------------------------------------------------------------------------- /lesson02/CPP2.private_public.cpp: -------------------------------------------------------------------------------- 1 | /* CPP2.class_functions.cpp Ορισμός των μεθόδων έξω από τις κλάσεις */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | class cow 7 | { 8 | public: 9 | int weight; 10 | private: 11 | int hunger; 12 | }; 13 | 14 | 15 | int main() 16 | { 17 | dog piko; 18 | 19 | piko.init(6); 20 | 21 | cout<<"Piko, how are you today?"<10) 40 | cout<<"I am cool"; 41 | else 42 | cout<<"I have my nerves"; 43 | } 44 | -------------------------------------------------------------------------------- /lesson02/CPP2.public.cpp: -------------------------------------------------------------------------------- 1 | /* CPP2.public.cpp Δημόσια Μέλη της κλάσης */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | class cow 7 | { 8 | public: 9 | int weight; 10 | private: 11 | int hunger; 12 | }; 13 | 14 | 15 | int main() 16 | { 17 | cow molly; 18 | molly.weight = 500; 19 | // Molly.hunger = 10; // Δεν επιτρέπεται. Είναι ιδιωτική μεταβλητή 20 | } 21 | -------------------------------------------------------------------------------- /lesson02/cpp2(4sl).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/CPP-programming/3350f77306648649679dd092aad224d875f3e1d3/lesson02/cpp2(4sl).pdf -------------------------------------------------------------------------------- /lesson02/cpp2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/CPP-programming/3350f77306648649679dd092aad224d875f3e1d3/lesson02/cpp2.pdf -------------------------------------------------------------------------------- /lesson03/CPP3.1d_dynamic_array.cpp: -------------------------------------------------------------------------------- 1 | /* CPP3.1d_dynamic_array.cpp Μονοδιάστατος πίνακας με δυναμική δέσμευση μνήμης */ 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int *arr; 8 | int n=4; 9 | 10 | /* Δέσμευση Μνήμης */ 11 | arr = new int [n]; 12 | if (!arr) cout<<"Error allocating memory!"; 13 | 14 | /* Κάποια δουλειά στον πίνακα */ 15 | for (int i=0; i 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int **arr; 8 | int i,j, n=4, m=4; 9 | 10 | /* Δέσμευση Μνήμης */ 11 | arr = new int * [n]; 12 | if (!arr) cout<<"Error allocating memory!"; 13 | for (i=0; i 4 | using namespace std; 5 | 6 | 7 | int main() 8 | { 9 | int x = 5; 10 | int *p; 11 | int **pp; 12 | 13 | p=&x; 14 | pp=&p; 15 | 16 | cout<<"x="< 4 | using namespace std; 5 | 6 | class ARRAY { 7 | public: 8 | ARRAY(int in_n); 9 | ~ARRAY(); 10 | void set_i(int i, int val); 11 | int get_i(int i); 12 | void print(); 13 | private: 14 | int *p; 15 | int n; 16 | }; 17 | 18 | 19 | int main() 20 | { 21 | int n=10; 22 | ARRAY pin(n); 23 | ARRAY pin2(10); 24 | 25 | for (int i=0; i=0 && i=0 && i 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy(); 8 | ~dummy(); 9 | void set_val(int in_val); 10 | int get_val(); 11 | private: 12 | int *p_val; 13 | }; 14 | 15 | int main() 16 | { 17 | dummy ob1; 18 | ob1.set_val(3); 19 | dummy ob2; 20 | ob2 = ob1; 21 | 22 | cout< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy(); 8 | ~dummy(); 9 | void set_val(int in_val); 10 | int get_val(); 11 | private: 12 | int *p_val; 13 | }; 14 | 15 | int main() 16 | { 17 | dummy ob; 18 | 19 | ob.set_val(3); 20 | 21 | cout< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy(int in_x); 8 | ~dummy(); 9 | private: 10 | int x; 11 | }; 12 | 13 | int main() 14 | { 15 | dummy *p = NULL; 16 | 17 | p = new dummy(5); //constructing 18 | if (!p) cout<<"Error allocating memory"; 19 | 20 | cout< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | int x; 8 | }; 9 | 10 | int main() 11 | { 12 | dummy *p = NULL; //Θα δείξει σε αντικείμενο 13 | p = new dummy; // Δέσμευση χώρου 14 | 15 | /* Αναθέσεις τιμών */ 16 | (*p).x = 5; 17 | 18 | /* Εκτυπώσεις */ 19 | cout<<"(*p).x = "<<(*p).x< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | int x; 8 | }; 9 | 10 | int main() 11 | { 12 | dummy *p = NULL; //Θα δείξει σε αντικείμενο 13 | p = new dummy; // Δέσμευση χώρου 14 | if (!p) cout<<"Error allocating memory"; 15 | 16 | /* Αναθέσεις τιμών */ 17 | p->x = 5; 18 | 19 | /* Εκτυπώσεις */ 20 | cout<<"p->x = "<x< // CPP3.dynamic_variable.cpp 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int x; // Μεταβλητή που δεσμέυει στατικό χώρο 7 | int *p; // Δείκτης σε ακέραιο 8 | 9 | p = new int; // Δυναμική δέσμευση μνήμης 10 | if (!p) cout<<“Error allocating memory”; 11 | 12 | /* Αναθέσεις τιμών */ 13 | x = 1; 14 | *p = 5; 15 | /* Εκτυπώσεις */ 16 | cout<<"x = "< 4 | using namespace std; 5 | 6 | void f() 7 | { 8 | int y; 9 | cout<<"inside f"; 10 | } 11 | 12 | int main() 13 | { 14 | int x; 15 | 16 | cout<<"inside main"; 17 | f(); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /lesson03/CPP3.new_int.cpp: -------------------------------------------------------------------------------- 1 | /* CPP3.new_int.cpp Δεσμευση μνήμης για ακέραιο */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int x; // Μεταβλητή που δεσμέυει στατικό χώρο 9 | int *p; // Δείκτης σε ακέραιο 10 | 11 | p = new int; // Δυναμική δέσμευση μνήμης 12 | 13 | /* Αναθέσεις τιμών */ 14 | x = 5; 15 | *p = 1; 16 | 17 | /* Εκτυπώσεις */ 18 | cout<<"x = "< 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int x=5; 9 | int *p = NULL; 10 | 11 | p = &x; 12 | 13 | cout<<"x = "< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | int x; 8 | }; 9 | 10 | int main() 11 | { 12 | dummy ob; 13 | dummy *p = &ob; 14 | if (!p) cout<<"Error allocating memory"; 15 | 16 | /* Αναθέσεις τιμών */ 17 | ob.x = 6; 18 | 19 | /* Εκτυπώσεις */ 20 | cout<x<<" "<<(*p).x< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | int x; 8 | int y; 9 | }; 10 | 11 | int main() 12 | { 13 | dummy ob; 14 | 15 | cout< 4 | using namespace std; 5 | 6 | class point { 7 | public: 8 | point(); 9 | point(double in_x, double in_y); 10 | void set_x(double in_x); 11 | void set_y(double in_y); 12 | double get_x() const; 13 | double get_y() const; 14 | void print(); 15 | private: 16 | double x; 17 | double y; 18 | }; 19 | 20 | void swap(int &x, int &y); 21 | void swap(double &x, double &y); 22 | void swap(point &x, point &y); 23 | 24 | int main() 25 | { 26 | point a(1.1,2.2), b(1.5,2.0); 27 | 28 | swap(a,b); 29 | 30 | a.print(); 31 | b.print(); 32 | 33 | return 0; 34 | } 35 | 36 | 37 | void swap(int &x, int &y) 38 | { 39 | int temp; 40 | 41 | temp=x; 42 | x=y; 43 | y=temp; 44 | } 45 | 46 | void swap(point &x, point &y) 47 | { 48 | point temp; 49 | 50 | temp=x; 51 | x=y; 52 | y=temp; 53 | } 54 | 55 | void swap(double &x, double &y) 56 | { 57 | double temp; 58 | 59 | temp=x; 60 | x=y; 61 | y=temp; 62 | } 63 | 64 | 65 | point::point() 66 | { 67 | x = 0.0; 68 | y = 0.0; 69 | } 70 | 71 | point::point(double in_x, double in_y) 72 | { 73 | x = in_x; 74 | y = in_y; 75 | } 76 | 77 | void point::set_x(double in_x) 78 | { 79 | x = in_x; 80 | } 81 | 82 | void point::set_y(double in_y) 83 | { 84 | y = in_y; 85 | } 86 | 87 | double point::get_x() const 88 | { 89 | return x; 90 | } 91 | 92 | double point::get_y() const 93 | { 94 | return y; 95 | } 96 | 97 | void point::print() 98 | { 99 | cout<<"("< 4 | using namespace std; 5 | 6 | class ARRAY { 7 | public: 8 | ARRAY(int in_n); 9 | ARRAY(const ARRAY &ob); 10 | ~ARRAY(); 11 | void set_i(int i, int val); 12 | int get_i(int i); 13 | int get_n() const; 14 | void print(); 15 | private: 16 | int *p; 17 | int n; 18 | }; 19 | 20 | 21 | int main() 22 | { 23 | int n=10; 24 | ARRAY pin(n); 25 | ARRAY pin2 = pin; 26 | 27 | for (int i=0; i=0 && i=0 && i 4 | #include 5 | using namespace std; 6 | 7 | class STRING { 8 | public: 9 | STRING(); 10 | STRING(char *in_str); 11 | STRING(const STRING &ob); 12 | ~STRING(); 13 | int get_length() const; 14 | char *get_str() const; 15 | void set_str(char *in_str); 16 | void print(); 17 | private: 18 | int length; 19 | char *str; 20 | }; 21 | 22 | 23 | int main() 24 | { 25 | char s[80]="This is a string"; 26 | STRING s1(s); 27 | STRING s2 = s1; 28 | 29 | strcpy(s, "This is a new string"); 30 | s2.set_str(s); 31 | 32 | s1.print(); 33 | cout< 3 | using namespace std; 4 | 5 | void change(int *pA); 6 | 7 | int main() 8 | { 9 | int a=1; 10 | 11 | cout<<"main: a="< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy(); 8 | dummy(const dummy &ob); 9 | ~dummy(); 10 | int x; 11 | }; 12 | 13 | void f(dummy arg); 14 | 15 | int main() 16 | { 17 | dummy d; 18 | 19 | cout<<"Main: Before calling f"< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy(); 8 | dummy(const dummy &ob); 9 | ~dummy(); 10 | int x; 11 | }; 12 | 13 | void f(dummy ob); 14 | 15 | int main() 16 | { 17 | dummy ob1; 18 | dummy ob2 = ob1; // dummy ob2(ob1); 19 | 20 | return 0; 21 | } 22 | 23 | dummy::dummy() 24 | { 25 | cout<<"Constructing..."< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy(); 8 | dummy(const dummy &ob); 9 | ~dummy(); 10 | int x; 11 | }; 12 | 13 | dummy f(); 14 | 15 | int main() 16 | { 17 | cout<<"Main: Before calling f"< 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int x = 3; 8 | int &r = x; 9 | 10 | cout<<"x="< 4 | using namespace std; 5 | 6 | #define N 3 7 | 8 | class dummy { 9 | public: 10 | dummy(); 11 | void set_x(int in_x); 12 | int get_x() const; 13 | private: 14 | int x; 15 | }; 16 | 17 | int main() 18 | { 19 | dummy array[N]; 20 | 21 | for (int i=0; i 4 | using namespace std; 5 | 6 | #define N 3 7 | 8 | class dummy { 9 | public: 10 | dummy(); 11 | void set_x(int in_x); 12 | int get_x() const; 13 | private: 14 | int x; 15 | }; 16 | 17 | int main() 18 | { 19 | dummy *array[N]; 20 | 21 | for (int i=0; iset_x(i*i); 26 | 27 | for (int i=0; iget_x()<<" "; 29 | 30 | for (int i=0; i 3 | using namespace std; 4 | 5 | #define N 3 6 | 7 | class grades 8 | { 9 | public: 10 | grades(); 11 | int set_i(int pos, int val); 12 | int get_i(int pos) const; 13 | private: 14 | int array[N]; 15 | }; 16 | 17 | int main() 18 | { 19 | grades my_grades; 20 | 21 | /* Εισαγωγή Δεδομένων */ 22 | my_grades.set_i(0,5); 23 | my_grades.set_i(1,8); 24 | my_grades.set_i(2,7); 25 | 26 | /* Εκτύπωση δεδομένων */ 27 | for (int i=0; i=0 && pos=0 && pos 3 | using namespace std; 4 | 5 | class flat { 6 | public: 7 | flat(); 8 | ~flat(); 9 | void set_people(int in_people); 10 | int get_people() const; 11 | private: 12 | int people; 13 | }; 14 | 15 | class storey { 16 | public: 17 | storey(); 18 | ~storey(); 19 | void set_people(int in_flat, int in_people); 20 | int get_people() const; 21 | private: 22 | flat flats[2]; 23 | }; 24 | 25 | 26 | class building { 27 | public: 28 | building(); 29 | ~building(); 30 | void set_people(int in_floor, int in_flat, int in_people); 31 | int get_people() const; 32 | private: 33 | storey storeys[2]; 34 | }; 35 | 36 | int main() 37 | { 38 | building b; 39 | 40 | b.set_people(0,0,3); 41 | b.set_people(0,1,2); 42 | b.set_people(1,0,2); 43 | b.set_people(1,1,1); 44 | 45 | cout< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy(int in_x, int in_y); 8 | void print(); 9 | private: 10 | int x,y; 11 | }; 12 | 13 | int main() 14 | { 15 | dummy ob (3,4); 16 | 17 | ob.print(); 18 | 19 | return 0; 20 | } 21 | 22 | dummy::dummy(int in_x, int in_y): 23 | x(in_x), y(in_y) 24 | { 25 | cout<<"Constructing..."< 3 | using namespace std; 4 | 5 | int sum(int x1, int x2=0, int x3=0, int x4=0); 6 | 7 | int main() 8 | { 9 | cout< 4 | using namespace std; 5 | 6 | class dummy { 7 | public: 8 | dummy(); 9 | void set_x(int in_x); 10 | int get_x() const; 11 | private: 12 | int x; 13 | }; 14 | 15 | int main() 16 | { 17 | dummy *array; 18 | int n=3; 19 | 20 | array = new dummy [n]; 21 | if (!array) cout<<"Mem error"; 22 | 23 | for (int i=0; i 3 | using namespace std; 4 | 5 | inline int sqr(int x); 6 | 7 | int main() 8 | { 9 | cout< 4 | using namespace std; 5 | 6 | class dummy { 7 | public: 8 | dummy() { x = 2; }; 9 | void print(int x); 10 | private: 11 | int x; // class member = 2 12 | }; 13 | 14 | int x=1; // global = 1 15 | 16 | int main() 17 | { 18 | dummy ob; 19 | int x=3; 20 | 21 | ob.print(x); 22 | 23 | return 0; 24 | } 25 | 26 | void dummy::print(int x) // argument = 3 27 | { 28 | int x = 4; // local = 4 29 | cout< 4 | using namespace std; 5 | 6 | class point { 7 | public: 8 | point(int in_x, int in_y); 9 | point *ret_this(); 10 | void print(); 11 | private: 12 | int x; 13 | int y; 14 | }; 15 | 16 | int main() 17 | { 18 | point ob(3,4); 19 | 20 | cout<<"Address: "<<&ob< 4 | using namespace std; 5 | 6 | class point { 7 | public: 8 | point(int x, int y); 9 | void print(); 10 | private: 11 | int x; 12 | int y; 13 | }; 14 | 15 | int main() 16 | { 17 | point ob(3,4); 18 | 19 | ob.print(); 20 | 21 | return 0; 22 | } 23 | 24 | point::point(int x, int y) 25 | { 26 | this->x=x; 27 | this->y=y; 28 | } 29 | 30 | void point::print() 31 | { 32 | cout<<"("< 4 | using namespace std; 5 | 6 | #define N 3 7 | 8 | class tictactoe { 9 | public: 10 | tictactoe(); 11 | bool play(int x, int y, char c); 12 | char check_winner(); 13 | void print(); 14 | private: 15 | char array[N][N]; 16 | }; 17 | 18 | 19 | int main() 20 | { 21 | tictactoe t; 22 | char player = 'X'; 23 | int x,y, step=0; 24 | 25 | while(true) 26 | { 27 | cout<<"PLAISIO: "<>x; 33 | cout<<"Dwse Y sintetagmeni: "; 34 | cin>>y; 35 | 36 | if (!t.play(x,y,player)) 37 | { 38 | cout<<"Lathos Kinisi!"; 39 | continue; 40 | } 41 | else 42 | step++; 43 | 44 | if (t.check_winner()!='-') 45 | { 46 | t.print(); 47 | cout<<"Nikise o "< 4 | using namespace std; 5 | 6 | #define N 3 7 | 8 | class tictactoe { 9 | public: 10 | tictactoe(); 11 | bool play(int x, int y); 12 | char check_winner(); 13 | void print(); 14 | void next_player(); 15 | char get_player() const; 16 | private: 17 | char array[N][N]; 18 | char player; 19 | }; 20 | 21 | 22 | int main() 23 | { 24 | tictactoe t; 25 | int x,y, step=0; 26 | 27 | while(true) 28 | { 29 | cout<<"PLAISIO: "<>x; 35 | cout<<"Dwse Y sintetagmeni: "; 36 | cin>>y; 37 | 38 | if (!t.play(x,y)) 39 | { 40 | cout<<"Lathos Kinisi!"; 41 | continue; 42 | } 43 | else 44 | step++; 45 | 46 | if (t.check_winner()!='-') 47 | { 48 | t.print(); 49 | cout<<"Nikise o "< 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class demigorgon 8 | { 9 | public: 10 | demigorgon(); 11 | int attack(); 12 | private: 13 | int height; 14 | int weight; 15 | int health; 16 | }; 17 | 18 | class demidog 19 | { 20 | public: 21 | demidog(); 22 | int attack(); 23 | private: 24 | int health; 25 | }; 26 | 27 | class hive 28 | { 29 | public: 30 | hive(bool in_demigorgon); 31 | int attack(); 32 | private: 33 | demigorgon *demi_ptr; 34 | demidog *array_demidogs; 35 | int n_demidogs; 36 | }; 37 | 38 | 39 | int main() 40 | { 41 | hive h(true); 42 | int damage; 43 | 44 | damage = h.attack(); 45 | 46 | cout<<"Total damage: "<attack(); 100 | sum+=damage; 101 | cout<<"Demigorgon attacks! (damage: "< 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy (int in_x); 8 | int get_x() const; 9 | void set_x(int in_x); 10 | private: 11 | int x; 12 | }; 13 | 14 | int main() 15 | { 16 | const dummy ob(3); 17 | 18 | //ob.set_x(4); //error 19 | 20 | return 0; 21 | } 22 | 23 | dummy::dummy(int in_x) 24 | { 25 | x=in_x; 26 | } 27 | 28 | int dummy::get_x() const 29 | { 30 | return x; 31 | } 32 | 33 | void dummy::set_x(int in_x) 34 | { 35 | x=in_x; 36 | } 37 | -------------------------------------------------------------------------------- /lesson05/cpp5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/CPP-programming/3350f77306648649679dd092aad224d875f3e1d3/lesson05/cpp5.pdf -------------------------------------------------------------------------------- /lesson05/cpp5.pointer_to_const_object.cpp: -------------------------------------------------------------------------------- 1 | /* cpp5.pointer_to_const_object.cpp Δείκτης προς const αντικείμενο */ 2 | #include 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy (int in_x); 8 | int get_x() const; 9 | void set_x(int in_x); 10 | private: 11 | int x; 12 | }; 13 | 14 | void f(const dummy *p); 15 | 16 | int main() 17 | { 18 | dummy ob(3); 19 | 20 | f(&ob); 21 | 22 | return 0; 23 | } 24 | 25 | dummy::dummy(int in_x) 26 | { 27 | x=in_x; 28 | } 29 | 30 | int dummy::get_x() const 31 | { 32 | return x; 33 | } 34 | 35 | void dummy::set_x(int in_x) 36 | { 37 | x=in_x; 38 | } 39 | 40 | void f(const dummy *p) 41 | { 42 | cout<get_x(); 43 | // p->set_x(2); // error 44 | } -------------------------------------------------------------------------------- /lesson05/cpp5.reference_to_const_object.cpp: -------------------------------------------------------------------------------- 1 | /* cpp5.reference_to_const_object.cpp Αναφορά προς const αντικείμενο */ 2 | #include 3 | using namespace std; 4 | 5 | class dummy { 6 | public: 7 | dummy (int in_x); 8 | int get_x() const; 9 | void set_x(int in_x); 10 | private: 11 | int x; 12 | }; 13 | 14 | void f(const dummy &ref); 15 | 16 | int main() 17 | { 18 | dummy ob(3); 19 | 20 | f(ob); 21 | 22 | return 0; 23 | } 24 | 25 | dummy::dummy(int in_x) 26 | { 27 | x=in_x; 28 | } 29 | 30 | int dummy::get_x() const 31 | { 32 | return x; 33 | } 34 | 35 | void dummy::set_x(int in_x) 36 | { 37 | x=in_x; 38 | } 39 | 40 | void f(const dummy &ref) 41 | { 42 | cout< 4 | using namespace std; 5 | 6 | class ARRAY { 7 | public: 8 | ARRAY(int in_n); 9 | ARRAY(const ARRAY &ob); 10 | ~ARRAY(); 11 | int get_n() const; 12 | int &operator[] (int i); 13 | void print(); 14 | private: 15 | int *p; 16 | int n; 17 | }; 18 | 19 | 20 | int main() 21 | { 22 | int n=10; 23 | ARRAY pin(n); 24 | 25 | for (int i=0; i=0 && i 4 | #include 5 | using namespace std; 6 | 7 | class STRING { 8 | public: 9 | STRING(); 10 | STRING(char *in_str); 11 | STRING(const STRING &ob); 12 | ~STRING(); 13 | int get_length() const; 14 | char *get_str() const; 15 | void set_str(char *in_str); 16 | void print(); 17 | STRING &operator=(STRING &right); 18 | private: 19 | int length; 20 | char *str; 21 | }; 22 | 23 | 24 | int main() 25 | { 26 | char s[80]="This is a string"; 27 | STRING s1(s); 28 | STRING s2; 29 | 30 | s2=s1; 31 | 32 | s1.print(); 33 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class STRING { 8 | public: 9 | STRING(); 10 | STRING(char *in_str); 11 | STRING(const STRING &ob); 12 | ~STRING(); 13 | int get_length() const; 14 | char *get_str() const; 15 | void set_str(char *in_str); 16 | void print(); 17 | STRING &operator=(STRING &right); 18 | char &operator[](int i); 19 | private: 20 | int length; 21 | char *str; 22 | }; 23 | 24 | 25 | int main() 26 | { 27 | char s[80]="This is a string"; 28 | STRING s1(s); 29 | STRING s2; 30 | 31 | s2=s1; 32 | 33 | s2[1]='a'; 34 | 35 | s1.print(); 36 | cout<=0 && i<=length) 120 | return str[i]; 121 | else 122 | cout<<"Index out of bounds!"; 123 | } 124 | -------------------------------------------------------------------------------- /lesson06/cpp6.askisi1.3.cpp: -------------------------------------------------------------------------------- 1 | /* cpp6.askisi1.3.cpp */ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class STRING { 8 | public: 9 | STRING(); 10 | STRING(char *in_str); 11 | STRING(const STRING &ob); 12 | ~STRING(); 13 | int get_length() const; 14 | char *get_str() const; 15 | void set_str(char *in_str); 16 | void print(); 17 | STRING &operator=(STRING &right); 18 | char &operator[](int i); 19 | bool operator<(STRING &right); 20 | bool operator>(STRING &right); 21 | bool operator==(STRING &right); 22 | private: 23 | int length; 24 | char *str; 25 | }; 26 | 27 | 28 | int main() 29 | { 30 | char s[80]="This is a string"; 31 | STRING s1(s); 32 | STRING s2; 33 | 34 | s2=s1; 35 | 36 | //s2[1]='a'; 37 | 38 | s1.print(); 39 | cout<=0 && i<=length) 129 | return str[i]; 130 | else 131 | cout<<"Index out of bounds!"; 132 | } 133 | 134 | bool STRING::operator<(STRING &right) 135 | { 136 | if (strcmp(str, right.str)<0) 137 | return true; 138 | return false; 139 | } 140 | 141 | bool STRING::operator>(STRING &right) 142 | { 143 | if (strcmp(str, right.str)>0) 144 | return true; 145 | return false; 146 | } 147 | 148 | bool STRING::operator==(STRING &right) 149 | { 150 | if (strcmp(str, right.str)==0) 151 | return true; 152 | return false; 153 | } -------------------------------------------------------------------------------- /lesson06/cpp6.askisi2.cpp: -------------------------------------------------------------------------------- 1 | /* cpp6.askisi2.cpp */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | class ARRAY { 7 | public: 8 | ARRAY(int in_n); 9 | ARRAY(const ARRAY &ob); 10 | ~ARRAY(); 11 | int get_n() const; 12 | void print(); 13 | int &operator[] (int i); 14 | ARRAY &operator= (ARRAY &right); 15 | ARRAY &operator+= (int right); 16 | private: 17 | int *p; 18 | int n; 19 | }; 20 | 21 | 22 | int main() 23 | { 24 | int n=10; 25 | ARRAY pin(n); 26 | ARRAY pin2(2); 27 | 28 | for (int i=0; i=0 && i0) 107 | { 108 | newp = new int [newn]; 109 | if (!newp) cout<<"Error Allocating Memory"; 110 | 111 | for (i=0; i 4 | using namespace std; 5 | 6 | 7 | class complex { 8 | public: 9 | complex(); 10 | complex(double in_real, double in_imag); 11 | void set_real(double in_real); 12 | void set_imag(double in_imag); 13 | double get_real() const; 14 | double get_imag() const; 15 | complex operator+ (const complex &right); 16 | complex operator+ (int right); 17 | complex operator- (const complex &right); 18 | complex operator++(int right); 19 | complex &operator++(); 20 | complex &operator= (const complex &right); 21 | private: 22 | double real; 23 | double imag; 24 | }; 25 | 26 | int main() 27 | { 28 | complex a(1.0,1.0); 29 | complex c; 30 | 31 | c=a; 32 | 33 | cout<<"a="< 4 | using namespace std; 5 | 6 | class complex { 7 | public: 8 | complex(); 9 | complex(double in_real, double in_imag); 10 | void set_real(double in_real); 11 | void set_imag(double in_imag); 12 | double get_real() const; 13 | double get_imag() const; 14 | complex operator+ (const complex &right); 15 | complex operator+ (int right); 16 | complex operator- (const complex &right); 17 | private: 18 | double real; 19 | double imag; 20 | }; 21 | 22 | int main() 23 | { 24 | complex a(1.0,1.0); 25 | complex b(2.0,3.0); 26 | 27 | complex c; 28 | 29 | c = a - b; 30 | 31 | cout< 4 | using namespace std; 5 | 6 | class complex { 7 | public: 8 | complex(); 9 | complex(double in_real, double in_imag); 10 | void set_real(double in_real); 11 | void set_imag(double in_imag); 12 | double get_real() const; 13 | double get_imag() const; 14 | complex operator+ (const complex &right); 15 | private: 16 | double real; 17 | double imag; 18 | }; 19 | 20 | int main() 21 | { 22 | complex a(1.0,1.0); 23 | complex b(2.0,3.0); 24 | 25 | complex c; 26 | 27 | c = a + b; 28 | 29 | cout< 4 | using namespace std; 5 | 6 | class complex { 7 | public: 8 | complex(); 9 | complex(double in_real, double in_imag); 10 | void set_real(double in_real); 11 | void set_imag(double in_imag); 12 | double get_real() const; 13 | double get_imag() const; 14 | complex operator+ (const complex &right); 15 | complex operator+ (int right); 16 | private: 17 | double real; 18 | double imag; 19 | }; 20 | 21 | int main() 22 | { 23 | complex a(1.0,1.0); 24 | 25 | complex c; 26 | 27 | c = a + 5; 28 | 29 | cout< 4 | using namespace std; 5 | 6 | 7 | class complex { 8 | public: 9 | complex(); 10 | complex(double in_real, double in_imag); 11 | void set_real(double in_real); 12 | void set_imag(double in_imag); 13 | double get_real() const; 14 | double get_imag() const; 15 | complex operator+ (const complex &right); 16 | complex operator+ (int right); 17 | complex operator- (const complex &right); 18 | complex operator++(int right); 19 | private: 20 | double real; 21 | double imag; 22 | }; 23 | 24 | int main() 25 | { 26 | complex a(1.0,1.0); 27 | complex c; 28 | 29 | c=a++; 30 | 31 | cout<<"a="< 4 | using namespace std; 5 | 6 | 7 | class complex { 8 | public: 9 | complex(); 10 | complex(double in_real, double in_imag); 11 | void set_real(double in_real); 12 | void set_imag(double in_imag); 13 | double get_real() const; 14 | double get_imag() const; 15 | complex operator+ (const complex &right); 16 | complex operator+ (int right); 17 | complex operator- (const complex &right); 18 | complex operator++(int right); 19 | complex &operator++(); 20 | private: 21 | double real; 22 | double imag; 23 | }; 24 | 25 | int main() 26 | { 27 | complex a(1.0,1.0); 28 | complex c; 29 | 30 | c=++a; 31 | 32 | cout<<"a="< 2 | #include "STRING.h" 3 | 4 | STRING::STRING() 5 | { 6 | length = 0; 7 | str = new char[1]; 8 | str[0]='\0'; 9 | } 10 | 11 | STRING::STRING(char *in_str) 12 | { 13 | length = strlen(in_str); 14 | 15 | str = new char [length+1]; 16 | if (!str) cout<<"Error Allocating Memory"; 17 | 18 | strcpy(str, in_str); 19 | } 20 | 21 | STRING::STRING(const char *in_str) 22 | { 23 | length = strlen(in_str); 24 | 25 | str = new char [length+1]; 26 | if (!str) cout<<"Error Allocating Memory"; 27 | 28 | strcpy(str, in_str); 29 | } 30 | 31 | STRING::STRING(const STRING &ob) 32 | { 33 | length = ob.get_length(); 34 | 35 | str = new char [length+1]; 36 | if (!str) cout<<"Error Allocating Memory"; 37 | 38 | strcpy(str, ob.get_str()); 39 | } 40 | 41 | STRING::~STRING() 42 | { 43 | delete [] str; 44 | } 45 | 46 | int STRING::get_length() const 47 | { 48 | return length; 49 | } 50 | 51 | char *STRING::get_str() const 52 | { 53 | return str; 54 | } 55 | 56 | void STRING::set_str(char *in_str) 57 | { 58 | if (str!=NULL) 59 | delete str; 60 | 61 | length = strlen(in_str); 62 | 63 | str = new char [length]; 64 | if (!str) cout<<"Error Allocating Memory"; 65 | 66 | strcpy(str, in_str); 67 | } 68 | 69 | STRING &STRING::operator=(STRING &right) 70 | { 71 | if (this==&right) return *this; 72 | 73 | length=right.length; 74 | 75 | delete [] str; 76 | str = new char [length+1]; 77 | if (!str) cout<<"Error Allocating Memory"; 78 | 79 | strcpy(str, right.str); 80 | 81 | return *this; 82 | } 83 | 84 | char &STRING::operator[](int i) 85 | { 86 | if (i>=0 && i<=length) 87 | return str[i]; 88 | else 89 | cout<<"Index out of bounds!"; 90 | } 91 | 92 | bool STRING::operator<(STRING &right) 93 | { 94 | if (strcmp(str, right.str)<0) 95 | return true; 96 | return false; 97 | } 98 | 99 | bool STRING::operator>(STRING &right) 100 | { 101 | if (strcmp(str, right.str)>0) 102 | return true; 103 | return false; 104 | } 105 | 106 | bool STRING::operator==(STRING &right) 107 | { 108 | if (strcmp(str, right.str)==0) 109 | return true; 110 | return false; 111 | } 112 | 113 | ostream &operator<<(ostream &left, const STRING &ob) 114 | { 115 | left<>(istream &left, STRING &ob) 121 | { 122 | char in_str[80]; 123 | 124 | left>>in_str; 125 | 126 | if (ob.str!=NULL) 127 | delete [] ob.str; 128 | 129 | ob.str = new char [strlen(in_str)+1]; 130 | if (ob.str==NULL) 131 | cout<<"Error allocating memory"; 132 | 133 | ob.length=strlen(in_str); 134 | strcpy(ob.str,in_str); 135 | 136 | return left; 137 | } 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /lesson07/STRING.h: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class STRING { 5 | public: 6 | STRING(); 7 | STRING(char *in_str); 8 | STRING(const char *in_str); 9 | STRING(const STRING &ob); 10 | ~STRING(); 11 | int get_length() const; 12 | char *get_str() const; 13 | void set_str(char *in_str); 14 | STRING &operator=(STRING &right); 15 | char &operator[](int i); 16 | bool operator<(STRING &right); 17 | bool operator>(STRING &right); 18 | bool operator==(STRING &right); 19 | friend ostream &operator<<(ostream &left, const STRING &ob); 20 | friend istream &operator>>(istream &left, STRING &ob); 21 | private: 22 | int length; 23 | char *str; 24 | }; 25 | -------------------------------------------------------------------------------- /lesson07/cpp7.askisi1.cpp: -------------------------------------------------------------------------------- 1 | /* cpp7.askisi1.cpp */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | class ARRAY { 7 | public: 8 | ARRAY(int in_n); 9 | ARRAY(const ARRAY &ob); 10 | ~ARRAY(); 11 | int get_n() const; 12 | int &operator[] (int i); 13 | ARRAY &operator= (ARRAY &right); 14 | ARRAY &operator+= (int right); 15 | friend ostream &operator<<(ostream &left, const ARRAY &ob); 16 | private: 17 | int *p; 18 | int n; 19 | }; 20 | 21 | 22 | int main() 23 | { 24 | int n=10; 25 | ARRAY pin(n); 26 | 27 | for (int i=0; i=0 && i0) 94 | { 95 | newp = new int [newn]; 96 | if (!newp) cout<<"Error Allocating Memory"; 97 | 98 | for (i=0; i 3 | #include "STRING.h" 4 | using namespace std; 5 | 6 | class table; // fwd declaration 7 | 8 | class chair { 9 | public: 10 | chair(STRING in_color, int in_height); 11 | friend ostream &operator<<(ostream &left, const chair &right); 12 | friend void shrink(chair &c, table &t); 13 | private: 14 | STRING color; 15 | int height; 16 | }; 17 | 18 | 19 | class table { 20 | public: 21 | table(STRING in_color, int in_width, int in_length); 22 | friend ostream &operator<<(ostream &left, const table &right); 23 | friend void shrink(chair &c, table &t); 24 | private: 25 | STRING color; 26 | int width; 27 | int length; 28 | }; 29 | 30 | void shrink(chair &c, table &t) 31 | { 32 | c.height = 0.9 * c.height; 33 | t.width = 0.9 * t.width; 34 | t.length = 0.9 * t.length; 35 | } 36 | 37 | 38 | int main() 39 | { 40 | chair c("Mple", 150); 41 | table t("Kokkino", 200, 200); 42 | 43 | cout< 4 | using namespace std; 5 | 6 | class point 7 | { 8 | public: 9 | point(int in_x, int in_y); 10 | int get_x() const; 11 | int get_y() const; 12 | void set_x(int in_x); 13 | void set_y(int in_y); 14 | friend ostream &operator<<(ostream &left, const point &right); 15 | private: 16 | int x; 17 | int y; 18 | }; 19 | 20 | 21 | int main() 22 | { 23 | point p(63,65); 24 | 25 | cout<=0 && in_x<60) 32 | x = in_x; 33 | else 34 | { 35 | cout<<"Lathos stin x-syntetagmeni. "; 36 | x = 0; 37 | } 38 | 39 | if (in_y>=0 && in_y<60) 40 | y = in_y; 41 | else 42 | { 43 | cout<<"Lathos stin y-syntetagmeni. "; 44 | y = 0; 45 | } 46 | } 47 | 48 | int point::get_x() const 49 | { 50 | return x; 51 | } 52 | 53 | int point::get_y() const 54 | { 55 | return y; 56 | } 57 | void point::set_x(int in_x) 58 | { 59 | x = in_x; 60 | } 61 | 62 | void point::set_y(int in_y) 63 | { 64 | y = in_y; 65 | } 66 | 67 | ostream &operator<<(ostream &left, const point &right) 68 | { 69 | cout<<"("< 2 | using namespace std; 3 | 4 | class man 5 | { 6 | public: 7 | friend class god; 8 | man(int in_sins); 9 | int get_sins() const; 10 | private: 11 | int sins; 12 | }; 13 | 14 | class god 15 | { 16 | public: 17 | void forgive(man &ob); 18 | }; 19 | 20 | int main() 21 | { 22 | man Euthypro(100); 23 | god Apollo; 24 | 25 | cout<<"Sins="<data=x; 53 | 54 | newnode->next=head; 55 | head=newnode; 56 | return true; 57 | } 58 | 59 | void linked_list::print() 60 | { 61 | node *current; 62 | 63 | current=head; 64 | while(current!=NULL) 65 | { 66 | cout<data<<" "; 67 | current=current->next; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /lesson07/cpp7.friend_function.cpp: -------------------------------------------------------------------------------- 1 | /* cpp7.friend_function.cpp: Φιλικές Συναρτήσεις */ 2 | #include 3 | using namespace std; 4 | 5 | class man 6 | { 7 | public: 8 | friend void good_weather(man &ob); 9 | man(int in_mood); 10 | void report_mood(); 11 | private: 12 | int mood; 13 | }; 14 | 15 | void good_weather(man &ob); 16 | 17 | int main() 18 | { 19 | man bob(5); 20 | 21 | bob.report_mood(); 22 | good_weather(bob); 23 | bob.report_mood(); 24 | 25 | return 0; 26 | } 27 | 28 | man::man(int in_mood) 29 | { 30 | mood = in_mood; 31 | } 32 | 33 | void man::report_mood() 34 | { 35 | if (mood<10) 36 | cout<<"I am ok..."< 4 | using namespace std; 5 | 6 | class man; //forward declaration 7 | 8 | class state 9 | { 10 | public: 11 | void withdraw(man &ob); 12 | }; 13 | 14 | class man 15 | { 16 | public: 17 | friend void state::withdraw(man &ob); 18 | man(int in_money); 19 | int get_money() const; 20 | private: 21 | int money; 22 | }; 23 | 24 | int main() 25 | { 26 | man Papadakis(1500); 27 | state Greece; 28 | 29 | cout<<"Papadakis' Money=" 30 | < 4 | using namespace std; 5 | 6 | 7 | class complex { 8 | public: 9 | complex(); 10 | complex(double in_real, double in_imag); 11 | void set_real(double in_real); 12 | void set_imag(double in_imag); 13 | double get_real() const; 14 | double get_imag() const; 15 | complex operator+ (const complex &right); 16 | complex operator- (const complex &right); 17 | complex operator++(int right); 18 | complex &operator++(); 19 | complex &operator= (const complex &right); 20 | friend complex operator+(int left, complex &right); 21 | friend ostream &operator<<(ostream &left, const complex &right); 22 | private: 23 | double real; 24 | double imag; 25 | }; 26 | 27 | ostream &operator<<(ostream &left, const complex &right); 28 | complex operator+(int left, complex &right); 29 | 30 | int main() 31 | { 32 | complex a(1.0,1.0); 33 | 34 | cout< 4 | using namespace std; 5 | 6 | class ARRAY { 7 | public: 8 | static int MAX_LENGTH; 9 | ARRAY(int in_n); 10 | ARRAY(const ARRAY &ob); 11 | ~ARRAY(); 12 | int get_n() const; 13 | int &operator[] (int i); 14 | ARRAY &operator= (ARRAY &right); 15 | ARRAY &operator+= (int right); 16 | friend ostream &operator<<(ostream &left, const ARRAY &ob); 17 | private: 18 | int *p; 19 | int n; 20 | }; 21 | 22 | int ARRAY::MAX_LENGTH = 1000; 23 | 24 | 25 | int main() 26 | { 27 | int n=1001; 28 | ARRAY pin(n); 29 | 30 | 31 | return 0; 32 | } 33 | 34 | ARRAY::ARRAY(int in_n) 35 | { 36 | if (in_n>MAX_LENGTH) 37 | { 38 | cout<<"n is too big!"; 39 | n=0; 40 | } 41 | 42 | else 43 | { 44 | n = in_n; 45 | 46 | p = new int [n]; 47 | if (!p) 48 | cout<<"Error allocating memory!"; 49 | } 50 | } 51 | 52 | ARRAY::ARRAY(const ARRAY &ob) 53 | { 54 | n = ob.get_n(); 55 | 56 | p = new int [n]; 57 | if (!p) cout<<"Memory Allocation Error!"; 58 | } 59 | 60 | ARRAY::~ARRAY() 61 | { 62 | if (n>0) 63 | delete [] p; 64 | } 65 | 66 | int ARRAY::get_n() const 67 | { 68 | return n; 69 | } 70 | 71 | int &ARRAY::operator[] (int i) 72 | { 73 | if (i>=0 && i0) 102 | { 103 | newp = new int [newn]; 104 | if (!newp) cout<<"Error Allocating Memory"; 105 | 106 | for (i=0; i 3 | using namespace std; 4 | 5 | int fib(int n); 6 | 7 | int main() 8 | { 9 | cout< 3 | using namespace std; 4 | 5 | class life { 6 | public: 7 | int x; 8 | life(int in_x); 9 | ~life(); 10 | }; 11 | 12 | int main() 13 | { 14 | static life ob1(1); 15 | cout<<"Start Main"< 3 | using namespace std; 4 | 5 | void func(); 6 | 7 | int main() 8 | { 9 | int i; 10 | 11 | for (i=0; i<10; i++) 12 | func(); 13 | 14 | return 0; 15 | } 16 | void func() 17 | { 18 | int y=0; 19 | static int x=0; 20 | 21 | x=x+1; 22 | y=y+1; 23 | cout< 4 | #include 5 | using namespace std; 6 | 7 | #define SIZE 10 8 | 9 | void insertion_sort(string *pinakas, int N); 10 | void swap(string &s1, string &s2); 11 | 12 | int main() 13 | { 14 | string pin[SIZE]={"abcd","adas","asdr","aqwe","qwetyy","asdasd","asdasdq","xcvxc","ytreh","werwer"}; 15 | 16 | insertion_sort(pin,SIZE); 17 | 18 | for (int i=0; i=1; j--) 35 | { 36 | if (pinakas[j] 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #define N 10 9 | 10 | int main() 11 | { 12 | string words[N]={"bond", "neo", "boromir", "saruman", "gandalf", "sansha", "stark", "spiderman", "frodo", "morpheus"}; 13 | 14 | /* Choose random words */ 15 | 16 | srand(time(0)); 17 | 18 | string hidden = words[rand()%10]; 19 | 20 | /* Arxikopoiisi game */ 21 | 22 | string game(hidden.length(), '-'); 23 | 24 | /* Guesses */ 25 | 26 | string bad_guesses; 27 | string right_guesses; 28 | 29 | /* MAX */ 30 | 31 | int MAX = 10; 32 | int wrong_guesses = 0; 33 | 34 | /* algorithm */ 35 | char letter; 36 | 37 | while (true) 38 | { 39 | cout<>letter; 42 | 43 | if (bad_guesses.find(letter)!=bad_guesses.npos) 44 | { 45 | cout<<"To gramma exei epilexthei"< 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s("Initial string"); 10 | 11 | cout< 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s("0123456789"); 10 | 11 | cout<<"Initial: "< 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s1; 10 | string s2("Medium"); 11 | string s3("A rather large one"); 12 | 13 | cout<<"Capacity s1: "< 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s("0123456789"); 10 | string f("345"); 11 | 12 | s+=s; 13 | cout<<"str- find-345c: pos: "< 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s1("abcd"); 10 | string s2; 11 | 12 | s2=s1+s1+s1; 13 | 14 | s2[4]='A'; 15 | 16 | if (s1>s2) 17 | cout< 4 | #include 5 | using namespace std; 6 | 7 | #define SIZE 100 8 | 9 | class video { 10 | public: 11 | int get_duration() const; 12 | void set_video(string in_artist_name, string in_track_name, int in_duration); 13 | friend ostream &operator<<(ostream &left, video &right); 14 | private: 15 | string artist_name; 16 | string track_name; 17 | int duration; 18 | }; 19 | 20 | 21 | class playlist { 22 | public: 23 | playlist(string in_name, string in_descr); 24 | void add_video(video in_video); 25 | friend ostream &operator<<(ostream &left, playlist &right); 26 | private: 27 | string name; 28 | string descr; 29 | int duration; 30 | video videos[SIZE]; 31 | int videos_size; 32 | }; 33 | 34 | int main() 35 | { 36 | playlist l("my playlist", "the best songs"); 37 | 38 | video temp; 39 | 40 | temp.set_video("Mpagianteras", "Sa magemeno to mialo moy", 207); 41 | l.add_video(temp); 42 | temp.set_video("Sin Boy", "Mama", 400); 43 | l.add_video(temp); 44 | 45 | cout< 4 | #include 5 | using namespace std; 6 | 7 | #define SIZE 100 8 | 9 | class video { 10 | public: 11 | int get_duration() const; 12 | void set_video(string in_artist_name, string in_track_name, int in_duration); 13 | friend ostream &operator<<(ostream &left, video &right); 14 | private: 15 | string artist_name; 16 | string track_name; 17 | int duration; 18 | }; 19 | 20 | class playlist { 21 | public: 22 | playlist(string in_name, string in_descr); 23 | void add_video(video in_video); 24 | friend ostream &operator<<(ostream &left, playlist &right); 25 | protected: 26 | string name; 27 | string descr; 28 | int duration; 29 | video videos[SIZE]; 30 | int videos_size; 31 | }; 32 | 33 | class classical_playlist: public playlist { 34 | public: 35 | classical_playlist(string in_name, string in_descr, string in_period); 36 | friend ostream &operator<<(ostream &left, classical_playlist &right); 37 | private: 38 | string period; 39 | }; 40 | 41 | int main() 42 | { 43 | classical_playlist l("my playlist", "the best songs", "rempetiko"); 44 | 45 | video temp; 46 | 47 | temp.set_video("Mpagianteras", "Sa magemeno to mialo moy", 207); 48 | l.add_video(temp); 49 | temp.set_video("Sin Boy", "Mama", 400); 50 | l.add_video(temp); 51 | 52 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class card { 8 | public: 9 | card(string in_name, string in_color, string in_rarity); 10 | friend ostream &operator<<(ostream &left, card &right); 11 | protected: 12 | string name; 13 | string color; 14 | string rarity; 15 | }; 16 | 17 | class creature: public card { 18 | public: 19 | creature(string in_name, string in_color, string in_rarity, int in_attack, int in_defense, string in_kind); 20 | friend ostream &operator<<(ostream &left, creature &right); 21 | private: 22 | int attack; 23 | int defense; 24 | string kind; 25 | }; 26 | 27 | 28 | int main() 29 | { 30 | creature c("Akroan Crusader", "red", "common",1,1,"Human Soldier"); 31 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class card { 8 | public: 9 | card(string in_name, string in_color, string in_rarity); 10 | friend ostream &operator<<(ostream &left, card &right); 11 | protected: 12 | string name; 13 | string color; 14 | string rarity; 15 | }; 16 | 17 | class creature: public card { 18 | public: 19 | creature(string in_name, string in_color, string in_rarity, int in_attack, int in_defense, string in_kind); 20 | friend ostream &operator<<(ostream &left, creature &right); 21 | private: 22 | int attack; 23 | int defense; 24 | string kind; 25 | }; 26 | 27 | class land: public card { 28 | public: 29 | land(string in_name, string in_color, string in_rarity, int in_mana, string in_descr, bool in_tap); 30 | friend ostream &operator<<(ostream &left, land &right); 31 | private: 32 | int mana; 33 | string descr; 34 | bool tap; 35 | }; 36 | 37 | int main() 38 | { 39 | creature c("Akroan Crusader", "red", "common",1,1,"Human Soldier"); 40 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | int get_weight() const; 11 | int get_height() const; 12 | void set_weight(int in_weight); 13 | void set_height(int in_height); 14 | private: 15 | int weight; 16 | int height; 17 | }; 18 | 19 | class dog: public animal { 20 | public: 21 | dog(int in_weight, int in_height, string in_house_address); 22 | void bark(); 23 | void set_house_address(string in_house_address); 24 | string get_house_address() const; 25 | friend ostream &operator<<(ostream &left, const dog &right); 26 | private: 27 | string house_address; 28 | }; 29 | 30 | 31 | int main() 32 | { 33 | dog piko(10,35,"Left Hill 154"); 34 | 35 | piko.bark(); 36 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | ~animal(); 11 | int get_weight() const; 12 | int get_height() const; 13 | void set_weight(int in_weight); 14 | void set_height(int in_height); 15 | private: 16 | int weight; 17 | int height; 18 | }; 19 | 20 | class dog: public animal { 21 | public: 22 | dog(int in_weight, int in_height, string in_house_address); 23 | ~dog(); 24 | void bark(); 25 | void set_house_address(string in_house_address); 26 | string get_house_address() const; 27 | friend ostream &operator<<(ostream &left, const dog &right); 28 | private: 29 | string house_address; 30 | }; 31 | 32 | 33 | int main() 34 | { 35 | dog piko(10,35,"Left Hill 154"); 36 | 37 | piko.bark(); 38 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class B { 8 | public: 9 | int pub; 10 | void f(); 11 | protected: 12 | int pro; 13 | private: 14 | int pri; 15 | }; 16 | 17 | class D: public B { 18 | public: 19 | void g(); 20 | }; 21 | 22 | int main() 23 | { 24 | B b; 25 | b.pub=1; 26 | //b.pro=1; //no access 27 | //b.pri=1; //no access 28 | D d; 29 | d.pub=1; 30 | //d.bpro=1; //no access 31 | //d.bpri=1; //no access 32 | } 33 | 34 | void B::f() 35 | { 36 | pub=1; 37 | pro=1; 38 | pri=1; 39 | } 40 | 41 | void D::g() 42 | { 43 | pub=1; 44 | pro=1; 45 | //pri=1; //no access 46 | } -------------------------------------------------------------------------------- /lesson10/cpp10.public_inheritance.cpp: -------------------------------------------------------------------------------- 1 | /* cpp10.public_inheritance.cpp */ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | int get_weight() const; 10 | int get_height() const; 11 | void set_weight(int in_weight); 12 | void set_height(int in_height); 13 | private: 14 | int weight; 15 | int height; 16 | }; 17 | 18 | class dog: public animal { 19 | public: 20 | void bark(); 21 | void set_house_address(string in_house_address); 22 | string get_house_address() const; 23 | friend ostream &operator<<(ostream &left, const dog &right); 24 | private: 25 | string house_address; 26 | }; 27 | 28 | 29 | int main() 30 | { 31 | dog piko; 32 | 33 | piko.set_house_address("Left Hill 154"); 34 | piko.set_weight(10); 35 | piko.set_height(35); 36 | 37 | 38 | piko.bark(); 39 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | int get_weight() const; 11 | int get_height() const; 12 | void set_weight(int in_weight); 13 | void set_height(int in_height); 14 | virtual void make_sound() = 0; 15 | private: 16 | int weight; 17 | int height; 18 | }; 19 | 20 | class dog: public animal { 21 | public: 22 | dog(int in_weight, int in_height, string in_house_address); 23 | void set_house_address(string in_house_address); 24 | string get_house_address() const; 25 | void make_sound(); 26 | friend ostream &operator<<(ostream &left, const dog &right); 27 | private: 28 | string house_address; 29 | }; 30 | 31 | class cat: public animal { 32 | public: 33 | cat(int in_weight, int in_height); 34 | void make_sound(); 35 | friend ostream &operator<<(ostream &left, const cat &right); 36 | }; 37 | 38 | 39 | int main() 40 | { 41 | animal *bp = new dog(10,35,"Left Hill 154"); 42 | 43 | delete bp; 44 | 45 | return 0; 46 | } 47 | 48 | 49 | animal::animal(int in_weight, int in_height) 50 | { 51 | weight = in_weight; 52 | height = in_height; 53 | } 54 | 55 | 56 | void animal::set_weight(int in_weight) 57 | { 58 | weight = in_weight; 59 | } 60 | 61 | void animal::set_height(int in_height) 62 | { 63 | height = in_height; 64 | } 65 | 66 | int animal::get_weight() const 67 | { 68 | return weight; 69 | } 70 | 71 | int animal::get_height() const 72 | { 73 | return height; 74 | } 75 | 76 | 77 | 78 | dog::dog(int in_weight, int in_height, string in_house_address): 79 | animal(in_weight, in_height) 80 | { 81 | house_address=in_house_address; 82 | } 83 | 84 | void dog::set_house_address(string in_house_address) 85 | { 86 | house_address = in_house_address; 87 | } 88 | 89 | string dog::get_house_address() const 90 | { 91 | return house_address; 92 | } 93 | 94 | void dog::make_sound() 95 | { 96 | cout<<"Arf arf"< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | ~animal(); 11 | int get_weight() const; 12 | int get_height() const; 13 | void set_weight(int in_weight); 14 | void set_height(int in_height); 15 | void make_sound(); 16 | private: 17 | int weight; 18 | int height; 19 | }; 20 | 21 | class dog: public animal { 22 | public: 23 | dog(int in_weight, int in_height, string in_house_address); 24 | ~dog(); 25 | void set_house_address(string in_house_address); 26 | string get_house_address() const; 27 | void make_sound(); 28 | friend ostream &operator<<(ostream &left, const dog &right); 29 | private: 30 | string house_address; 31 | }; 32 | 33 | class cat: public animal { 34 | public: 35 | cat(int in_weight, int in_height); 36 | void make_sound(); 37 | friend ostream &operator<<(ostream &left, const cat &right); 38 | }; 39 | 40 | 41 | int main() 42 | { 43 | animal *bp = new dog(10,35,"Left Hill 154"); 44 | 45 | delete bp; 46 | 47 | return 0; 48 | } 49 | 50 | 51 | animal::animal(int in_weight, int in_height) 52 | { 53 | weight = in_weight; 54 | height = in_height; 55 | } 56 | 57 | animal::~animal() 58 | { 59 | cout<<"Destructing animal..."; 60 | } 61 | 62 | void animal::set_weight(int in_weight) 63 | { 64 | weight = in_weight; 65 | } 66 | 67 | void animal::set_height(int in_height) 68 | { 69 | height = in_height; 70 | } 71 | 72 | int animal::get_weight() const 73 | { 74 | return weight; 75 | } 76 | 77 | int animal::get_height() const 78 | { 79 | return height; 80 | } 81 | 82 | void animal::make_sound() 83 | { 84 | cout<<"Animal sound"< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | ~animal(); 11 | int get_weight() const; 12 | int get_height() const; 13 | void set_weight(int in_weight); 14 | void set_height(int in_height); 15 | void make_sound(); 16 | private: 17 | int weight; 18 | int height; 19 | }; 20 | 21 | class dog: public animal { 22 | public: 23 | dog(int in_weight, int in_height, string in_house_address); 24 | ~dog(); 25 | void set_house_address(string in_house_address); 26 | string get_house_address() const; 27 | void make_sound(); 28 | friend ostream &operator<<(ostream &left, const dog &right); 29 | private: 30 | string house_address; 31 | }; 32 | 33 | class cat: public animal { 34 | public: 35 | cat(int in_weight, int in_height); 36 | void make_sound(); 37 | friend ostream &operator<<(ostream &left, const cat &right); 38 | }; 39 | 40 | 41 | int main() 42 | { 43 | dog *dp= new dog(10,35,"Left Hill 154"); 44 | 45 | delete dp; 46 | 47 | return 0; 48 | } 49 | 50 | 51 | animal::animal(int in_weight, int in_height) 52 | { 53 | weight = in_weight; 54 | height = in_height; 55 | } 56 | 57 | animal::~animal() 58 | { 59 | cout<<"Destructing animal..."; 60 | } 61 | 62 | void animal::set_weight(int in_weight) 63 | { 64 | weight = in_weight; 65 | } 66 | 67 | void animal::set_height(int in_height) 68 | { 69 | height = in_height; 70 | } 71 | 72 | int animal::get_weight() const 73 | { 74 | return weight; 75 | } 76 | 77 | int animal::get_height() const 78 | { 79 | return height; 80 | } 81 | 82 | void animal::make_sound() 83 | { 84 | cout<<"Animal sound"< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | int get_weight() const; 11 | int get_height() const; 12 | void set_weight(int in_weight); 13 | void set_height(int in_height); 14 | void make_sound(); 15 | private: 16 | int weight; 17 | int height; 18 | }; 19 | 20 | class dog: public animal { 21 | public: 22 | dog(int in_weight, int in_height, string in_house_address); 23 | void set_house_address(string in_house_address); 24 | string get_house_address() const; 25 | void make_sound(); 26 | friend ostream &operator<<(ostream &left, const dog &right); 27 | private: 28 | string house_address; 29 | }; 30 | 31 | class cat: public animal { 32 | public: 33 | cat(int in_weight, int in_height); 34 | void make_sound(); 35 | friend ostream &operator<<(ostream &left, const cat &right); 36 | }; 37 | 38 | 39 | int main() 40 | { 41 | dog piko(10,35,"Left Hill 154"); 42 | cat kitty(6, 20); 43 | animal an(40, 80); 44 | 45 | piko.make_sound(); 46 | kitty.make_sound(); 47 | an.make_sound(); 48 | 49 | return 0; 50 | } 51 | 52 | 53 | animal::animal(int in_weight, int in_height) 54 | { 55 | weight = in_weight; 56 | height = in_height; 57 | } 58 | 59 | void animal::set_weight(int in_weight) 60 | { 61 | weight = in_weight; 62 | } 63 | 64 | void animal::set_height(int in_height) 65 | { 66 | height = in_height; 67 | } 68 | 69 | int animal::get_weight() const 70 | { 71 | return weight; 72 | } 73 | 74 | int animal::get_height() const 75 | { 76 | return height; 77 | } 78 | 79 | void animal::make_sound() 80 | { 81 | cout<<"Animal sound"< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | int get_weight() const; 11 | int get_height() const; 12 | void set_weight(int in_weight); 13 | void set_height(int in_height); 14 | void make_sound(); 15 | private: 16 | int weight; 17 | int height; 18 | }; 19 | 20 | class dog: public animal { 21 | public: 22 | dog(int in_weight, int in_height, string in_house_address); 23 | void set_house_address(string in_house_address); 24 | string get_house_address() const; 25 | void make_sound(); 26 | friend ostream &operator<<(ostream &left, const dog &right); 27 | private: 28 | string house_address; 29 | }; 30 | 31 | class cat: public animal { 32 | public: 33 | cat(int in_weight, int in_height); 34 | void make_sound(); 35 | friend ostream &operator<<(ostream &left, const cat &right); 36 | }; 37 | 38 | 39 | int main() 40 | { 41 | animal *bpd= new dog(10,35,"Left Hill 154"); 42 | animal *bpc= new cat(6, 20); 43 | animal *bpa=new animal(40, 80); 44 | 45 | bpd->make_sound(); 46 | bpc->make_sound(); 47 | bpa->make_sound(); 48 | 49 | delete bpd; 50 | delete bpc; 51 | delete bpa; 52 | 53 | return 0; 54 | } 55 | 56 | 57 | animal::animal(int in_weight, int in_height) 58 | { 59 | weight = in_weight; 60 | height = in_height; 61 | } 62 | 63 | void animal::set_weight(int in_weight) 64 | { 65 | weight = in_weight; 66 | } 67 | 68 | void animal::set_height(int in_height) 69 | { 70 | height = in_height; 71 | } 72 | 73 | int animal::get_weight() const 74 | { 75 | return weight; 76 | } 77 | 78 | int animal::get_height() const 79 | { 80 | return height; 81 | } 82 | 83 | void animal::make_sound() 84 | { 85 | cout<<"Animal sound"< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | int get_weight() const; 11 | int get_height() const; 12 | void set_weight(int in_weight); 13 | void set_height(int in_height); 14 | void make_sound(); 15 | private: 16 | int weight; 17 | int height; 18 | }; 19 | 20 | class dog: public animal { 21 | public: 22 | dog(int in_weight, int in_height, string in_house_address); 23 | void set_house_address(string in_house_address); 24 | string get_house_address() const; 25 | void make_sound(); 26 | friend ostream &operator<<(ostream &left, const dog &right); 27 | private: 28 | string house_address; 29 | }; 30 | 31 | class cat: public animal { 32 | public: 33 | cat(int in_weight, int in_height); 34 | void make_sound(); 35 | friend ostream &operator<<(ostream &left, const cat &right); 36 | }; 37 | 38 | 39 | int main() 40 | { 41 | animal *pin[3]; 42 | pin[0] = new dog(10,35,"Left Hill 154"); 43 | pin[1] = new cat(6, 20); 44 | pin[2] = new animal(40, 80); 45 | 46 | for (int i=0; i<3; i++) 47 | pin[i]->make_sound(); 48 | 49 | return 0; 50 | } 51 | 52 | 53 | 54 | animal::animal(int in_weight, int in_height) 55 | { 56 | weight = in_weight; 57 | height = in_height; 58 | } 59 | 60 | void animal::set_weight(int in_weight) 61 | { 62 | weight = in_weight; 63 | } 64 | 65 | void animal::set_height(int in_height) 66 | { 67 | height = in_height; 68 | } 69 | 70 | int animal::get_weight() const 71 | { 72 | return weight; 73 | } 74 | 75 | int animal::get_height() const 76 | { 77 | return height; 78 | } 79 | 80 | void animal::make_sound() 81 | { 82 | cout<<"Animal sound"< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | virtual ~animal(); 11 | int get_weight() const; 12 | int get_height() const; 13 | void set_weight(int in_weight); 14 | void set_height(int in_height); 15 | void make_sound(); 16 | private: 17 | int weight; 18 | int height; 19 | }; 20 | 21 | class dog: public animal { 22 | public: 23 | dog(int in_weight, int in_height, string in_house_address); 24 | ~dog(); 25 | void set_house_address(string in_house_address); 26 | string get_house_address() const; 27 | void make_sound(); 28 | friend ostream &operator<<(ostream &left, const dog &right); 29 | private: 30 | string house_address; 31 | }; 32 | 33 | class cat: public animal { 34 | public: 35 | cat(int in_weight, int in_height); 36 | void make_sound(); 37 | friend ostream &operator<<(ostream &left, const cat &right); 38 | }; 39 | 40 | 41 | int main() 42 | { 43 | animal *bp = new dog(10,35,"Left Hill 154"); 44 | 45 | delete bp; 46 | 47 | return 0; 48 | } 49 | 50 | 51 | animal::animal(int in_weight, int in_height) 52 | { 53 | weight = in_weight; 54 | height = in_height; 55 | } 56 | 57 | animal::~animal() 58 | { 59 | cout<<"Destructing animal..."; 60 | } 61 | 62 | void animal::set_weight(int in_weight) 63 | { 64 | weight = in_weight; 65 | } 66 | 67 | void animal::set_height(int in_height) 68 | { 69 | height = in_height; 70 | } 71 | 72 | int animal::get_weight() const 73 | { 74 | return weight; 75 | } 76 | 77 | int animal::get_height() const 78 | { 79 | return height; 80 | } 81 | 82 | void animal::make_sound() 83 | { 84 | cout<<"Animal sound"< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | int get_weight() const; 11 | int get_height() const; 12 | void set_weight(int in_weight); 13 | void set_height(int in_height); 14 | virtual void make_sound(); 15 | private: 16 | int weight; 17 | int height; 18 | }; 19 | 20 | class dog: public animal { 21 | public: 22 | dog(int in_weight, int in_height, string in_house_address); 23 | void set_house_address(string in_house_address); 24 | string get_house_address() const; 25 | void make_sound(); 26 | friend ostream &operator<<(ostream &left, const dog &right); 27 | private: 28 | string house_address; 29 | }; 30 | 31 | class cat: public animal { 32 | public: 33 | cat(int in_weight, int in_height); 34 | void make_sound(); 35 | friend ostream &operator<<(ostream &left, const cat &right); 36 | }; 37 | 38 | 39 | int main() 40 | { 41 | animal *pin[3]; 42 | pin[0] = new dog(10,35,"Left Hill 154"); 43 | pin[1] = new cat(6, 20); 44 | pin[2] = new animal(40, 80); 45 | 46 | for (int i=0; i<3; i++) 47 | pin[i]->make_sound(); 48 | 49 | for (int i=0; i<3; i++) 50 | delete pin[i]; 51 | 52 | return 0; 53 | } 54 | 55 | 56 | 57 | animal::animal(int in_weight, int in_height) 58 | { 59 | weight = in_weight; 60 | height = in_height; 61 | } 62 | 63 | void animal::set_weight(int in_weight) 64 | { 65 | weight = in_weight; 66 | } 67 | 68 | void animal::set_height(int in_height) 69 | { 70 | height = in_height; 71 | } 72 | 73 | int animal::get_weight() const 74 | { 75 | return weight; 76 | } 77 | 78 | int animal::get_height() const 79 | { 80 | return height; 81 | } 82 | 83 | void animal::make_sound() 84 | { 85 | cout<<"Animal sound"< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | int get_weight() const; 11 | int get_height() const; 12 | void set_weight(int in_weight); 13 | void set_height(int in_height); 14 | virtual void make_sound(); 15 | private: 16 | int weight; 17 | int height; 18 | }; 19 | 20 | class dog: public animal { 21 | public: 22 | dog(int in_weight, int in_height, string in_house_address); 23 | void set_house_address(string in_house_address); 24 | string get_house_address() const; 25 | void make_sound(); 26 | friend ostream &operator<<(ostream &left, const dog &right); 27 | private: 28 | string house_address; 29 | }; 30 | 31 | class cat: public animal { 32 | public: 33 | cat(int in_weight, int in_height); 34 | void make_sound(); 35 | friend ostream &operator<<(ostream &left, const cat &right); 36 | }; 37 | 38 | 39 | int main() 40 | { 41 | animal pin[3] = { 42 | dog(10,35,"Left Hill 154"), 43 | cat(6, 20), 44 | animal(40, 80) 45 | }; 46 | 47 | for (int i=0; i<3; i++) 48 | pin[i].make_sound(); 49 | 50 | return 0; 51 | } 52 | 53 | 54 | 55 | animal::animal(int in_weight, int in_height) 56 | { 57 | weight = in_weight; 58 | height = in_height; 59 | } 60 | 61 | void animal::set_weight(int in_weight) 62 | { 63 | weight = in_weight; 64 | } 65 | 66 | void animal::set_height(int in_height) 67 | { 68 | height = in_height; 69 | } 70 | 71 | int animal::get_weight() const 72 | { 73 | return weight; 74 | } 75 | 76 | int animal::get_height() const 77 | { 78 | return height; 79 | } 80 | 81 | void animal::make_sound() 82 | { 83 | cout<<"Animal sound"< 4 | #include 5 | using namespace std; 6 | 7 | class animal { 8 | public: 9 | animal(int in_weight, int in_height); 10 | int get_weight() const; 11 | int get_height() const; 12 | void set_weight(int in_weight); 13 | void set_height(int in_height); 14 | virtual void make_sound(); 15 | private: 16 | int weight; 17 | int height; 18 | }; 19 | 20 | class dog: public animal { 21 | public: 22 | dog(int in_weight, int in_height, string in_house_address); 23 | void set_house_address(string in_house_address); 24 | string get_house_address() const; 25 | void make_sound(); 26 | friend ostream &operator<<(ostream &left, const dog &right); 27 | private: 28 | string house_address; 29 | }; 30 | 31 | class cat: public animal { 32 | public: 33 | cat(int in_weight, int in_height); 34 | void make_sound(); 35 | friend ostream &operator<<(ostream &left, const cat &right); 36 | }; 37 | 38 | void f(animal &ob) 39 | { 40 | ob.make_sound(); 41 | } 42 | 43 | int main() 44 | { 45 | dog d(10,35,"Left Hill 154"); 46 | 47 | f(d); 48 | 49 | return 0; 50 | } 51 | 52 | 53 | 54 | animal::animal(int in_weight, int in_height) 55 | { 56 | weight = in_weight; 57 | height = in_height; 58 | } 59 | 60 | void animal::set_weight(int in_weight) 61 | { 62 | weight = in_weight; 63 | } 64 | 65 | void animal::set_height(int in_height) 66 | { 67 | height = in_height; 68 | } 69 | 70 | int animal::get_weight() const 71 | { 72 | return weight; 73 | } 74 | 75 | int animal::get_height() const 76 | { 77 | return height; 78 | } 79 | 80 | void animal::make_sound() 81 | { 82 | cout<<"Animal sound"< 3 | #include 4 | using namespace std; 5 | 6 | 7 | class movie { 8 | public: 9 | movie(string in_title, double in_imdb_score, int in_weeks); 10 | virtual double score(); 11 | private: 12 | string title; 13 | double imdb_score; 14 | int weeks; 15 | }; 16 | 17 | 18 | class thriller: public movie { 19 | public: 20 | thriller(string in_title, double in_imdb_score, int in_weeks, double in_fear_factor); 21 | double score(); 22 | private: 23 | double fear_factor; 24 | }; 25 | 26 | 27 | int main() 28 | { 29 | thriller m1("It Chapter 2", 6.8, 10, 8.0); 30 | thriller m2("Us", 6.9, 30, 7.0); 31 | thriller m3("The Silence of Lambs", 8.6, 1540, 9.0); 32 | 33 | cout< 3 | #include 4 | using namespace std; 5 | 6 | 7 | class movie { 8 | public: 9 | movie(); 10 | movie(string in_title, double in_imdb_score, int in_weeks); 11 | virtual double score(); 12 | private: 13 | string title; 14 | double imdb_score; 15 | int weeks; 16 | }; 17 | 18 | 19 | class thriller: virtual public movie { 20 | public: 21 | thriller(double in_fear_factor); 22 | thriller(string in_title, double in_imdb_score, int in_weeks, double in_fear_factor); 23 | virtual double score(); 24 | protected: 25 | double fear_factor; 26 | }; 27 | 28 | 29 | class comedy: virtual public movie { 30 | public: 31 | comedy(double in_fun_factor); 32 | comedy(string in_title, double in_imdb_score, int in_weeks, double in_fun_factor); 33 | virtual double score(); 34 | protected: 35 | double fun_factor; 36 | }; 37 | 38 | class comedy_thriller: public thriller, public comedy { 39 | public: 40 | comedy_thriller(string in_title, double in_imdb_score, int in_weeks, double in_fear_factor, double in_fun_factor); 41 | double score(); 42 | }; 43 | 44 | 45 | int main() 46 | { 47 | thriller m1("It Chapter 2", 6.8, 10, 8.0); 48 | thriller m2("Us", 6.9, 30, 7.0); 49 | thriller m3("The Silence of Lambs", 8.6, 1540, 9.0); 50 | comedy m4("Jexi", 6.6, 4, 3.0); 51 | comedy_thriller m5("Addams Family", 5.9, 5, 3.0, 4.0); 52 | comedy_thriller m6("Shaun of the Dead", 7.9, 825, 4.0, 9.0); 53 | 54 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class person { 8 | public: 9 | person(); 10 | person(string in_name, int in_salary); 11 | private: 12 | string name; 13 | int salary; 14 | }; 15 | 16 | class barista: virtual public person { 17 | public: 18 | barista(); 19 | barista(string in_name, int in_salary); 20 | void prepare(); 21 | private: 22 | }; 23 | 24 | class waiter: virtual public person { 25 | public: 26 | waiter(); 27 | waiter(string in_name, int in_salary); 28 | void serve(int customers, barista &b); 29 | private: 30 | int customers_served; 31 | }; 32 | 33 | class owner: public barista, public waiter { 34 | public: 35 | owner(string in_name, int in_salary); 36 | private: 37 | 38 | }; 39 | 40 | int main() 41 | { 42 | 43 | return 0; 44 | } 45 | 46 | person::person(string in_name, int in_salary) 47 | { 48 | name=in_name; 49 | salary=in_salary; 50 | } 51 | 52 | person::person() 53 | {} 54 | 55 | 56 | waiter::waiter(string in_name, int in_salary): 57 | person(in_name, in_salary) 58 | { 59 | customers_served=0; 60 | } 61 | 62 | waiter::waiter() 63 | { 64 | customers_served=0; 65 | } 66 | 67 | void waiter::serve(int customers, barista &b) 68 | { 69 | b.prepare(); 70 | customers+=customers; 71 | } 72 | 73 | 74 | 75 | barista::barista(string in_name, int in_salary): 76 | person(in_name, in_salary) 77 | { 78 | 79 | } 80 | 81 | barista::barista() 82 | {} 83 | 84 | 85 | void barista::prepare() 86 | { 87 | // cout<<.... 88 | } 89 | 90 | 91 | owner::owner(string in_name, int in_salary): 92 | person(in_name, in_salary) 93 | { } 94 | -------------------------------------------------------------------------------- /lesson12/cpp12.askisi2.2.cpp: -------------------------------------------------------------------------------- 1 | /* cpp12.askisi2.2.cpp */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | class person { 10 | public: 11 | person(); 12 | person(string in_name, int in_salary); 13 | private: 14 | string name; 15 | int salary; 16 | }; 17 | 18 | class barista: virtual public person { 19 | public: 20 | barista(); 21 | barista(string in_name, int in_salary); 22 | void prepare(); 23 | private: 24 | }; 25 | 26 | class waiter: virtual public person { 27 | public: 28 | waiter(); 29 | waiter(string in_name, int in_salary); 30 | void serve(int in_customers, barista &b); 31 | void print(); 32 | private: 33 | int customers_served; 34 | }; 35 | 36 | class owner: public barista, public waiter { 37 | public: 38 | owner(string in_name, int in_salary); 39 | private: 40 | 41 | }; 42 | 43 | int main() 44 | { 45 | int clients, waiter_to_serve, barista_to_serve; 46 | owner *o = new owner("Joe", 500); 47 | waiter *w1 = new waiter("Jack", 700); 48 | waiter *w2 = new waiter("Suzy", 1000); 49 | barista *b = new barista("bob", 400); 50 | 51 | waiter *waiters[3]={o,w1,w2}; 52 | barista *baristas[2]={o,b}; 53 | 54 | 55 | 56 | srand(time(NULL)); 57 | for (int i=0; i<100; i++) 58 | { 59 | clients=1+rand()%5; 60 | 61 | waiter_to_serve=rand()%3; 62 | barista_to_serve=rand()%2; 63 | 64 | waiters[waiter_to_serve]->serve(clients, *baristas[barista_to_serve]); 65 | } 66 | 67 | for (int i=0; i<3; i++) 68 | { 69 | waiters[i]->print(); 70 | cout< 4 | #include 5 | using namespace std; 6 | 7 | 8 | class person { 9 | public: 10 | person(string in_name); 11 | protected: 12 | string name; 13 | }; 14 | 15 | class king: public person { 16 | public: 17 | king(string in_name, string in_realm); 18 | void set_realm(string in_realm); 19 | void rule(); 20 | protected: 21 | string realm; 22 | }; 23 | 24 | class philosopher: public person { 25 | public: 26 | philosopher(string in_name, string in_philosophy); 27 | void set_philosophy(string in_philosophy); 28 | void think(); 29 | protected: 30 | string philosophy; 31 | }; 32 | 33 | class phil_king: public king, public philosopher { 34 | public: 35 | phil_king(string in_name, string in_realm, string in_philosophy); 36 | void routine(); 37 | friend ostream &operator<<(ostream &left, phil_king right); 38 | }; 39 | 40 | 41 | int main() 42 | { 43 | phil_king The_King("Marcus Aurelius", "Roman Empire", "stoic"); 44 | 45 | The_King.routine(); 46 | } 47 | 48 | person::person(string in_name) 49 | { 50 | name = in_name; 51 | } 52 | 53 | king::king(string in_name, string in_realm): 54 | person(in_name) 55 | { 56 | realm = in_realm; 57 | } 58 | 59 | void king::set_realm(string in_realm) 60 | { 61 | realm = in_realm; 62 | } 63 | 64 | void king::rule() 65 | { 66 | cout<<"Now, I rule"< 4 | #include 5 | using namespace std; 6 | 7 | class king { 8 | public: 9 | king(string in_realm); 10 | ~king(); 11 | void set_realm(string in_realm); 12 | void rule(); 13 | private: 14 | string realm; 15 | }; 16 | 17 | class philosopher { 18 | public: 19 | philosopher(string in_philosophy); 20 | ~philosopher(); 21 | void set_philosophy(string in_philosophy); 22 | void think(); 23 | private: 24 | string philosophy; 25 | }; 26 | 27 | class phil_king: public king, public philosopher { 28 | public: 29 | phil_king(string in_realm, 30 | string in_philosophy); 31 | ~phil_king(); 32 | void routine(); 33 | }; 34 | 35 | 36 | int main() 37 | { 38 | phil_king marcus_aurelius("Roman Empire", "stoic"); 39 | 40 | marcus_aurelius.routine(); 41 | } 42 | 43 | 44 | king::king(string in_realm) 45 | { 46 | realm = in_realm; 47 | cout<<"Constructing King"< 4 | #include 5 | using namespace std; 6 | 7 | class king { 8 | public: 9 | void set_realm(string in_realm); 10 | void rule(); 11 | private: 12 | string realm; 13 | }; 14 | 15 | class philosopher { 16 | public: 17 | void set_philosophy(string in_philosophy); 18 | void think(); 19 | private: 20 | string philosophy; 21 | }; 22 | 23 | class phil_king: public king, public philosopher { 24 | public: 25 | void routine(); 26 | }; 27 | 28 | 29 | int main() 30 | { 31 | phil_king marcus_aurelius; 32 | 33 | marcus_aurelius.set_realm("Roman Empire"); 34 | marcus_aurelius.set_philosophy("stoic"); 35 | 36 | marcus_aurelius.routine(); 37 | } 38 | 39 | void king::set_realm(string in_realm) 40 | { 41 | realm = in_realm; 42 | } 43 | 44 | void king::rule() 45 | { 46 | cout<<"Now, I rule"< 4 | #include 5 | using namespace std; 6 | 7 | 8 | class person { 9 | public: 10 | person(string in_name); 11 | protected: 12 | string name; 13 | }; 14 | 15 | class king: public person { 16 | public: 17 | king(string in_name, string in_realm); 18 | void set_realm(string in_realm); 19 | void rule(); 20 | protected: 21 | string realm; 22 | }; 23 | 24 | class philosopher: public person { 25 | public: 26 | philosopher(string in_name, string in_philosophy); 27 | void set_philosophy(string in_philosophy); 28 | void think(); 29 | protected: 30 | string philosophy; 31 | }; 32 | 33 | class phil_king: public king, public philosopher { 34 | public: 35 | phil_king(string in_name, string in_realm, string in_philosophy); 36 | void routine(); 37 | friend ostream &operator<<(ostream &left, phil_king right); 38 | }; 39 | 40 | 41 | int main() 42 | { 43 | phil_king The_King("Marcus Aurelius", "Roman Empire", "stoic"); 44 | 45 | The_King.routine(); 46 | } 47 | 48 | person::person(string in_name) 49 | { 50 | name = in_name; 51 | } 52 | 53 | king::king(string in_name, string in_realm): 54 | person(in_name) 55 | { 56 | realm = in_realm; 57 | } 58 | 59 | void king::set_realm(string in_realm) 60 | { 61 | realm = in_realm; 62 | } 63 | 64 | void king::rule() 65 | { 66 | cout<<"Now, I rule"< 4 | #include 5 | using namespace std; 6 | 7 | 8 | class person { 9 | public: 10 | person(); 11 | person(string in_name); 12 | protected: 13 | string name; 14 | }; 15 | 16 | class king: virtual public person { 17 | public: 18 | king(string in_realm); 19 | void set_realm(string in_realm); 20 | void rule(); 21 | protected: 22 | string realm; 23 | }; 24 | 25 | class philosopher: virtual public person { 26 | public: 27 | philosopher(string in_philosophy); 28 | void set_philosophy(string in_philosophy); 29 | void think(); 30 | protected: 31 | string philosophy; 32 | }; 33 | 34 | class phil_king: public king, public philosopher { 35 | public: 36 | phil_king(string in_name, string in_realm, string in_philosophy); 37 | void routine(); 38 | friend ostream &operator<<(ostream &left, phil_king right); 39 | }; 40 | 41 | 42 | int main() 43 | { 44 | phil_king The_King("Marcus Aurelius", "Roman Empire", "stoic"); 45 | 46 | The_King.routine(); 47 | } 48 | 49 | person::person() 50 | {} 51 | 52 | person::person(string in_name) 53 | { 54 | name = in_name; 55 | } 56 | 57 | 58 | king::king(string in_realm) 59 | { 60 | realm = in_realm; 61 | } 62 | 63 | void king::set_realm(string in_realm) 64 | { 65 | realm = in_realm; 66 | } 67 | 68 | void king::rule() 69 | { 70 | cout<<"Now, I rule"< 4 | using namespace std; 5 | 6 | template 7 | class ARRAY { 8 | public: 9 | ARRAY(int in_n); 10 | ARRAY(const ARRAY &ob); 11 | ~ARRAY(); 12 | int get_n() const; 13 | T &operator[] (int i); 14 | ARRAY &operator= (ARRAY &right); 15 | ARRAY &operator+= (int right); 16 | template friend ostream &operator<<(ostream &left, const ARRAY &ob); 17 | private: 18 | T *p; 19 | int n; 20 | }; 21 | 22 | 23 | int main() 24 | { 25 | int n=10; 26 | ARRAY pin(n); 27 | 28 | for (int i=0; i 37 | ARRAY::ARRAY(int in_n) 38 | { 39 | n = in_n; 40 | 41 | p = new T [n]; 42 | if (!p) 43 | cout<<"Error allocating memory!"; 44 | } 45 | 46 | template 47 | ARRAY::ARRAY(const ARRAY &ob) 48 | { 49 | n = ob.get_n(); 50 | 51 | p = new T [n]; 52 | if (!p) cout<<"Memory Allocation Error!"; 53 | 54 | } 55 | 56 | template 57 | ARRAY::~ARRAY() 58 | { 59 | delete [] p; 60 | } 61 | 62 | template 63 | int ARRAY::get_n() const 64 | { 65 | return n; 66 | } 67 | 68 | template 69 | T &ARRAY::operator[] (int i) 70 | { 71 | if (i>=0 && i 79 | ARRAY &ARRAY::operator= (ARRAY &right) 80 | { 81 | if (this==&right) return *this; 82 | 83 | n=right.n; 84 | delete [] p; 85 | p=new T [n]; 86 | if(!p) cout<<"Error Allcoating Memory"; 87 | 88 | for (int i=0; i 95 | ARRAY &ARRAY::operator+= (int right) 96 | { 97 | int newn=n+right; 98 | int *newp; 99 | int i; 100 | 101 | if (right>0) 102 | { 103 | newp = new T [newn]; 104 | if (!newp) cout<<"Error Allocating Memory"; 105 | 106 | for (i=0; i 121 | ostream &operator<<(ostream &left, const ARRAY &ob) 122 | { 123 | int i; 124 | 125 | left<<"["; 126 | for (i=0; i 3 | using namespace std; 4 | 5 | template 6 | class linked_list; 7 | 8 | template 9 | class node 10 | { 11 | public: 12 | friend class linked_list; 13 | private: 14 | T data; 15 | node *next; 16 | }; 17 | 18 | template 19 | class linked_list 20 | { 21 | public: 22 | linked_list(); 23 | ~linked_list(); 24 | bool empty(); 25 | T data(node *p); 26 | bool insert_start(T in_data); 27 | bool insert_after(node *p, T x); 28 | bool delete_start(T *x); 29 | bool delete_after(node *prev, T *x); 30 | void print(); 31 | private: 32 | node *head; 33 | }; 34 | 35 | 36 | int main() 37 | { 38 | linked_list list; 39 | string temp; 40 | 41 | list.insert_start("5.1"); 42 | list.insert_start("4.2"); 43 | list.insert_start("3.3"); 44 | list.delete_start(&temp); 45 | list.print(); 46 | 47 | cout< 53 | linked_list::linked_list() 54 | { 55 | head = NULL; 56 | } 57 | 58 | template 59 | linked_list::~linked_list() 60 | { 61 | node *ptr; 62 | 63 | while (head!=NULL) 64 | { 65 | ptr=head; 66 | head=head->next; 67 | delete ptr; 68 | } 69 | } 70 | 71 | 72 | template 73 | bool linked_list::empty() 74 | { 75 | return head == NULL; 76 | } 77 | 78 | template 79 | T linked_list::data(node *p) 80 | { 81 | return p->data; 82 | } 83 | 84 | template 85 | bool linked_list::insert_start(T x) 86 | { 87 | node *newnode; 88 | 89 | newnode = new node; 90 | if (!newnode) 91 | { 92 | cout<<"Error allocating memory"; 93 | return false; 94 | } 95 | newnode->data=x; 96 | 97 | newnode->next=head; 98 | head=newnode; 99 | return true; 100 | } 101 | 102 | template 103 | bool linked_list::insert_after(node *p, T x) 104 | { 105 | node *newnode; 106 | 107 | newnode=new node; 108 | if (!newnode) 109 | { 110 | cout<<"Adynamia desmeusis mnimis"; 111 | return false; 112 | } 113 | newnode->data=x; 114 | 115 | newnode->next=p->next; 116 | p->next=newnode; 117 | return true; 118 | } 119 | 120 | template 121 | bool linked_list::delete_start(T *x) 122 | { 123 | node *current; 124 | 125 | if (head==NULL) 126 | return false; 127 | 128 | current=head; 129 | *x=current->data; 130 | 131 | head=head->next; 132 | delete current; 133 | 134 | return true; 135 | } 136 | 137 | template 138 | bool linked_list::delete_after(node *prev, T *x) 139 | { 140 | node *current; 141 | 142 | if (prev->next==NULL) 143 | return false; 144 | 145 | current=prev->next; 146 | *x=current->data; 147 | 148 | prev->next=current->next; 149 | delete current; 150 | return true; 151 | } 152 | 153 | 154 | 155 | 156 | template 157 | void linked_list::print() 158 | { 159 | node *current; 160 | 161 | current=head; 162 | while(current!=NULL) 163 | { 164 | cout<data<<" "; 165 | current=current->next; 166 | } 167 | } -------------------------------------------------------------------------------- /lesson13/cpp13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/CPP-programming/3350f77306648649679dd092aad224d875f3e1d3/lesson13/cpp13.pdf -------------------------------------------------------------------------------- /lesson13/cpp13.swap.cpp: -------------------------------------------------------------------------------- 1 | /* cpp13.swap.cpp Template function */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | template 7 | void myswap(T &a, T &b); 8 | 9 | 10 | int main() 11 | { 12 | int a=2, b=3; 13 | 14 | cout<<"a="< st2; 38 | string temp2; 39 | 40 | st2.push("Hello"); 41 | st2.push("World"); 42 | 43 | st2.pop(&temp2); 44 | cout< 53 | stack::stack() 54 | { 55 | top=-1; 56 | } 57 | 58 | template 59 | bool stack::empty() 60 | { 61 | return top==-1; 62 | } 63 | 64 | template 65 | bool stack::full() 66 | { 67 | return top==STACK_SIZE-1; 68 | } 69 | 70 | template 71 | bool stack::push(T elem) 72 | { 73 | if (full()) 74 | return false; 75 | 76 | top++; 77 | array[top]=elem; 78 | return true; 79 | } 80 | 81 | template 82 | bool stack::pop(T *elem) 83 | { 84 | if (empty()) 85 | return false; 86 | 87 | *elem = array[top]; 88 | top--; 89 | return true; 90 | } 91 | -------------------------------------------------------------------------------- /lesson13/cpp13.template_non_type_param.cpp: -------------------------------------------------------------------------------- 1 | /* cpp13.template_non_type_param.cpp Template stack */ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | template 8 | class stack { 9 | public: 10 | stack(); 11 | bool empty(); 12 | bool full(); 13 | bool push(T elem); 14 | bool pop(T *elem); 15 | private: 16 | T array[STACK_SIZE]; 17 | int top; 18 | }; 19 | 20 | int main() 21 | { 22 | /* integer stack */ 23 | stack st; 24 | int temp; 25 | 26 | st.push(2); 27 | st.push(5); 28 | 29 | st.pop(&temp); 30 | cout< st2; 36 | string temp2; 37 | 38 | st2.push("Hello"); 39 | st2.push("World"); 40 | 41 | st2.pop(&temp2); 42 | cout< 51 | stack::stack() 52 | { 53 | top=-1; 54 | } 55 | 56 | template 57 | bool stack::empty() 58 | { 59 | return top==-1; 60 | } 61 | 62 | template 63 | bool stack::full() 64 | { 65 | return top==STACK_SIZE-1; 66 | } 67 | 68 | template 69 | bool stack::push(T elem) 70 | { 71 | if (full()) 72 | return false; 73 | 74 | top++; 75 | array[top]=elem; 76 | return true; 77 | } 78 | 79 | template 80 | bool stack::pop(T *elem) 81 | { 82 | if (empty()) 83 | return false; 84 | 85 | *elem = array[top]; 86 | top--; 87 | return true; 88 | } 89 | -------------------------------------------------------------------------------- /lesson13/cpp13.template_with_friend.cpp: -------------------------------------------------------------------------------- 1 | /* cpp13.template_with_friend.cpp Template stack */ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define STACK_SIZE 100 8 | 9 | 10 | template 11 | class stack { 12 | public: 13 | stack(); 14 | bool empty(); 15 | bool full(); 16 | bool push(T elem); 17 | bool pop(T *elem); 18 | template friend ostream &operator<<(ostream &left, const stack &right); 19 | private: 20 | T array[STACK_SIZE]; 21 | int top; 22 | }; 23 | 24 | int main() 25 | { 26 | /* integer stack */ 27 | stack st; 28 | int temp; 29 | 30 | st.push(1); 31 | cout< 46 | stack::stack() 47 | { 48 | top=-1; 49 | } 50 | 51 | template 52 | bool stack::empty() 53 | { 54 | return top==-1; 55 | } 56 | 57 | template 58 | bool stack::full() 59 | { 60 | return top==STACK_SIZE-1; 61 | } 62 | 63 | template 64 | bool stack::push(T elem) 65 | { 66 | if (full()) 67 | return false; 68 | 69 | top++; 70 | array[top]=elem; 71 | return true; 72 | } 73 | 74 | template 75 | bool stack::pop(T *elem) 76 | { 77 | if (empty()) 78 | return false; 79 | 80 | *elem = array[top]; 81 | top--; 82 | return true; 83 | } 84 | 85 | template 86 | ostream &operator<<(ostream &left, const stack &right) 87 | { 88 | for (int i=0; i<=right.top; i++) 89 | left< 4 | #include 5 | using namespace std; 6 | 7 | #define STACK_SIZE 100 8 | 9 | template 10 | class stack { 11 | public: 12 | static int ob_cnt; 13 | stack(); 14 | bool empty(); 15 | bool full(); 16 | bool push(T elem); 17 | bool pop(T *elem); 18 | private: 19 | T array[STACK_SIZE]; 20 | int top; 21 | }; 22 | 23 | template 24 | int stack::ob_cnt = 0; 25 | 26 | int main() 27 | { 28 | /* integer stack */ 29 | stack st; 30 | stack st2; 31 | stack st3; 32 | 33 | cout<::ob_cnt<::ob_cnt; 35 | 36 | return 0; 37 | } 38 | 39 | 40 | template 41 | stack::stack() 42 | { 43 | top=-1; 44 | ob_cnt++; 45 | } 46 | 47 | template 48 | bool stack::empty() 49 | { 50 | return top==-1; 51 | } 52 | 53 | template 54 | bool stack::full() 55 | { 56 | return top==STACK_SIZE-1; 57 | } 58 | 59 | template 60 | bool stack::push(T elem) 61 | { 62 | if (full()) 63 | return false; 64 | 65 | top++; 66 | array[top]=elem; 67 | return true; 68 | } 69 | 70 | template 71 | bool stack::pop(T *elem) 72 | { 73 | if (empty()) 74 | return false; 75 | 76 | *elem = array[top]; 77 | top--; 78 | return true; 79 | } 80 | --------------------------------------------------------------------------------