├── .gitignore ├── LICENSE ├── README.md ├── REF.md ├── book-ar ├── KodeBook.ist ├── KodeBookAr.cls ├── OOPintro.tex ├── calls.tex ├── cite.bib ├── classObject.tex ├── copyright.tex ├── encapsulation.tex ├── engdnat.bst ├── generics.tex ├── helloworld.tex ├── inheritance.tex ├── lstlangKB.sty ├── makefile ├── oop4coders.tex ├── polymorphism.tex └── preface.tex ├── book-fr ├── KodeBook.ist ├── KodeBookFR.cls ├── OOPintro.tex ├── calls.tex ├── cite.bib ├── classObject.tex ├── copyright.tex ├── encapsulation.tex ├── engdnat.bst ├── generics.tex ├── helloworld.tex ├── inheritance.tex ├── lstlangKB.sty ├── makefile ├── oop4coders.tex ├── polymorphism.tex └── preface.tex ├── book ├── KodeBook.cls ├── KodeBook.ist ├── OOPintro.tex ├── calls.tex ├── cite.bib ├── classObject.tex ├── copyright.tex ├── encapsulation.tex ├── engdnat.bst ├── fonts │ ├── CrimsonText │ │ ├── CrimsonText-Bold.ttf │ │ ├── CrimsonText-BoldItalic.ttf │ │ ├── CrimsonText-Italic.ttf │ │ ├── CrimsonText-Regular.ttf │ │ └── OFL.txt │ ├── Handlee │ │ ├── Handlee-Regular.ttf │ │ └── OFL.txt │ ├── Merienda │ │ ├── Merienda-Bold.ttf │ │ ├── Merienda-Regular.ttf │ │ └── OFL.txt │ ├── SourceCodePro │ │ ├── OFL.txt │ │ └── SourceCodePro-Bold.ttf │ └── decoratedRomanIni │ │ └── DecoratedRomanInitials.ttf ├── generics.tex ├── helloworld.tex ├── inheritance.tex ├── lstlangKB.sty ├── makefile ├── oop4coders.tex ├── polymorphism.tex └── preface.tex ├── codes ├── LICENSE ├── cpp │ ├── clsobj │ │ ├── obj_meth.cpp │ │ └── person0.cpp │ ├── encapsulation │ │ ├── app.cpp │ │ ├── frnd.h │ │ ├── person.cpp │ │ ├── person.h │ │ ├── student.cpp │ │ └── student.h │ ├── hello │ │ ├── entry.cpp │ │ ├── except.cpp │ │ ├── except2.cpp │ │ ├── func.cpp │ │ └── helloworld.cpp │ ├── inheritance │ │ ├── abst │ │ │ ├── app.cpp │ │ │ ├── gradstudent.cpp │ │ │ ├── gradstudent.h │ │ │ ├── person.cpp │ │ │ ├── person.h │ │ │ ├── professor.cpp │ │ │ ├── professor.h │ │ │ ├── student.cpp │ │ │ └── student.h │ │ ├── fin │ │ │ ├── finalcls1.cpp │ │ │ ├── finalcls2.cpp │ │ │ └── finalmth.cpp │ │ ├── multiple │ │ │ ├── diamond1.cpp │ │ │ ├── diamond2.cpp │ │ │ ├── multiple1.cpp │ │ │ └── multiple2.cpp │ │ └── single │ │ │ ├── app.cpp │ │ │ ├── person.cpp │ │ │ ├── person.h │ │ │ ├── professor.cpp │ │ │ ├── professor.h │ │ │ ├── student.cpp │ │ │ └── student.h │ └── polymorphism │ │ ├── overloading.cpp │ │ ├── overriding.cpp │ │ ├── subtype.cpp │ │ └── type_manip.cpp ├── java │ ├── .classpath │ ├── .project │ └── src │ │ ├── clsobj │ │ ├── ObjMeth.java │ │ └── Person.java │ │ ├── encapsulation │ │ ├── core │ │ │ ├── App2.java │ │ │ ├── Person.java │ │ │ └── Student.java │ │ └── main │ │ │ └── App.java │ │ ├── hello │ │ ├── Entry.java │ │ ├── Except.java │ │ ├── Func.java │ │ └── HelloWorld.java │ │ ├── inheritance │ │ ├── abst │ │ │ ├── App.java │ │ │ ├── GradStudent.java │ │ │ ├── Person.java │ │ │ ├── Professor.java │ │ │ └── Student.java │ │ ├── fin │ │ │ ├── FinalCls.java │ │ │ └── FinalMth.java │ │ ├── multiple │ │ │ ├── Diamond1.java │ │ │ ├── Multiple1.java │ │ │ └── Multiple2.java │ │ └── single │ │ │ ├── App.java │ │ │ ├── GradStudent.java │ │ │ ├── Person.java │ │ │ ├── Professor.java │ │ │ └── Student.java │ │ └── polymorphism │ │ ├── Overloading.java │ │ ├── Overriding.java │ │ ├── Subtype.java │ │ └── TypeManip.java ├── javascript │ ├── clsobj │ │ ├── obj_meth.js │ │ ├── person.js │ │ ├── person2.js │ │ ├── person3.js │ │ ├── property.js │ │ └── proto.js │ ├── encapsulation │ │ ├── app.js │ │ ├── decorate.js │ │ ├── person.js │ │ └── student.js │ ├── hello │ │ ├── entry.js │ │ ├── except.js │ │ ├── func.htm │ │ ├── func.js │ │ ├── helloworld.htm │ │ └── helloworld.js │ ├── inheritance │ │ ├── abst │ │ │ ├── es5 │ │ │ │ ├── app.js │ │ │ │ ├── gradstudent.js │ │ │ │ ├── person.js │ │ │ │ ├── professor.js │ │ │ │ └── student.js │ │ │ └── es6 │ │ │ │ ├── app.js │ │ │ │ ├── gradstudent.js │ │ │ │ ├── person.js │ │ │ │ ├── professor.js │ │ │ │ └── student.js │ │ ├── fin │ │ │ ├── es5 │ │ │ │ └── finalcls.js │ │ │ └── es6 │ │ │ │ └── finalcls.js │ │ ├── multiple │ │ │ └── es5 │ │ │ │ ├── diamond1.js │ │ │ │ └── multiple1.js │ │ └── single │ │ │ ├── es5 │ │ │ ├── app.js │ │ │ ├── person.js │ │ │ ├── professor.js │ │ │ └── student.js │ │ │ └── es6 │ │ │ ├── app.js │ │ │ ├── person.js │ │ │ ├── professor.js │ │ │ └── student.js │ └── polymorphism │ │ ├── es5 │ │ ├── overloading.js │ │ ├── overriding.js │ │ ├── subtype.js │ │ └── type_manip.js │ │ └── es6 │ │ ├── overloading.js │ │ ├── overriding.js │ │ ├── subtype.js │ │ └── type_manip.js ├── lua │ ├── clsobj │ │ ├── objmeth.lua │ │ ├── person.lua │ │ ├── person2.lua │ │ └── property.lua │ ├── encapsulation │ │ ├── app.lua │ │ └── person.lua │ ├── hello │ │ ├── entry.lua │ │ ├── except.lua │ │ ├── func.lua │ │ └── helloworld.lua │ ├── inheritance │ │ ├── abst │ │ │ ├── app.lua │ │ │ ├── person.lua │ │ │ ├── professor.lua │ │ │ └── student.lua │ │ ├── multiple │ │ │ ├── diamond.lua │ │ │ ├── multiple1.lua │ │ │ └── multiple2.lua │ │ └── single │ │ │ ├── app.lua │ │ │ ├── const_inherit.lua │ │ │ ├── person.lua │ │ │ ├── professor.lua │ │ │ └── student.lua │ └── polymorphism │ │ ├── overloading.lua │ │ ├── overriding.lua │ │ ├── subtype.lua │ │ └── type_manip.lua ├── perl │ ├── clsobj │ │ ├── objmeth.pl │ │ ├── person.pl │ │ └── property.pl │ ├── encapsulation │ │ ├── Person.pm │ │ ├── Student.pm │ │ ├── app.pl │ │ ├── private.pl │ │ └── public.pl │ ├── hello │ │ ├── entry.pl │ │ ├── except.pl │ │ ├── func.pl │ │ └── helloworld.pl │ ├── inheritance │ │ ├── abst │ │ │ ├── Person.pm │ │ │ ├── Professor.pm │ │ │ ├── Student.pm │ │ │ └── app.pl │ │ ├── fin │ │ │ └── finalcls.pl │ │ ├── multiple │ │ │ ├── diamond.pl │ │ │ ├── multiple1.pl │ │ │ └── multiple2.pl │ │ └── single │ │ │ ├── Person.pm │ │ │ ├── Professor.pm │ │ │ ├── Student.pm │ │ │ └── app.pl │ └── polymorphism │ │ ├── overloading.pl │ │ ├── overriding.pl │ │ ├── subtype.pl │ │ └── type_manip.pl ├── php │ ├── clsobj │ │ ├── objmeth.php │ │ ├── person.php │ │ └── property.php │ ├── encapsulation │ │ ├── app.php │ │ ├── person.php │ │ └── student.php │ ├── hello │ │ ├── entry.php │ │ ├── except.php │ │ ├── func.php │ │ └── helloworld.php │ ├── inheritance │ │ ├── abst │ │ │ ├── app.php │ │ │ ├── gradstudent.php │ │ │ ├── person.php │ │ │ ├── professor.php │ │ │ └── student.php │ │ ├── fin │ │ │ ├── finalcls.php │ │ │ └── finalmth.php │ │ ├── multiple │ │ │ ├── diamond.php │ │ │ ├── multiple1.php │ │ │ └── multiple2.php │ │ └── single │ │ │ ├── app.php │ │ │ ├── gradstudent.php │ │ │ ├── person.php │ │ │ ├── professor.php │ │ │ └── student.php │ └── polymorphism │ │ ├── overloading.php │ │ ├── overriding.php │ │ ├── subtype.php │ │ └── type_manip.php ├── python │ ├── clsobj │ │ ├── objmeth.py │ │ ├── person.py │ │ ├── property.py │ │ └── student.py │ ├── encapsulation │ │ ├── app.py │ │ ├── person.py │ │ └── student.py │ ├── hello │ │ ├── entry.py │ │ ├── except.py │ │ ├── func.py │ │ └── helloworld.py │ ├── inheritance │ │ ├── abst │ │ │ ├── app.py │ │ │ ├── gradstudent.py │ │ │ ├── person.py │ │ │ ├── professor.py │ │ │ └── student.py │ │ ├── fin │ │ │ ├── finalcls.py │ │ │ └── finalmth.py │ │ ├── multiple │ │ │ ├── diamond.py │ │ │ ├── multiple1.py │ │ │ └── multiple2.py │ │ └── single │ │ │ ├── app.py │ │ │ ├── person.py │ │ │ ├── professor.py │ │ │ └── student.py │ └── polymorphism │ │ ├── overloading.py │ │ ├── overriding.py │ │ ├── subtype.py │ │ └── type_manip.py └── ruby │ ├── clsobj │ ├── objmeth.rb │ ├── person.rb │ └── property.rb │ ├── encapsulation │ ├── app.rb │ ├── person.rb │ └── student.rb │ ├── hello │ ├── entry.rb │ ├── except.rb │ ├── except2.rb │ ├── func.rb │ └── helloworld.rb │ ├── inheritance │ ├── abst │ │ ├── app.rb │ │ ├── gradstudent.rb │ │ ├── person.rb │ │ ├── professor.rb │ │ └── student.rb │ ├── fin │ │ └── finalmth.rb │ ├── multiple │ │ ├── multiple1.rb │ │ └── multiple2.rb │ └── single │ │ ├── app.rb │ │ ├── gradstudent.rb │ │ ├── person.rb │ │ ├── professor.rb │ │ └── student.rb │ └── polymorphism │ ├── overloading.rb │ ├── overriding.rb │ ├── subtype.rb │ └── type_manip.rb ├── img ├── C++.pdf ├── Java.pdf ├── Javascript.pdf ├── Lua.pdf ├── PHP.pdf ├── Python.pdf ├── Ruby.pdf ├── book.jpeg ├── by-sa.png ├── by.png ├── cc-by-sa.png ├── cover.jpg └── sa.png └── tools ├── install └── publish.sh /book-ar/KodeBook.ist: -------------------------------------------------------------------------------- 1 | item_0 "\\item {\\color{indigo}\\bfseries " 2 | item_x1 " \\leavevmode\\leaders\\hrule height 6pt\\hfill } \n \\subitem " 3 | 4 | delim_1 " \\dotfill " 5 | delim_2 " \\dotfill " -------------------------------------------------------------------------------- /book-ar/calls.tex: -------------------------------------------------------------------------------- 1 | 2 | \bibliographystyle{engdnat}%unsrtnat, plainnat 3 | 4 | %\usepackage{pgf-umlcd} 5 | 6 | 7 | 8 | 9 | \hypersetup{ 10 | pdfkeywords={OOP; Programming; Coding}, 11 | pdfsubject={Computing; Computer programming} 12 | } 13 | 14 | \renewcommand{\UrlFont}{\ttfamily\footnotesize} 15 | 16 | \DeclareAcronym{oop}{ 17 | short = \textarab{ب. ك. ت.} , 18 | long = Object-oriented programming , 19 | class = abbrev 20 | } 21 | 22 | \DeclareAcronym{oo}{ 23 | short = OO , 24 | long = Object-oriented , 25 | class = abbrev 26 | } 27 | 28 | \DeclareAcronym{ecma}{ 29 | short = ECMA , 30 | long = European Computer Manufacturers Association , 31 | class = abbrev 32 | } 33 | 34 | \DeclareAcronym{cli}{ 35 | short = CLI , 36 | long = Command-Line Interface , 37 | class = abbrev 38 | } 39 | 40 | \DeclareAcronym{cgi}{ 41 | short = CGI , 42 | long = Common Gateway Interface , 43 | class = abbrev 44 | } 45 | 46 | \DeclareAcronym{es6}{ 47 | short = ES6 , 48 | long = ECMAScript 2015 , 49 | class = abbrev 50 | } 51 | 52 | \DeclareAcronym{es5}{ 53 | short = ES5 , 54 | long = ECMAScript 5 , 55 | class = abbrev 56 | } 57 | 58 | \DeclareAcronym{sfinae}{ 59 | short = SFINAE , 60 | long = Substitution Failure Is Not An Error , 61 | class = abbrev 62 | } 63 | 64 | \DeclareAcronym{api}{ 65 | short = API , 66 | long = Application Programming Interface , 67 | class = abbrev 68 | } 69 | 70 | %\makeglossaries 71 | 72 | %\newacronym{oop}{OOP}{Object-oriented programming} -------------------------------------------------------------------------------- /book-ar/generics.tex: -------------------------------------------------------------------------------- 1 | %===================================================================== 2 | \ifx\wholebook\relax\else 3 | \documentclass{KodeBook} 4 | \input{calls} 5 | \begin{document} 6 | \chapter{Generics and OOP} 7 | \fi 8 | %===================================================================== 9 | 10 | 11 | 12 | %===================================================================== 13 | \ifx\wholebook\relax\else 14 | % \cleardoublepage 15 | % \bibliographystyle{../use/ESIbib} 16 | % \bibliography{../bib/RATstat} 17 | \end{document} 18 | \fi 19 | %===================================================================== -------------------------------------------------------------------------------- /book-ar/makefile: -------------------------------------------------------------------------------- 1 | # -*- makefile -*- 2 | 3 | FILE = *.tex 4 | 5 | CMD = xelatex -synctex=1 -interaction=nonstopmode oop4coders.tex 6 | 7 | allmake: $(FILE) 8 | 9 | $(CMD) #run xelatx 10 | bibtex oop4coders.aux 11 | makeindex oop4coders 12 | $(CMD) #run xelatx 13 | 14 | 15 | clean: 16 | rm -f *.synctex.gz *.log *.aux *.blg *.out *.qry *.nav *.snm *.toc *.lof *.lot *.glo *.acn *.up* *.idx *.ilg *.ind 17 | 18 | clear: clean 19 | rm -f *.pdf *.bbl 20 | # clean 21 | -------------------------------------------------------------------------------- /book-ar/oop4coders.tex: -------------------------------------------------------------------------------- 1 | % !TEX TS-program = xelatex 2 | % !TeX program = xelatex 3 | % !TEX encoding = UTF-8 Unicode 4 | % !TEX spellcheck = en-US 5 | \documentclass{KodeBookAr} 6 | 7 | %To prevent other chapters from creating a new document 8 | \let\wholebook=\relax 9 | 10 | %The calls (includes) for other packages are in a separate file 11 | \input{calls} 12 | 13 | 14 | 15 | %opening 16 | %\title{Object-Oriented Programming \\ \hspace{1cm} for \\ \hspace{2cm} Coders} 17 | \title{البرمجة كائنية التوجه للمبرمجين} 18 | %\titleFP{\textarab{البرمجة} \textarab{كائنية} \textarab{التوجه} \textarab{للمبرمجين}} 19 | 20 | \author{\ar{عبد الكريم عريّس}} 21 | \cover{../img/cover.jpg} 22 | \license{../img/cc-by-sa.png} 23 | 24 | %\setplainversion 25 | 26 | \begin{document} 27 | 28 | \maketitle 29 | 30 | \frontmatter 31 | 32 | \addcontentsline{toc}{chapter}{\ar{معلومات عامة}} 33 | 34 | \input{copyright} 35 | 36 | \input{preface} 37 | 38 | \addcontentsline{toc}{chapter}{\ar{المحتويات والقوائم}} 39 | 40 | \kodetoc 41 | 42 | %\kodelof 43 | 44 | \kodelot 45 | 46 | \kodeabbrev 47 | 48 | \mainmatter 49 | 50 | \input{OOPintro} 51 | 52 | \input{helloworld} 53 | 54 | \input{classObject} 55 | 56 | \input{encapsulation} 57 | 58 | \input{inheritance} 59 | 60 | \input{polymorphism} 61 | 62 | %\chapter{Generics and OOP} 63 | %\input{generics} 64 | 65 | \backmatter 66 | 67 | \bibliography{cite} 68 | \addcontentsline{toc}{chapter}{Bibliography} 69 | 70 | \printindex 71 | \addcontentsline{toc}{chapter}{Index} 72 | 73 | \cleardoublepage 74 | \pagestyle{empty} 75 | \vspace*{\fill} 76 | \Huge\color{indigo} 77 | \begin{center} 78 | That's all\\ ...\\ The END 79 | \end{center} 80 | \vspace*{\fill} 81 | 82 | \end{document} 83 | -------------------------------------------------------------------------------- /book-fr/KodeBook.ist: -------------------------------------------------------------------------------- 1 | item_0 "\\item {\\color{indigo}\\bfseries " 2 | item_x1 " \\leavevmode\\leaders\\hrule height 6pt\\hfill } \n \\subitem " 3 | 4 | delim_1 " \\dotfill " 5 | delim_2 " \\dotfill " -------------------------------------------------------------------------------- /book-fr/calls.tex: -------------------------------------------------------------------------------- 1 | \usepackage{amsmath,amssymb} % AMS Math 2 | \usepackage[utf8]{inputenc} 3 | %\usepackage[T1]{fontenc} 4 | 5 | %\usepackage[pdftex]{graphicx} 6 | 7 | %\usepackage{listingsutf8} 8 | %\usepackage{xcolor} 9 | %\usepackage{times} 10 | \usepackage{array} 11 | \usepackage{natbib} 12 | \usepackage{lscape}%to flip tables in a page 13 | \usepackage{pdflscape} 14 | \usepackage{longtable} 15 | \usepackage{tabu} 16 | \usepackage{wrapfig} 17 | 18 | %\usepackage[english]{babel} 19 | 20 | \bibliographystyle{engdnat}%unsrtnat, plainnat 21 | 22 | %\usepackage{pgf-umlcd} 23 | 24 | 25 | 26 | 27 | \hypersetup{ 28 | pdfkeywords={OOP; Programming; Coding}, 29 | pdfsubject={Computing; Computer programming} 30 | } 31 | 32 | \renewcommand{\UrlFont}{\ttfamily\footnotesize} 33 | 34 | \DeclareAcronym{oop}{ 35 | short = OOP , 36 | long = Object-oriented programming , 37 | class = abbrev 38 | } 39 | 40 | \DeclareAcronym{oo}{ 41 | short = OO , 42 | long = Object-oriented , 43 | class = abbrev 44 | } 45 | 46 | \DeclareAcronym{ecma}{ 47 | short = ECMA , 48 | long = European Computer Manufacturers Association , 49 | class = abbrev 50 | } 51 | 52 | \DeclareAcronym{cli}{ 53 | short = CLI , 54 | long = Command-Line Interface , 55 | class = abbrev 56 | } 57 | 58 | \DeclareAcronym{cgi}{ 59 | short = CGI , 60 | long = Common Gateway Interface , 61 | class = abbrev 62 | } 63 | 64 | \DeclareAcronym{es6}{ 65 | short = ES6 , 66 | long = ECMAScript 2015 , 67 | class = abbrev 68 | } 69 | 70 | \DeclareAcronym{es5}{ 71 | short = ES5 , 72 | long = ECMAScript 5 , 73 | class = abbrev 74 | } 75 | 76 | \DeclareAcronym{sfinae}{ 77 | short = SFINAE , 78 | long = Substitution Failure Is Not An Error , 79 | class = abbrev 80 | } 81 | 82 | \DeclareAcronym{api}{ 83 | short = API , 84 | long = Application Programming Interface , 85 | class = abbrev 86 | } 87 | 88 | %\makeglossaries 89 | 90 | %\newacronym{oop}{OOP}{Object-oriented programming} -------------------------------------------------------------------------------- /book-fr/generics.tex: -------------------------------------------------------------------------------- 1 | %===================================================================== 2 | \ifx\wholebook\relax\else 3 | \documentclass{KodeBook} 4 | \input{calls} 5 | \begin{document} 6 | \chapter{Generics and OOP} 7 | \fi 8 | %===================================================================== 9 | 10 | 11 | 12 | %===================================================================== 13 | \ifx\wholebook\relax\else 14 | % \cleardoublepage 15 | % \bibliographystyle{../use/ESIbib} 16 | % \bibliography{../bib/RATstat} 17 | \end{document} 18 | \fi 19 | %===================================================================== -------------------------------------------------------------------------------- /book-fr/makefile: -------------------------------------------------------------------------------- 1 | # -*- makefile -*- 2 | 3 | FILE = *.tex 4 | 5 | CMD = xelatex -synctex=1 -interaction=nonstopmode oop4coders.tex 6 | 7 | allmake: $(FILE) 8 | 9 | $(CMD) #run xelatx 10 | bibtex oop4coders.aux 11 | makeindex oop4coders 12 | $(CMD) #run xelatx 13 | 14 | 15 | clean: 16 | rm -f *.synctex.gz *.log *.aux *.blg *.out *.qry *.nav *.snm *.toc *.lof *.lot *.glo *.acn *.up* *.idx *.ilg *.ind 17 | 18 | clear: clean 19 | rm -f *.pdf *.bbl 20 | # clean 21 | -------------------------------------------------------------------------------- /book-fr/oop4coders.tex: -------------------------------------------------------------------------------- 1 | % !TEX TS-program = xelatex 2 | % !TeX program = xelatex 3 | % !TEX encoding = UTF-8 Unicode 4 | % !TEX spellcheck = fr 5 | 6 | \documentclass{KodeBookFR} 7 | 8 | %To prevent other chapters from creating a new document 9 | \let\wholebook=\relax 10 | 11 | %The calls (includes) for other packages are in a separate file 12 | \input{calls} 13 | 14 | 15 | %opening 16 | %\title{Object-Oriented Programming \\ \hspace{1cm} for \\ \hspace{2cm} Coders} 17 | \title{La programmation orientée-objet pour les programmeurs} 18 | 19 | \author{ARIES Abdelkrime} 20 | \cover{../img/cover.jpg} 21 | \license{../img/cc-by-sa.png} 22 | 23 | %\setplainversion 24 | 25 | \begin{document} 26 | 27 | \maketitle 28 | 29 | \frontmatter 30 | 31 | \addcontentsline{toc}{chapter}{General information} 32 | 33 | \input{copyright} 34 | 35 | \input{preface} 36 | 37 | \addcontentsline{toc}{chapter}{Contents and Lists} 38 | 39 | \kodetoc 40 | 41 | %\kodelof 42 | 43 | \kodelot 44 | 45 | \kodeabbrev 46 | 47 | \mainmatter 48 | 49 | \input{OOPintro} 50 | 51 | \input{helloworld} 52 | 53 | \input{classObject} 54 | 55 | \input{encapsulation} 56 | 57 | \input{inheritance} 58 | 59 | \input{polymorphism} 60 | 61 | %\chapter{Generics and OOP} 62 | %\input{generics} 63 | 64 | \backmatter 65 | 66 | \bibliography{cite} 67 | \addcontentsline{toc}{chapter}{Bibliography} 68 | 69 | \printindex 70 | \addcontentsline{toc}{chapter}{Index} 71 | 72 | \cleardoublepage 73 | \pagestyle{empty} 74 | \vspace*{\fill} 75 | \Huge\color{indigo} 76 | \begin{center} 77 | That's all\\ ...\\ The END 78 | \end{center} 79 | \vspace*{\fill} 80 | 81 | \end{document} 82 | -------------------------------------------------------------------------------- /book/KodeBook.ist: -------------------------------------------------------------------------------- 1 | item_0 "\\item {\\color{indigo}\\bfseries " 2 | item_x1 " \\leavevmode\\leaders\\hrule height 6pt\\hfill } \n \\subitem " 3 | 4 | delim_1 " \\dotfill " 5 | delim_2 " \\dotfill " -------------------------------------------------------------------------------- /book/calls.tex: -------------------------------------------------------------------------------- 1 | \usepackage{amsmath,amssymb} % AMS Math 2 | \usepackage[utf8]{inputenc} 3 | %\usepackage[T1]{fontenc} 4 | 5 | %\usepackage[pdftex]{graphicx} 6 | 7 | %\usepackage{listingsutf8} 8 | %\usepackage{xcolor} 9 | %\usepackage{times} 10 | \usepackage{array} 11 | \usepackage{natbib} 12 | \usepackage{lscape}%to flip tables in a page 13 | \usepackage{pdflscape} 14 | \usepackage{longtable} 15 | \usepackage{tabu} 16 | \usepackage{wrapfig} 17 | 18 | %\usepackage[english]{babel} 19 | 20 | \bibliographystyle{engdnat}%unsrtnat, plainnat 21 | 22 | %\usepackage{pgf-umlcd} 23 | 24 | 25 | 26 | 27 | \hypersetup{ 28 | pdfkeywords={OOP; Programming; Coding}, 29 | pdfsubject={Computing; Computer programming} 30 | } 31 | 32 | \renewcommand{\UrlFont}{\ttfamily\footnotesize} 33 | 34 | \DeclareAcronym{oop}{ 35 | short = OOP , 36 | long = Object-oriented programming , 37 | class = abbrev 38 | } 39 | 40 | \DeclareAcronym{oo}{ 41 | short = OO , 42 | long = Object-oriented , 43 | class = abbrev 44 | } 45 | 46 | \DeclareAcronym{ecma}{ 47 | short = ECMA , 48 | long = European Computer Manufacturers Association , 49 | class = abbrev 50 | } 51 | 52 | \DeclareAcronym{cli}{ 53 | short = CLI , 54 | long = Command-Line Interface , 55 | class = abbrev 56 | } 57 | 58 | \DeclareAcronym{cgi}{ 59 | short = CGI , 60 | long = Common Gateway Interface , 61 | class = abbrev 62 | } 63 | 64 | \DeclareAcronym{es6}{ 65 | short = ES6 , 66 | long = ECMAScript 2015 , 67 | class = abbrev 68 | } 69 | 70 | \DeclareAcronym{es5}{ 71 | short = ES5 , 72 | long = ECMAScript 5 , 73 | class = abbrev 74 | } 75 | 76 | \DeclareAcronym{sfinae}{ 77 | short = SFINAE , 78 | long = Substitution Failure Is Not An Error , 79 | class = abbrev 80 | } 81 | 82 | \DeclareAcronym{api}{ 83 | short = API , 84 | long = Application Programming Interface , 85 | class = abbrev 86 | } 87 | 88 | %\makeglossaries 89 | 90 | %\newacronym{oop}{OOP}{Object-oriented programming} -------------------------------------------------------------------------------- /book/fonts/CrimsonText/CrimsonText-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/book/fonts/CrimsonText/CrimsonText-Bold.ttf -------------------------------------------------------------------------------- /book/fonts/CrimsonText/CrimsonText-BoldItalic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/book/fonts/CrimsonText/CrimsonText-BoldItalic.ttf -------------------------------------------------------------------------------- /book/fonts/CrimsonText/CrimsonText-Italic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/book/fonts/CrimsonText/CrimsonText-Italic.ttf -------------------------------------------------------------------------------- /book/fonts/CrimsonText/CrimsonText-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/book/fonts/CrimsonText/CrimsonText-Regular.ttf -------------------------------------------------------------------------------- /book/fonts/Handlee/Handlee-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/book/fonts/Handlee/Handlee-Regular.ttf -------------------------------------------------------------------------------- /book/fonts/Merienda/Merienda-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/book/fonts/Merienda/Merienda-Bold.ttf -------------------------------------------------------------------------------- /book/fonts/Merienda/Merienda-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/book/fonts/Merienda/Merienda-Regular.ttf -------------------------------------------------------------------------------- /book/fonts/SourceCodePro/SourceCodePro-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/book/fonts/SourceCodePro/SourceCodePro-Bold.ttf -------------------------------------------------------------------------------- /book/fonts/decoratedRomanIni/DecoratedRomanInitials.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/book/fonts/decoratedRomanIni/DecoratedRomanInitials.ttf -------------------------------------------------------------------------------- /book/generics.tex: -------------------------------------------------------------------------------- 1 | %===================================================================== 2 | \ifx\wholebook\relax\else 3 | \documentclass{KodeBook} 4 | \input{calls} 5 | \begin{document} 6 | \chapter{Generics and OOP} 7 | \fi 8 | %===================================================================== 9 | 10 | 11 | 12 | %===================================================================== 13 | \ifx\wholebook\relax\else 14 | % \cleardoublepage 15 | % \bibliographystyle{../use/ESIbib} 16 | % \bibliography{../bib/RATstat} 17 | \end{document} 18 | \fi 19 | %===================================================================== -------------------------------------------------------------------------------- /book/makefile: -------------------------------------------------------------------------------- 1 | # -*- makefile -*- 2 | 3 | FILE = *.tex 4 | 5 | CMD = xelatex -synctex=1 -interaction=nonstopmode oop4coders.tex 6 | 7 | allmake: $(FILE) 8 | 9 | $(CMD) #run xelatx 10 | bibtex oop4coders.aux 11 | makeindex oop4coders 12 | $(CMD) #run xelatx 13 | 14 | 15 | clean: 16 | rm -f *.synctex.gz *.log *.aux *.blg *.out *.qry *.nav *.snm *.toc *.lof *.lot *.glo *.acn *.up* *.idx *.ilg *.ind 17 | 18 | clear: clean 19 | rm -f *.pdf *.bbl 20 | # clean 21 | -------------------------------------------------------------------------------- /book/oop4coders.tex: -------------------------------------------------------------------------------- 1 | \documentclass{KodeBook} 2 | 3 | %To prevent other chapters from creating a new document 4 | \let\wholebook=\relax 5 | 6 | %The calls (includes) for other packages are in a separate file 7 | \input{calls} 8 | 9 | 10 | %opening 11 | %\title{Object-Oriented Programming \\ \hspace{1cm} for \\ \hspace{2cm} Coders} 12 | \title{Object-Oriented Programming for Coders} 13 | 14 | \author{Abdelkrime Aries} 15 | \cover{../img/cover.jpg} 16 | \license{../img/cc-by-sa.png} 17 | 18 | %\setplainversion 19 | 20 | \begin{document} 21 | 22 | \maketitle 23 | 24 | \frontmatter 25 | 26 | \addcontentsline{toc}{chapter}{General information} 27 | 28 | \input{copyright} 29 | 30 | \input{preface} 31 | 32 | \addcontentsline{toc}{chapter}{Contents and Lists} 33 | 34 | \kodetoc 35 | 36 | %\kodelof 37 | 38 | \kodelot 39 | 40 | \kodeabbrev 41 | 42 | \mainmatter 43 | 44 | \input{OOPintro} 45 | 46 | \input{helloworld} 47 | 48 | \input{classObject} 49 | 50 | \input{encapsulation} 51 | 52 | \input{inheritance} 53 | 54 | \input{polymorphism} 55 | 56 | %\chapter{Generics and OOP} 57 | %\input{generics} 58 | 59 | \backmatter 60 | 61 | \bibliography{cite} 62 | \addcontentsline{toc}{chapter}{Bibliography} 63 | 64 | \printindex 65 | \addcontentsline{toc}{chapter}{Index} 66 | 67 | \cleardoublepage 68 | \pagestyle{empty} 69 | \vspace*{\fill} 70 | \Huge\color{indigo} 71 | \begin{center} 72 | That's all\\ ...\\ The END 73 | \end{center} 74 | \vspace*{\fill} 75 | 76 | \end{document} 77 | -------------------------------------------------------------------------------- /codes/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 20016 Abdelkrime Aries 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /codes/cpp/clsobj/obj_meth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Person 5 | { 6 | 7 | public: 8 | 9 | Person(std::string name, int byear){ 10 | this->name = name; 11 | this->byear = byear; 12 | }; 13 | 14 | Person clone() { 15 | return Person(name, byear + 1); 16 | }; 17 | 18 | private: 19 | friend std::ostream& operator<<(std::ostream &strm, const Person &p); 20 | friend bool operator==(const Person &p1, const Person &p2); 21 | friend bool operator>(const Person &p1, const Person &p2); 22 | std::string name; 23 | int byear; 24 | }; 25 | 26 | std::ostream& operator<<(std::ostream &strm, const Person &p) { 27 | return strm << "(" << p.name << ": " << p.byear << ")"; 28 | } 29 | 30 | bool operator==(const Person &p1, const Person &p2) { 31 | return p1.name == p2.name; 32 | } 33 | 34 | bool operator>(const Person &p1, const Person &p2) { 35 | if (p1.byear == p2.byear) return (p1.name.compare(p2.name) > 0); 36 | return (p1.byear > p2.byear); 37 | } 38 | 39 | int main() 40 | { 41 | Person p = Person("Karim", 1986); 42 | Person p2 = Person("Karim+1", 1986); 43 | 44 | std::cout << "As string: " << p << "\n"; 45 | 46 | Person p3 = p.clone(); 47 | std::cout << "Copy: " << p3 << "\n"; 48 | std::cout << "p == p3? " << (p == p3) << "\n"; 49 | std::cout << "p > p3? " << (p > p3) << "\n"; 50 | 51 | } 52 | -------------------------------------------------------------------------------- /codes/cpp/clsobj/person0.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Person 5 | { 6 | 7 | public: 8 | 9 | Person(std::string name, int byear){ 10 | this->name = name; 11 | this->byear = byear; 12 | nbr++; 13 | }; 14 | 15 | ~Person(){ 16 | nbr--; 17 | std::cout << name << " is out\n"; 18 | }; 19 | 20 | void info() { 21 | std::cout << "My name: " << name ; 22 | std::cout << ", My birth year: " << this->byear << "\n"; 23 | }; 24 | 25 | static int population() { 26 | return nbr; 27 | }; 28 | 29 | private: 30 | 31 | std::string name; 32 | int byear; 33 | static int nbr; 34 | }; 35 | 36 | int Person::nbr = 0; 37 | 38 | int main() 39 | { 40 | Person p = Person("Karim", 1986); 41 | Person* p2 = new Person("Karim+1", 1987); 42 | 43 | p.info(); 44 | p2->info(); 45 | int nbr = Person::population(); 46 | 47 | std::cout << "The number of persons: " << nbr << "\n"; 48 | delete p2; //delete the pointer 49 | nbr = Person::population(); 50 | std::cout << "The number of persons: " << nbr << "\n"; 51 | } 52 | -------------------------------------------------------------------------------- /codes/cpp/encapsulation/app.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "student.h" 3 | #include "frnd.h" 4 | 5 | void info_fct(Person p) { 6 | std::cout << "Friend function- name: " << p.name << "\n"; 7 | std::cout << "Friend function- job: " << p.t << "\n"; 8 | } 9 | 10 | int main() 11 | { 12 | Person p = Person("Karim_p"); 13 | Student s = Student("Karim_s"); 14 | p.luckyNumber = 5; 15 | //p.num = 6;//error: private 16 | //p.t = "admin"; //error: protected 17 | p.info(); 18 | s.info(); 19 | 20 | Frnd::info(p); 21 | info_fct(p); 22 | 23 | p.copy(s); 24 | 25 | p.info(); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /codes/cpp/encapsulation/frnd.h: -------------------------------------------------------------------------------- 1 | #ifndef FRND_H 2 | #define FRND_H 3 | 4 | #include 5 | #include "person.h" 6 | 7 | class Frnd 8 | { 9 | public: 10 | static void info(Person p) { 11 | std::cout << "Friend class- name: " << p.name << "\n"; 12 | std::cout << "Friend class- job: " << p.t << "\n"; 13 | } 14 | }; 15 | #endif /* FRND_H */ 16 | -------------------------------------------------------------------------------- /codes/cpp/encapsulation/person.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include 3 | 4 | int Person::nbr = 0; 5 | 6 | Person::Person(std::string name) 7 | { 8 | this->name = name; 9 | num = (Person::nbr++); 10 | t = "person"; 11 | luckyNumber = 0; 12 | } 13 | 14 | void Person::info() 15 | { 16 | std::cout << "My name: " << name << "\n"; 17 | std::cout << "My number: " << num << "\n"; 18 | std::cout << "I am a: " << t << "\n"; 19 | std::cout << "My lucky number is: " << luckyNumber << "\n"; 20 | std::cout << "--------------------------\n"; 21 | } 22 | 23 | void Person::copy(Person other) 24 | { 25 | name = other.name; 26 | t = other.t; 27 | } 28 | -------------------------------------------------------------------------------- /codes/cpp/encapsulation/person.h: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_H 2 | #define PERSON_H 3 | 4 | #include 5 | 6 | class Person 7 | { 8 | 9 | friend class Frnd; 10 | friend void info_fct(Person p); 11 | 12 | public: 13 | int luckyNumber; 14 | Person(std::string name); 15 | void info(); 16 | void copy(Person other); 17 | 18 | protected: 19 | std::string t; 20 | 21 | private: 22 | std::string name; 23 | int num; 24 | static int nbr; 25 | }; 26 | #endif /* PERSON_H */ 27 | -------------------------------------------------------------------------------- /codes/cpp/encapsulation/student.cpp: -------------------------------------------------------------------------------- 1 | #include "student.h" 2 | 3 | Student::Student(std::string name): Person(name) 4 | { 5 | this->t = "student";//protected member 6 | this->luckyNumber = 1;//public member 7 | //this->name = "other name";//private member: error 8 | } 9 | -------------------------------------------------------------------------------- /codes/cpp/encapsulation/student.h: -------------------------------------------------------------------------------- 1 | #ifndef STUDENT_H 2 | #define STUDENT_H 3 | //You don't need to include string, it is included in person.h 4 | #include "person.h" 5 | 6 | class Student: public Person 7 | { 8 | public: 9 | Student(std::string name); 10 | }; 11 | 12 | #endif /* STUDENT_H */ 13 | -------------------------------------------------------------------------------- /codes/cpp/hello/entry.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int fact(int i); 5 | 6 | using namespace std; 7 | int main(int argc, char *argv[]) 8 | { 9 | 10 | if (argc < 2) { 11 | cout << "Please enter an integer value\n"; 12 | return 1; 13 | } 14 | 15 | int n = atoi(argv[1]); 16 | cout << "Fact(" << n << ")= " << fact(n) << "\n" ; 17 | } 18 | 19 | int fact(int i){ 20 | if (i <=0) return 1; 21 | return i * fact(i-1); 22 | } 23 | -------------------------------------------------------------------------------- /codes/cpp/hello/except.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int fact(int i){ 5 | if (i < 0) throw "Negative numbers don't have a factorial!"; 6 | if (i > 5) throw "The number is too big!"; 7 | 8 | if (i ==0) return 1; 9 | return i * fact(i-1); 10 | } 11 | 12 | using namespace std; 13 | int main(int argc, char *argv[]) 14 | { 15 | 16 | if (argc < 2) { 17 | cout << "Please enter an integer value\n"; 18 | exit(0); 19 | } 20 | 21 | int n = atoi(argv[1]); 22 | 23 | try { 24 | int f = fact(n); 25 | cout << "Fact(" << n << ")= " << f << "\n" ; 26 | } 27 | catch (const char* e) { 28 | cout << e << "\n"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /codes/cpp/hello/except2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | struct NegException : public exception { 7 | const char * what () const throw () { 8 | return "Negative numbers don't have a factorial!"; 9 | } 10 | }; 11 | 12 | struct BigException : public exception { 13 | const char * what () const throw () { 14 | return "The number is too big!"; 15 | } 16 | }; 17 | 18 | int fact(int i){ 19 | if (i < 0) throw NegException(); 20 | if (i > 5) throw BigException(); 21 | 22 | if (i ==1) return 1; 23 | return i * fact(i-1); 24 | } 25 | 26 | int main(int argc, char *argv[]) 27 | { 28 | if (argc < 2) { 29 | cout << "Please enter an integer value\n"; 30 | exit(0); 31 | } 32 | 33 | int n = atoi(argv[1]); 34 | 35 | try { 36 | int f = fact(n); 37 | cout << "Fact(" << n << ")= " << f << "\n" ; 38 | } 39 | catch (exception& e) { 40 | cout << e.what() << "\n"; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /codes/cpp/hello/func.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int fact(int i); 3 | int main() 4 | { 5 | int n; 6 | std::cout << "Please enter an integer value: "; 7 | std::cin >> n; 8 | std::cout << "Fact(" << n << ")= "; 9 | std::cout << fact(n) << "\n"; 10 | } 11 | 12 | int fact(int i){ 13 | if (i <=1) return 1; 14 | return i * fact(i-1); 15 | } 16 | -------------------------------------------------------------------------------- /codes/cpp/hello/helloworld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | std::cout << "Hello, world!\n"; 6 | } 7 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/abst/app.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "student.h" 3 | #include "gradstudent.h" 4 | #include "professor.h" 5 | 6 | int main() 7 | { 8 | //Person pe = Person();//is abstract; cannot be instantiated 9 | //Student st = Student();//is abstract; cannot be instantiated 10 | Professor pr = Professor(); 11 | GradStudent gs = GradStudent(); 12 | 13 | pr.info(); 14 | gs.info(); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/abst/gradstudent.cpp: -------------------------------------------------------------------------------- 1 | #include "gradstudent.h" 2 | #include 3 | 4 | void GradStudent::info2() 5 | { 6 | std::cout << "GradStudent info2" << "\n"; 7 | } 8 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/abst/gradstudent.h: -------------------------------------------------------------------------------- 1 | #ifndef GSTUDENT_H 2 | #define GSTUDENT_H 3 | #include "student.h" 4 | 5 | class GradStudent: public Student 6 | { 7 | public: 8 | void info2(); 9 | }; 10 | #endif /* GSTUDENT_H */ 11 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/abst/person.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | 3 | #include 4 | 5 | void Person::info() 6 | { 7 | std::cout << "====INFO====" << "\n"; 8 | info2(); 9 | } 10 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/abst/person.h: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_H 2 | #define PERSON_H 3 | 4 | class Person 5 | { 6 | public: 7 | void info(); 8 | virtual void info2() = 0; 9 | }; 10 | #endif /* PERSON_H */ 11 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/abst/professor.cpp: -------------------------------------------------------------------------------- 1 | #include "professor.h" 2 | #include 3 | 4 | void Professor::info2() 5 | { 6 | std::cout << "Professor info2" << "\n"; 7 | } 8 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/abst/professor.h: -------------------------------------------------------------------------------- 1 | #ifndef PROF_H 2 | #define PROF_H 3 | #include "person.h" 4 | 5 | class Professor: public Person 6 | { 7 | public: 8 | void info2(); 9 | }; 10 | #endif /* PROF_H */ 11 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/abst/student.cpp: -------------------------------------------------------------------------------- 1 | #include "student.h" 2 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/abst/student.h: -------------------------------------------------------------------------------- 1 | #ifndef STUDENT_H 2 | #define STUDENT_H 3 | #include "person.h" 4 | 5 | class Student: public Person 6 | { 7 | }; 8 | #endif /* STUDENT_H */ 9 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/fin/finalcls1.cpp: -------------------------------------------------------------------------------- 1 | //Final indicator 2 | template 3 | class MakeFinal 4 | { 5 | private: 6 | MakeFinal() {} 7 | friend T; 8 | }; 9 | 10 | //Final class 11 | class Person: virtual MakeFinal {}; 12 | 13 | class Student: public Person {}; 14 | 15 | int main() 16 | { 17 | Person p = Person(); 18 | //Student s = Student();//can't instantiate 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/fin/finalcls2.cpp: -------------------------------------------------------------------------------- 1 | class Person final {}; 2 | 3 | //class Student: public Person {}; //cannot derive 4 | 5 | int main() 6 | { 7 | Person p = Person(); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/fin/finalmth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Person { 4 | public: 5 | void virtual finalMethod() final { 6 | std::cout << "Person final method\n"; 7 | }; 8 | void method() { 9 | std::cout << "Person method\n"; 10 | }; 11 | void virtual virtualMethod() { 12 | std::cout << "Person virtual method\n"; 13 | }; 14 | }; 15 | 16 | class Student: public Person { 17 | public: 18 | //void virtual finalMethod() {};//cannot override 19 | void method() { 20 | std::cout << "Student method\n"; 21 | }; 22 | void virtualMethod() { 23 | std::cout << "Student virtual method\n"; 24 | }; 25 | }; 26 | 27 | int main() 28 | { 29 | Person* p = new Person(); 30 | Student* s = new Student(); 31 | Person* p2 = s; 32 | 33 | p->finalMethod(); 34 | s->finalMethod(); 35 | p2->finalMethod(); 36 | 37 | p->method(); 38 | s->method(); 39 | p2->method(); 40 | 41 | p->virtualMethod(); 42 | s->virtualMethod(); 43 | p2->virtualMethod(); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/multiple/diamond1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | class Person { 6 | public: 7 | Person(std::string name){ 8 | this->name = name; 9 | std::cout << "Person" << std::endl; 10 | } 11 | protected: 12 | std::string name; 13 | }; 14 | 15 | class Student: public Person { 16 | public: 17 | Student(std::string name, double mark): Person(name){ 18 | this->mark = mark; 19 | std::cout << "Student" << std::endl; 20 | } 21 | protected: 22 | double mark; 23 | }; 24 | 25 | class Professor: public Person { 26 | public: 27 | Professor(std::string name, int hours): Person(name){ 28 | this->hours = hours; 29 | std::cout << "Professor" << std::endl; 30 | } 31 | protected: 32 | int hours; 33 | }; 34 | 35 | class PhdStudent: public Student, public Professor { 36 | public: 37 | PhdStudent(std::string name, int hours, double mark): 38 | Student(name, mark), Professor(name, hours){ 39 | this->hours = hours; 40 | std::cout << "PhdStudent" << std::endl; 41 | } 42 | 43 | void info() { 44 | //reference to 'name' is ambiguous 45 | //std::cout << "name: " << name << std::endl; 46 | std::cout << "student name: " << Student::name << std::endl; 47 | std::cout << "professor name: " << Professor::name << std::endl; 48 | } 49 | }; 50 | 51 | 52 | int main() 53 | { 54 | PhdStudent ps = PhdStudent("Karim", 8, 12.5); 55 | ps.info(); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/multiple/diamond2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | class Person { 6 | public: 7 | Person(std::string name){ 8 | this->name = name; 9 | std::cout << "Person" << std::endl; 10 | } 11 | protected: 12 | std::string name; 13 | }; 14 | 15 | class Student: virtual public Person { 16 | public: 17 | Student(std::string name, double mark): Person(name){ 18 | this->mark = mark; 19 | std::cout << "Student" << std::endl; 20 | } 21 | protected: 22 | double mark; 23 | }; 24 | 25 | class Professor: virtual public Person { 26 | public: 27 | Professor(std::string name, int hours): Person(name){ 28 | this->hours = hours; 29 | std::cout << "Professor" << std::endl; 30 | } 31 | protected: 32 | int hours; 33 | }; 34 | 35 | class PhdStudent: public Student, public Professor { 36 | public: 37 | PhdStudent(std::string name, int hours, double mark): 38 | Student(name, mark), Professor(name, hours), Person(name){ 39 | this->hours = hours; 40 | std::cout << "PhdStudent" << std::endl; 41 | } 42 | 43 | void info() { 44 | std::cout << "name: " << name << std::endl; 45 | } 46 | }; 47 | 48 | 49 | int main() 50 | { 51 | PhdStudent ps = PhdStudent("Karim", 8, 12.5); 52 | ps.info(); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/multiple/multiple1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Person { 5 | public: 6 | Person(){ 7 | std::cout << "I am a person" << std::endl; 8 | } 9 | }; 10 | 11 | class Machine { 12 | public: 13 | Machine(){ 14 | std::cout << "I am a machine" << std::endl; 15 | } 16 | }; 17 | 18 | class Android: public Machine, public Person { 19 | public: 20 | Android(){ 21 | std::cout << "So, I am an android" << std::endl; 22 | } 23 | }; 24 | 25 | class Cyborg: public Person, public Machine { 26 | public: 27 | Cyborg(): Machine(), Person(){ 28 | std::cout << "So, I am a cyborg" << std::endl; 29 | } 30 | }; 31 | 32 | 33 | int main() 34 | { 35 | Person p; 36 | std::cout << "=====================" << std::endl; 37 | Machine m; 38 | std::cout << "=====================" << std::endl; 39 | Android a; 40 | std::cout << "=====================" << std::endl; 41 | Cyborg c; 42 | std::cout << "=====================" << std::endl; 43 | } 44 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/multiple/multiple2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Person { 5 | public: 6 | Person(std::string name){ 7 | this->name = name; 8 | serial = 5; 9 | }; 10 | void info() { 11 | std::cout << "Person name: " << name << std::endl; 12 | std::cout << "Person serial: " << serial << std::endl; 13 | } 14 | protected: 15 | std::string name; 16 | int serial; 17 | }; 18 | 19 | class Machine { 20 | public: 21 | Machine(int serial){ 22 | this->serial = serial; 23 | } 24 | void info() { 25 | std::cout << "Machine serial: " << serial << std::endl; 26 | } 27 | protected: 28 | int serial; 29 | }; 30 | 31 | class Android: public Machine, public Person { 32 | public: 33 | Android(std::string name, int serial): Machine(serial), Person(name){ 34 | } 35 | }; 36 | 37 | class Cyborg: public Person, public Machine { 38 | public: 39 | Cyborg(std::string name, int serial): Machine(serial), Person(name){ 40 | } 41 | 42 | void info() { 43 | std::cout << "Cyborg name: " << name << std::endl; 44 | //reference to 'serial' is ambiguous 45 | //std::cout << "Cyborg Serial: " << serial << std::endl; 46 | std::cout << "Cyborg.Person Serial: " << Person::serial << std::endl; 47 | std::cout << "Cyborg.Machine Serial: " << Machine::serial << std::endl; 48 | } 49 | }; 50 | 51 | 52 | int main() 53 | { 54 | Person p = Person("Person"); 55 | Machine m = Machine(1); 56 | Android a = Android("Android", 2); 57 | Cyborg c = Cyborg("Cyborg", 3); 58 | 59 | p.info(); 60 | m.info(); 61 | //a.info(); //request for member 'info' is ambiguous 62 | c.info(); 63 | } 64 | 65 | //This is just for LaTeX editing 66 | /* 67 | int main() 68 | { 69 | Android a = Android(); //or Android a; 70 | //a.info(); //request for member 'info' is ambiguous 71 | return 0; 72 | } 73 | */ 74 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/single/app.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "student.h" 3 | #include "professor.h" 4 | 5 | int main() 6 | { 7 | Person pe = Person("person1"); 8 | Student st = Student("student1", 15); 9 | Professor pr = Professor("professor1"); 10 | pe.luckyNumber = 10; 11 | st.luckyNumber = 20; 12 | //st.Person.luckyNumber = 40;//can't access it like this 13 | //pr.luckyNumber = 30; //private 14 | 15 | pe.info(); 16 | st.info(); 17 | //pr.info(); //private 18 | //pe.notImplemented();//The function has to be implemented 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/single/person.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | 3 | Person::Person(std::string name) 4 | { 5 | this->name = name; 6 | this->luckyNumber = 2; 7 | } 8 | 9 | void Person::info() 10 | { 11 | std::cout << "My name: " << this->name << "\n"; 12 | std::cout << "My Lucky number: " << this->luckyNumber << "\n"; 13 | } 14 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/single/person.h: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_H 2 | #define PERSON_H 3 | 4 | #include 5 | #include 6 | 7 | class Person 8 | { 9 | public: 10 | Person(std::string name); 11 | void info(); 12 | void notImplemented(); 13 | int luckyNumber; 14 | 15 | private: 16 | std::string name; 17 | }; 18 | #endif /* PERSON_H */ 19 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/single/professor.cpp: -------------------------------------------------------------------------------- 1 | #include "professor.h" 2 | 3 | /* 4 | int Professor::nbr = 0; 5 | 6 | Professor::Professor(): 7 | Person("Professor" + nbr) 8 | { 9 | nbr++; 10 | } 11 | */ 12 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/single/professor.h: -------------------------------------------------------------------------------- 1 | #ifndef PROF_H 2 | #define PROF_H 3 | #include "person.h" 4 | 5 | class Professor: private Person 6 | { 7 | public: 8 | //Professor(); 9 | using Person::Person; 10 | //private: 11 | //static int nbr; 12 | }; 13 | #endif /* PROF_H */ 14 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/single/student.cpp: -------------------------------------------------------------------------------- 1 | #include "student.h" 2 | 3 | Student::Student(std::string name, int grade): 4 | Person(name) 5 | { 6 | this->grade = grade; 7 | } 8 | 9 | void Student::info() 10 | { 11 | Person::info(); 12 | std::cout << "My grade: " << this->grade << "\n"; 13 | std::cout << "My luckyNumber2: " << this->luckyNumber << "\n"; 14 | } 15 | -------------------------------------------------------------------------------- /codes/cpp/inheritance/single/student.h: -------------------------------------------------------------------------------- 1 | #ifndef STUDENT_H 2 | #define STUDENT_H 3 | #include "person.h" 4 | 5 | class Student: public Person 6 | { 7 | public: 8 | Student(std::string name, int grade); 9 | void info(); 10 | int luckyNumber; 11 | 12 | private: 13 | int grade; 14 | }; 15 | #endif /* STUDENT_H */ 16 | -------------------------------------------------------------------------------- /codes/cpp/polymorphism/overloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Person { 5 | public: 6 | void read(){ std::cout << "I am reading" << std::endl; } 7 | void read(std::string text){ std::cout << "A text: " << text << std::endl; } 8 | }; 9 | 10 | class Student: public Person { 11 | public: 12 | using Person::read; 13 | void read(int nbr){ std::cout << "I read on table n°: " << nbr << std::endl; } 14 | }; 15 | 16 | int main() 17 | { 18 | Person pe; 19 | Student st; 20 | 21 | std::cout << "PERSON" << std::endl; 22 | pe.read(); 23 | pe.read("I am a person"); 24 | 25 | std::cout << "STUDENT" << std::endl; 26 | st.read(); // using Person::read; or error 27 | st.Person::read(); // Without using Person::read; 28 | st.read("I am a student"); // using Person::read; or error 29 | st.read(5); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /codes/cpp/polymorphism/overriding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Person { 5 | public: 6 | virtual void talk(){ std::cout << "I am a person" << std::endl; } 7 | void type(){ std::cout << "Person" << std::endl; } 8 | }; 9 | 10 | class Student: public Person { 11 | public: 12 | void talk(){ std::cout << "I am a student" << std::endl; } 13 | void type(){ std::cout << "Student" << std::endl; } 14 | }; 15 | 16 | int main() 17 | { 18 | Person *pe = new Person(); 19 | Student *st = new Student(); 20 | Person *pst = st; 21 | 22 | pe->talk(); // I am a person 23 | st->talk(); // I am a student 24 | pst->talk(); //I am a student 25 | 26 | pe->type(); //Person 27 | st->type(); //Student 28 | pst->type(); //Person 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /codes/cpp/polymorphism/subtype.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Person { 5 | public: 6 | void talk(){ 7 | std::cout << "I am a person" << std::endl; 8 | } 9 | }; 10 | 11 | class Student: public Person {}; 12 | 13 | class Professor: public Person {}; 14 | 15 | void announce(std::string msg, Person p){ 16 | std::cout << msg << ": "; 17 | p.talk(); 18 | } 19 | 20 | int main() 21 | { 22 | Person pe; Student st; Professor pr; 23 | 24 | announce("Person", pe); 25 | announce("Student", st); 26 | announce("Professor", pr); 27 | 28 | std::cout << "Table of Person" << std::endl; 29 | Person people[] = {pe, st, pr}; 30 | for (int i =0; i<3; ++i) { people[i].talk(); } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /codes/java/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /codes/java/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | OOP4Coders 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /codes/java/src/clsobj/ObjMeth.java: -------------------------------------------------------------------------------- 1 | package clsobj; 2 | 3 | public class ObjMeth { 4 | 5 | public static void main(String[] args) { 6 | Person2 p = new Person2("Karim", 1986); 7 | Person2 p2 = new Person2("Karim+1", 1986); 8 | 9 | System.out.println("As string: " + p); 10 | 11 | System.out.println("HashCode: " + p.hashCode()); 12 | 13 | try { 14 | Person2 p3 = p.clone(); 15 | System.out.println("Copy: " + p3); 16 | System.out.println("p == p3? " + (p==p3)); 17 | System.out.println("p.equals(p3)? " + (p.equals(p3))); 18 | System.out.println("p.compareTo(p2)? " + (p.compareTo(p2))); 19 | System.out.println("p.compareTo(p3)? " + (p.compareTo(p3))); 20 | } catch (CloneNotSupportedException e) {} 21 | 22 | } 23 | 24 | } 25 | 26 | class Person2 implements Cloneable, Comparable { 27 | 28 | private String name; 29 | private int byear; 30 | 31 | public Person2(String name, int byear){ 32 | this.name = name; 33 | this.byear = byear; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "(" + name + ": " + byear + ")"; 39 | } 40 | 41 | @Override 42 | public int hashCode() { 43 | return name.hashCode(); 44 | } 45 | 46 | @Override 47 | public boolean equals(Object obj) { 48 | if (getClass() != obj.getClass()) return false; 49 | return name.equals(((Person2) obj).name); 50 | } 51 | 52 | public Person2 clone() throws CloneNotSupportedException { 53 | Person2 c = (Person2) super.clone(); 54 | c.byear++; 55 | return c; 56 | } 57 | 58 | @Override 59 | public int compareTo(Person2 p2) { 60 | int cmp = byear - p2.byear; 61 | if (cmp == 0) cmp = name.compareTo(p2.name); 62 | return cmp; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /codes/java/src/clsobj/Person.java: -------------------------------------------------------------------------------- 1 | package clsobj; 2 | 3 | public class Person { 4 | 5 | private String name; 6 | private int byear; 7 | private static int nbr = 0; 8 | 9 | public Person(String name, int byear){ 10 | this.name = name; 11 | this.byear = byear; 12 | nbr++; 13 | } 14 | 15 | @Override 16 | public void finalize() { 17 | nbr--; 18 | System.out.println(name + " is out"); 19 | } 20 | 21 | public void info(){ 22 | System.out.print("My name is: " + name); 23 | System.out.println(", My birth year is: " + byear); 24 | } 25 | 26 | public static int population() { 27 | return nbr; 28 | } 29 | 30 | public static void main(String[] args) { 31 | Person p = new Person("Karim", 1986); 32 | Person p2 = new Person("Karim+1", 1987); 33 | 34 | p.info(); 35 | p2.info(); 36 | int nbr = Person.population(); 37 | 38 | System.out.println("The number of persons: " + nbr); 39 | p2 = null; 40 | nbr = Person.population(); 41 | System.out.println("The number of persons: " + nbr); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /codes/java/src/encapsulation/core/App2.java: -------------------------------------------------------------------------------- 1 | package encapsulation.core; 2 | 3 | public class App2 { 4 | 5 | public static void main(String[] args) { 6 | Person p = new Person("karim_p"); 7 | p.t = "admin"; 8 | p.luckyNumber2 = 8; 9 | p.info(); 10 | } 11 | 12 | } 13 | 14 | /* 15 | * This is a test of protected inside 16 | * the same package 17 | */ 18 | -------------------------------------------------------------------------------- /codes/java/src/encapsulation/core/Person.java: -------------------------------------------------------------------------------- 1 | package encapsulation.core; 2 | 3 | public class Person { 4 | 5 | public int luckyNumber; 6 | int luckyNumber2; 7 | protected String t; 8 | private String name; 9 | private int num; 10 | private static int nbr; 11 | 12 | public Person(String name) { 13 | this.name = name; 14 | num = (nbr++); 15 | t = "person"; 16 | luckyNumber = 0; 17 | luckyNumber2 = 0; 18 | } 19 | 20 | public void info() { 21 | System.out.println("My name: " + name); 22 | System.out.println("My number: " + num); 23 | System.out.println("I am a: " + t); 24 | System.out.println("My lucky number is: " + luckyNumber); 25 | System.out.println("My lucky number 2 is: " + luckyNumber2); 26 | System.out.println("--------------------------"); 27 | } 28 | 29 | public void copy(Person other) { 30 | num = other.num; 31 | t = other.t; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /codes/java/src/encapsulation/core/Student.java: -------------------------------------------------------------------------------- 1 | package encapsulation.core; 2 | 3 | public class Student extends Person { 4 | 5 | public Student(String name) { 6 | super(name); 7 | t = "student"; 8 | luckyNumber = 1; 9 | luckyNumber2 = 2; 10 | //this.num = 80;//cannot be accessed: private 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /codes/java/src/encapsulation/main/App.java: -------------------------------------------------------------------------------- 1 | package encapsulation.main; 2 | 3 | import encapsulation.core.*; 4 | 5 | public class App { 6 | 7 | public static void main(String[] args) { 8 | Person p = new Person("Karim_p"); 9 | Student s = new Student("Karim_s"); 10 | p.luckyNumber = 5; 11 | //p.luckyNumber2 = 6;//error: package visibility 12 | //p.num = 6;//error: private 13 | //p.t = "admin"; //error: protected 14 | p.info(); 15 | s.info(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /codes/java/src/hello/Entry.java: -------------------------------------------------------------------------------- 1 | package hello; 2 | 3 | public class Entry { 4 | public static void main(String[] args) { 5 | 6 | if (args.length < 1) { 7 | System.out.println("Please enter an integer value"); 8 | System.exit(0); 9 | } 10 | 11 | int n = Integer.parseInt(args[0]); 12 | System.out.println("Fact(" + n + ")= " + fact(n)); 13 | 14 | } 15 | 16 | public static int fact(int i) { 17 | if (i <=1) return 1; 18 | return i * fact(i-1); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /codes/java/src/hello/Except.java: -------------------------------------------------------------------------------- 1 | package hello; 2 | 3 | public class Except { 4 | 5 | public static void main(String[] args) { 6 | 7 | int n = 0; 8 | try { 9 | n = Integer.parseInt(args[0]); 10 | } 11 | catch (IndexOutOfBoundsException e) { 12 | System.out.println("Please enter an integer value"); 13 | System.exit(0); 14 | } 15 | catch (NumberFormatException e) { 16 | System.out.println("The argument has to be an integer"); 17 | System.exit(0); 18 | } 19 | 20 | try { 21 | int f = fact(n); 22 | System.out.println("Fact(" + n + ")= " + f); 23 | } 24 | catch (NegException | BigException e) { 25 | System.out.println(e.getMessage()); 26 | } 27 | finally { 28 | System.out.println("That's all!!"); 29 | } 30 | 31 | } 32 | 33 | public static int fact(int i) throws NegException, BigException { 34 | if (i < 0 ) throw new NegException(); 35 | if (i > 5) throw new BigException(); 36 | if (i ==0) return 1; 37 | return i * fact(i-1); 38 | } 39 | 40 | } 41 | 42 | class NegException extends Exception { 43 | @Override 44 | public String getMessage() { 45 | return "Negative numbers don't have a factorial!"; 46 | } 47 | } 48 | 49 | class BigException extends Exception { 50 | public String getMessage() { 51 | return "The number is too big!"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /codes/java/src/hello/Func.java: -------------------------------------------------------------------------------- 1 | package hello; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Func { 6 | 7 | public static void main(String[] args) { 8 | System.out.print("Please enter an integer value: "); 9 | Scanner cs = new Scanner(System.in); 10 | int n = cs.nextInt(); 11 | cs.close(); 12 | System.out.print("Fact(" + n + ")="); 13 | System.out.println(fact(n)); 14 | } 15 | 16 | public static int fact(int i){ 17 | if (i <=1) return 1; 18 | return i * fact(i-1); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /codes/java/src/hello/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package hello; 2 | 3 | public class HelloWorld { 4 | public static void main(String[] args) { 5 | System.out.println("Hello World!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/abst/App.java: -------------------------------------------------------------------------------- 1 | package inheritance.abst; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | //Person pe = new Person();//is abstract; cannot be instantiated 7 | //Student st = new Student();//is abstract; cannot be instantiated 8 | Professor pr = new Professor(); 9 | GradStudent gs = new GradStudent(); 10 | 11 | pr.info(); 12 | gs.info(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/abst/GradStudent.java: -------------------------------------------------------------------------------- 1 | package inheritance.abst; 2 | 3 | public class GradStudent extends Student { 4 | 5 | public void info2() { 6 | System.out.println("GradStudent info2"); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/abst/Person.java: -------------------------------------------------------------------------------- 1 | package inheritance.abst; 2 | 3 | public abstract class Person { 4 | 5 | public void info() { 6 | System.out.println("====INFO==="); 7 | info2(); 8 | } 9 | 10 | public abstract void info2(); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/abst/Professor.java: -------------------------------------------------------------------------------- 1 | package inheritance.abst; 2 | 3 | public class Professor extends Person { 4 | 5 | public void info2() { 6 | System.out.println("Professor info2"); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/abst/Student.java: -------------------------------------------------------------------------------- 1 | package inheritance.abst; 2 | 3 | public abstract class Student extends Person { 4 | } 5 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/fin/FinalCls.java: -------------------------------------------------------------------------------- 1 | package inheritance.fin; 2 | 3 | public class FinalCls { 4 | 5 | public static void main(String[] args) { 6 | Person p = new Person(); 7 | } 8 | 9 | } 10 | 11 | final class Person {} 12 | 13 | //class Student extends Person {}//cannot inherit 14 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/fin/FinalMth.java: -------------------------------------------------------------------------------- 1 | package inheritance.fin; 2 | 3 | public class FinalMth { 4 | 5 | public static void main(String[] args) { 6 | Person p = new Person(); 7 | Student s = new Student(); 8 | 9 | p.method(); 10 | s.method(); 11 | 12 | p.finalMethod(); 13 | s.finalMethod(); 14 | } 15 | 16 | } 17 | 18 | class Person { 19 | public void method(){ 20 | System.out.println("Person method"); 21 | } 22 | public final void finalMethod(){ 23 | System.out.println("Person final method"); 24 | } 25 | } 26 | 27 | class Student extends Person { 28 | public void method(){ 29 | System.out.println("Student method"); 30 | } 31 | //public final void finalMethod(){}//cannot override 32 | } 33 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/multiple/Diamond1.java: -------------------------------------------------------------------------------- 1 | package inheritance.multiple; 2 | 3 | public class Diamond1 { 4 | 5 | public static void main(String[] args) { 6 | PhdStudent ps = new PhdStudent(); 7 | ps.info(); 8 | } 9 | 10 | } 11 | 12 | interface HumanBehavior { 13 | default void info(){ 14 | System.out.println("Person"); 15 | } 16 | } 17 | 18 | interface ProfessorBehavior extends HumanBehavior {} 19 | 20 | interface StudentBehavior extends HumanBehavior { 21 | default void info(){ 22 | System.out.println("Student"); 23 | } 24 | } 25 | 26 | class PhdStudent implements StudentBehavior, ProfessorBehavior {} 27 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/multiple/Multiple1.java: -------------------------------------------------------------------------------- 1 | package inheritance.multiple; 2 | 3 | public class Multiple1 { 4 | 5 | public static void main(String[] args) { 6 | Person p = new Person(); 7 | //p.info(); 8 | System.out.println("====================="); 9 | Machine m = new Machine(); 10 | //m.info(); 11 | System.out.println("====================="); 12 | Android a = new Android(); 13 | //a.info(); 14 | System.out.println("====================="); 15 | Cyborg c = new Cyborg(); 16 | //c.info(); 17 | System.out.println("====================="); 18 | } 19 | 20 | } 21 | 22 | interface HumanBehaviour { 23 | default void info() { 24 | System.out.println("I am a person"); 25 | } 26 | } 27 | 28 | interface MachineBehaviour { 29 | default void info() { 30 | System.out.println("I am a machine"); 31 | } 32 | } 33 | 34 | class Person implements HumanBehaviour { 35 | public Person(){ 36 | info(); 37 | } 38 | } 39 | 40 | class Machine implements MachineBehaviour { 41 | public Machine(){ 42 | info(); 43 | } 44 | } 45 | 46 | class Android extends Machine implements HumanBehaviour { 47 | public void info() { 48 | super.info(); 49 | HumanBehaviour.super.info(); 50 | System.out.println("So, I am an android"); 51 | } 52 | } 53 | 54 | class Cyborg extends Person implements MachineBehaviour { 55 | public void info() { 56 | super.info(); 57 | MachineBehaviour.super.info(); 58 | System.out.println("So, I am a cyborg"); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/multiple/Multiple2.java: -------------------------------------------------------------------------------- 1 | package inheritance.multiple; 2 | 3 | public class Multiple2 { 4 | 5 | public static void main(String[] args) { 6 | 7 | Person2 a = new Person2("person1"); 8 | a.info(); 9 | System.out.println("====================="); 10 | Student2 s = new Student2("student1", 12.5); 11 | s.info(); 12 | System.out.println("====================="); 13 | Professor2 p = new Professor2("professor1", 8); 14 | p.info(); 15 | System.out.println("====================="); 16 | PhdStudent2 ps = new PhdStudent2("phDstudent1", 4, 11.25); 17 | ps.info(); 18 | System.out.println("====================="); 19 | } 20 | 21 | } 22 | 23 | interface ProfessorBehavior2 extends HumanBehavior { 24 | default void info(){ 25 | System.out.println("Professor"); 26 | } 27 | } 28 | 29 | interface StudentBehavior2 extends HumanBehavior { 30 | default void info(){ 31 | System.out.println("Student"); 32 | } 33 | } 34 | 35 | class Person2 { 36 | protected String name; 37 | public Person2(String name){ 38 | this.name = name; 39 | } 40 | 41 | public void info() { 42 | System.out.println("my name: " + name); 43 | } 44 | } 45 | 46 | class Student2 extends Person2 implements StudentBehavior2 { 47 | protected double mark; 48 | public Student2(String name, double mark){ 49 | super(name); 50 | this.mark = mark; 51 | } 52 | 53 | public void info() { 54 | StudentBehavior2.super.info(); 55 | super.info(); 56 | System.out.println("my mark: " + mark); 57 | } 58 | } 59 | 60 | class Professor2 extends Person2 implements ProfessorBehavior2 { 61 | protected int hours; 62 | public Professor2(String name, int hours){ 63 | super(name); 64 | this.hours = hours; 65 | } 66 | public void info() { 67 | ProfessorBehavior2.super.info(); 68 | super.info(); 69 | System.out.println("my work hours: " + hours); 70 | } 71 | } 72 | 73 | 74 | class PhdStudent2 extends Person2 implements StudentBehavior2, ProfessorBehavior2 { 75 | protected double mark; 76 | protected int hours; 77 | public PhdStudent2(String name, int hours, double mark){ 78 | super(name); 79 | this.hours = hours; 80 | this.mark = mark; 81 | } 82 | 83 | public void info() { 84 | StudentBehavior2.super.info(); 85 | ProfessorBehavior2.super.info(); 86 | super.info(); 87 | System.out.println("my mark: " + mark); 88 | System.out.println("my work hours: " + hours); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/single/App.java: -------------------------------------------------------------------------------- 1 | package inheritance.single; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | Person pe = new Person("Person1"); 7 | Student st = new Student("Student1", 15); 8 | Professor pr = new Professor(); 9 | pe.luckyNumber = 10; 10 | st.luckyNumber = 20; 11 | //pr.luckyNumber = 30; 12 | pe.info(); 13 | st.info(); 14 | pr.info(); 15 | 16 | //pe.info2();//error: method private 17 | //st.info2();//error: method private 18 | pr.info2(); 19 | GradStudent gs = new GradStudent("Student2", 12); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/single/GradStudent.java: -------------------------------------------------------------------------------- 1 | package inheritance.single; 2 | 3 | public class GradStudent extends Student { 4 | 5 | public GradStudent(String name, int grade) { 6 | super(name, grade); 7 | //super.super.info();//error: expected 8 | //super.super.luckyNumber = 10;//error: expected 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/single/Person.java: -------------------------------------------------------------------------------- 1 | package inheritance.single; 2 | 3 | public class Person { 4 | 5 | public int luckyNumber; 6 | private String name; 7 | 8 | public Person(String name) { 9 | this.name = name; 10 | luckyNumber = 0; 11 | } 12 | 13 | public void info() { 14 | System.out.println("My name: " + name); 15 | System.out.println("My lucky number is: " + luckyNumber); 16 | } 17 | 18 | private void info2() { 19 | System.out.println("Person.info2"); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/single/Professor.java: -------------------------------------------------------------------------------- 1 | package inheritance.single; 2 | 3 | public class Professor extends Person { 4 | static int num = 0; 5 | private int luckyNumber = 9; 6 | public Professor() { 7 | super("Professor" + num); 8 | num++; 9 | } 10 | 11 | public void info2() { 12 | System.out.println("Professor info2, luckyNumber: " + luckyNumber); 13 | System.out.println("Professor info2, super.luckyNumber: " + super.luckyNumber); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /codes/java/src/inheritance/single/Student.java: -------------------------------------------------------------------------------- 1 | package inheritance.single; 2 | 3 | public class Student extends Person { 4 | 5 | private int grade; 6 | 7 | public Student(String name, int grade) { 8 | super(name); 9 | this.grade = grade; 10 | } 11 | 12 | public void info(){ 13 | super.info(); 14 | System.out.println("My grade is: " + grade); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /codes/java/src/polymorphism/Overloading.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | public class Overloading { 4 | public static void main(String[] args) { 5 | Person pe = new Person(); 6 | Student st = new Student(); 7 | 8 | System.out.println("PERSON"); 9 | pe.read(); 10 | pe.read("I am a person"); 11 | 12 | System.out.println("STUDENT"); 13 | st.read(); 14 | st.read("I am a student"); 15 | st.read(5); 16 | } 17 | } 18 | 19 | class Person { 20 | public void read(){ System.out.println("I am reading"); } 21 | public void read(String text){ System.out.println("A text: " + text); } 22 | } 23 | 24 | class Student extends Person { 25 | public void read(int nbr){ System.out.println("I read on table n°: " + nbr); } 26 | } 27 | -------------------------------------------------------------------------------- /codes/java/src/polymorphism/Overriding.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | public class Overriding { 4 | public static void main(String[] args) { 5 | Person pe = new Person(); 6 | Person st = new Student(); 7 | pe.talk(); // I am a person 8 | st.talk(); // I am a student 9 | } 10 | } 11 | 12 | class Person { 13 | public void talk(){ System.out.println("I am a person"); } 14 | } 15 | 16 | class Student extends Person { 17 | @Override 18 | public void talk(){ System.out.println("I am a student"); } 19 | } 20 | -------------------------------------------------------------------------------- /codes/java/src/polymorphism/Subtype.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | public class Subtype { 4 | public static void announce(String msg, Person p) { 5 | System.out.print(msg + ": "); 6 | p.talk(); 7 | } 8 | //... 9 | public static void announce2(String msg, Machine m) { 10 | System.out.print(msg + ": "); 11 | m.talk(); 12 | } 13 | public static void main(String[] args) { 14 | Person pe = new Person(); 15 | Student st = new Student(); 16 | Professor pr = new Professor(); 17 | 18 | announce("Person", pe); 19 | announce("Student", st); 20 | announce("Professor", pe); 21 | 22 | System.out.println("Table of Person"); 23 | Person[] people = {pe, st, pr}; 24 | for (Person p: people) { p.talk(); } 25 | 26 | Robot ro = new Robot(); 27 | Cyborg cy = new Cyborg(); 28 | 29 | announce2("Robot", ro); 30 | announce2("Cyborg", cy); 31 | announce("Cyborg", cy); 32 | } 33 | } 34 | 35 | class Person { 36 | public void talk(){ 37 | System.out.println("I am a person"); 38 | } 39 | } 40 | 41 | interface Machine { 42 | public void talk(); 43 | } 44 | 45 | class Student extends Person {} 46 | 47 | class Professor extends Person {} 48 | 49 | class Robot implements Machine { 50 | public void talk(){ 51 | System.out.println("I am a robot"); 52 | } 53 | } 54 | 55 | class Cyborg extends Person implements Machine {} 56 | -------------------------------------------------------------------------------- /codes/javascript/clsobj/obj_meth.js: -------------------------------------------------------------------------------- 1 | function Person(name, byear) { 2 | this.name = name; 3 | this.byear = byear; 4 | } 5 | 6 | Person.prototype.toString = function(){ 7 | return "(" + this.name + ": " + this.byear + ")"; 8 | } 9 | 10 | var p = new Person("Karim", 1986); 11 | 12 | console.log("As string: " + p); 13 | -------------------------------------------------------------------------------- /codes/javascript/clsobj/person.js: -------------------------------------------------------------------------------- 1 | class Person { 2 | constructor(name, byear) { 3 | this.name = name; 4 | this["byear"] = byear; 5 | Person.nbr++; 6 | } 7 | 8 | info(){ 9 | console.log("My name: " + this.name + ", My birth year: " + this.byear); 10 | } 11 | 12 | static population() { 13 | return Person.nbr; 14 | } 15 | } 16 | 17 | Person.nbr = 0; 18 | 19 | var p = new Person("Karim", 1986); 20 | var p2 = new Person("Karim+1", 1987); 21 | 22 | p.info(); 23 | p2.info(); 24 | var nbr = Person.population(); 25 | 26 | console.log("The number of persons: " + nbr); 27 | p2 = null; 28 | nbr = Person.population(); 29 | console.log("The number of persons: " + nbr); 30 | -------------------------------------------------------------------------------- /codes/javascript/clsobj/person2.js: -------------------------------------------------------------------------------- 1 | function Person(name, byear) { 2 | this.name = name; 3 | this.byear = byear; 4 | Person.nbr++; 5 | } 6 | 7 | Person.nbr = 0; 8 | 9 | Person.prototype.info = function(){ 10 | console.log("My name: " + this.name + ", My birth year: " + this.byear); 11 | } 12 | 13 | Person.population = function(){ 14 | return Person.nbr; 15 | } 16 | 17 | var p = new Person("Karim", 1986); 18 | var p2 = new Person("Karim+1", 1987); 19 | 20 | p.info(); 21 | p2.info(); 22 | var nbr = Person.population(); 23 | 24 | console.log("The number of persons: " + nbr); 25 | p2 = null; 26 | nbr = Person.population(); 27 | console.log("The number of persons: " + nbr); 28 | -------------------------------------------------------------------------------- /codes/javascript/clsobj/person3.js: -------------------------------------------------------------------------------- 1 | function Person(name, byear) { 2 | this.name = name; 3 | this.byear = byear; 4 | this.info = function(){ 5 | console.log("My name: " + this.name); 6 | console.log("My birth year: " + this.byear); 7 | } 8 | } 9 | 10 | var p = new Person("Karim", 1986); 11 | p.info(); 12 | -------------------------------------------------------------------------------- /codes/javascript/clsobj/property.js: -------------------------------------------------------------------------------- 1 | function Rectangle() { 2 | this._width = 0; 3 | this._height = 0; 4 | this.info = function() { 5 | console.log("width: ", this._width, ", height: ", this._height); 6 | }; 7 | } 8 | 9 | Object.defineProperties(Rectangle.prototype, { 10 | w: { 11 | get: function () { 12 | return this._width + 2; 13 | }, 14 | set: function (value) { 15 | if (value < this._width) { 16 | this._height = value; 17 | } 18 | else { 19 | this._height = this._width; 20 | this._width = value; 21 | } 22 | } 23 | }, 24 | h: { 25 | get: function () { 26 | return this._height + 1; 27 | } 28 | } 29 | }); 30 | 31 | var r = new Rectangle(); 32 | r.w = 50; 33 | r.w = 20; 34 | r.info(); 35 | console.log("w: ", r.w, ", h: ", r.h); 36 | -------------------------------------------------------------------------------- /codes/javascript/clsobj/proto.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | "use strict"; 4 | 5 | function Person(){ 6 | this.talk = function(){} 7 | } 8 | 9 | function Machine(){} 10 | Machine.prototype.talk = function(){} 11 | 12 | var p1 = new Person(), 13 | p2 = new Person(); 14 | 15 | var m1 = new Machine(), 16 | m2 = new Machine(); 17 | 18 | console.log("p1.talk == p2.talk", p1.talk == p2.talk); 19 | console.log("m1.talk == m2.talk", m1.talk == m2.talk); 20 | -------------------------------------------------------------------------------- /codes/javascript/encapsulation/app.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | var Student = require("./student.js"); 3 | 4 | var p = new Person("Karim_p"); 5 | var s = new Student("Karim_s"); 6 | 7 | console.log(p.luckyNumber); 8 | console.log(p._name); 9 | console.log(p.num);//undefined 10 | console.log("========================="); 11 | p.info(); 12 | s.info(); 13 | -------------------------------------------------------------------------------- /codes/javascript/encapsulation/decorate.js: -------------------------------------------------------------------------------- 1 | let Person = function(name) { 2 | this.name = name; 3 | } 4 | 5 | function create(name, members) { 6 | let p = new Person(name); 7 | p = Object.assign(p, members); 8 | return p; 9 | } 10 | 11 | let student = create ("Karim_s", { 12 | year: 2 13 | }); 14 | 15 | let teacher = create ("Karim_t", { 16 | courses: ["C1", "C2"] 17 | }); 18 | 19 | function info (person) { 20 | console.log(person.name); 21 | for(let member in person){ 22 | if (typeof member != "function") { 23 | console.log(member, " = ", person[member]); 24 | } 25 | } 26 | } 27 | 28 | info(student); 29 | info(teacher); 30 | -------------------------------------------------------------------------------- /codes/javascript/encapsulation/person.js: -------------------------------------------------------------------------------- 1 | module.exports = Person; 2 | 3 | function Person(name) { 4 | this.luckyNumber = 0; 5 | this._name = name; 6 | var num = Person.nbr; 7 | Person.nbr++; 8 | 9 | this.info1 = function(){ 10 | console.log("My number is: ", num); 11 | } 12 | } 13 | 14 | Person.nbr = 0; 15 | 16 | Person.prototype.info = function(){ 17 | console.log("My name: ", this._name); 18 | this.info1(); 19 | console.log("My lucky number is: ", luckyNumber); 20 | console.log("--------------------------"); 21 | } 22 | -------------------------------------------------------------------------------- /codes/javascript/encapsulation/student.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | 3 | module.exports = Student; 4 | 5 | function Student(name) { 6 | Person.call(this, name); 7 | this._name = name + "2";//can be accessed 8 | //console.log("Student => num = ", num, ", this.num = ", this.num); //error 9 | luckyNumber = 1; 10 | } 11 | 12 | Student.prototype = Object.create(Person.prototype); 13 | Student.constructor = Student; 14 | -------------------------------------------------------------------------------- /codes/javascript/hello/entry.js: -------------------------------------------------------------------------------- 1 | var args = process.argv; 2 | 3 | if (args.length < 3) { 4 | console.log("Please enter an integer value"); 5 | process.exit(); 6 | } 7 | 8 | var n = args[2]; 9 | console.log("Fact(" + n + ")= " + fact(n) ); 10 | 11 | function fact(i) { 12 | if (i <= 0) return 1; 13 | return i * fact(i-1); 14 | } 15 | -------------------------------------------------------------------------------- /codes/javascript/hello/except.js: -------------------------------------------------------------------------------- 1 | var args = process.argv; 2 | 3 | if (args.length < 3) { 4 | console.log("Please enter an integer value"); 5 | process.exit(); 6 | } 7 | 8 | var n = args[2]; 9 | 10 | try { 11 | var f = fact(n); 12 | console.log("Fact(" + n + ")= " + f ); 13 | } 14 | catch(error) { 15 | console.log(error); 16 | } 17 | finally { 18 | console.log("That's all!"); 19 | } 20 | 21 | function fact(i) { 22 | if (i < 0) throw "Negative numbers don't have a factorial!"; 23 | if (i > 5) throw "The number is too big!"; 24 | 25 | if (i == 0) return 1; 26 | return i * fact(i-1); 27 | } 28 | -------------------------------------------------------------------------------- /codes/javascript/hello/func.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | Functions 4 | 6 | 14 | 15 | 16 | Please enter an integer value: 17 | 18 | 19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /codes/javascript/hello/func.js: -------------------------------------------------------------------------------- 1 | function fact(i) { 2 | if (i <= 1) return 1; 3 | return i * fact(i-1); 4 | } 5 | -------------------------------------------------------------------------------- /codes/javascript/hello/helloworld.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | Hello world 4 | 5 | 6 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /codes/javascript/hello/helloworld.js: -------------------------------------------------------------------------------- 1 | console.log("Hello World!"); 2 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es5/app.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | var Student = require("./student.js"); 3 | var GradStudent = require("./gradstudent.js"); 4 | var Professor = require("./professor.js"); 5 | 6 | //var pe = new Person();//Error: abstract class 7 | var st = new Student(); 8 | var pr = new Professor(); 9 | var gs = new GradStudent(); 10 | 11 | //st.info();//Error: abstract method info2 12 | pr.info(); 13 | gs.info(); 14 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es5/gradstudent.js: -------------------------------------------------------------------------------- 1 | var Student = require("./student.js"); 2 | 3 | module.exports = GradStudent; 4 | 5 | function GradStudent() { 6 | Student.call(this); 7 | } 8 | 9 | GradStudent.prototype = Object.create(Student.prototype); 10 | GradStudent.prototype.constructor = GradStudent; 11 | 12 | GradStudent.prototype.info2 = function(){ 13 | console.log("GradStudent info2"); 14 | } 15 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es5/person.js: -------------------------------------------------------------------------------- 1 | module.exports = Person; 2 | 3 | function Person() { 4 | if (this.constructor == Person) { 5 | throw new Error("Cannot instanciate abstract class"); 6 | } 7 | } 8 | 9 | Person.prototype.info = function(){ 10 | console.log("====INFO==="); 11 | this.info2(); 12 | } 13 | 14 | Person.prototype.info2 = function(){ 15 | throw new Error("Cannot call abstract method"); 16 | } 17 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es5/professor.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | 3 | module.exports = Professor; 4 | 5 | function Professor() { 6 | Person.call(this); 7 | } 8 | 9 | Professor.prototype = Object.create(Person.prototype); 10 | Professor.prototype.constructor = Professor; 11 | 12 | Professor.prototype.info2 = function(){ 13 | console.log("Professor info2"); 14 | } 15 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es5/student.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | 3 | module.exports = Student; 4 | 5 | function Student() { 6 | Person.call(this); 7 | } 8 | 9 | Student.prototype = Object.create(Person.prototype); 10 | Student.prototype.constructor = Student; 11 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es6/app.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | var Student = require("./student.js"); 3 | var GradStudent = require("./gradstudent.js"); 4 | var Professor = require("./professor.js"); 5 | 6 | //var pe = new Person();//Error: abstract class 7 | var st = new Student(); 8 | var pr = new Professor(); 9 | var gs = new GradStudent(); 10 | 11 | //st.info();//Error: abstract method info2 12 | pr.info(); 13 | gs.info(); 14 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es6/gradstudent.js: -------------------------------------------------------------------------------- 1 | var Student = require("./student.js"); 2 | 3 | class GradStudent extends Student { 4 | info2() { 5 | console.log("GradStudent info2"); 6 | } 7 | } 8 | 9 | module.exports = GradStudent; 10 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es6/person.js: -------------------------------------------------------------------------------- 1 | class Person { 2 | constructor() { 3 | if (this.constructor == Person) { 4 | throw new Error("Cannot instanciate abstract class"); 5 | } 6 | } 7 | 8 | info() { 9 | console.log("====INFO==="); 10 | this.info2(); 11 | } 12 | 13 | info2(){ 14 | throw new Error("Cannot call abstract method"); 15 | } 16 | } 17 | 18 | module.exports = Person; 19 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es6/professor.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | 3 | class Professor extends Person { 4 | info2() { 5 | console.log("Professor info2"); 6 | } 7 | } 8 | 9 | module.exports = Professor; 10 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/abst/es6/student.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | 3 | class Student extends Person {} 4 | 5 | module.exports = Student; 6 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/fin/es5/finalcls.js: -------------------------------------------------------------------------------- 1 | function Person() { 2 | if (this.constructor !== Person) { 3 | throw new Error("Cannot extend final class"); 4 | } 5 | } 6 | 7 | function Student() { 8 | Person.call(this); 9 | } 10 | 11 | Student.prototype = Object.create(Person.prototype); 12 | Student.prototype.constructor = Student; 13 | 14 | var pe = new Person(); 15 | //var st = new Student();//Error: final class 16 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/fin/es6/finalcls.js: -------------------------------------------------------------------------------- 1 | class Person { 2 | constructor() { 3 | if (this.constructor !== Person) { 4 | throw new Error("Cannot extend final class"); 5 | } 6 | } 7 | } 8 | 9 | class Student extends Person {} 10 | 11 | var pe = new Person(); 12 | //var st = new Student();//Error: final class 13 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/multiple/es5/diamond1.js: -------------------------------------------------------------------------------- 1 | function Person(name) { 2 | this.name = name; 3 | console.log("Person called"); 4 | } 5 | 6 | Person.prototype.info = function() { 7 | console.log("my name is: ", this.name); 8 | } 9 | 10 | function Student(name, mark) { 11 | Person.call(this, name); 12 | this.mark = mark; 13 | console.log("Student called"); 14 | } 15 | 16 | Student.prototype = Object.create(Person.prototype); 17 | Student.prototype.constructor = Student; 18 | 19 | function Professor(name, hours) { 20 | Person.call(this, name); 21 | this.hours = hours; 22 | console.log("Professor called"); 23 | } 24 | 25 | Professor.prototype = Object.create(Person.prototype); 26 | Professor.prototype.constructor = Professor; 27 | 28 | function PhdStudent(name, hours, mark) { 29 | Professor.call(this, name, hours); 30 | Student.call(this, name + "_student", mark); 31 | console.log("PhdStudent called"); 32 | } 33 | 34 | PhdStudent.prototype = Object.assign( 35 | Object.create(Professor.prototype), 36 | Object.create(Student.prototype) 37 | ); 38 | PhdStudent.prototype.constructor = PhdStudent; 39 | PhdStudent.prototype.info = function() { 40 | console.log("my name is: ", this.name); 41 | } 42 | 43 | 44 | 45 | var a = new Person("person"); 46 | a.info(); 47 | console.log("====================="); 48 | var s = new Student("student", 15.5); 49 | s.info(); 50 | console.log("====================="); 51 | var p = new Professor("professor", 8); 52 | p.info(); 53 | console.log("====================="); 54 | var ps = new PhdStudent("phdStudent", 4, 12.25); 55 | ps.info(); 56 | console.log("====================="); 57 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/multiple/es5/multiple1.js: -------------------------------------------------------------------------------- 1 | function Person() { 2 | console.log("I am a person"); 3 | } 4 | 5 | Person.prototype.info = function() { 6 | console.log("Person"); 7 | } 8 | 9 | function Machine() { 10 | console.log("I am a machine"); 11 | } 12 | 13 | Machine.prototype.info = function() { 14 | console.log("Machine"); 15 | } 16 | 17 | function Android() { 18 | Person.call(this); 19 | Machine.call(this); 20 | } 21 | 22 | Android.prototype = Object.assign( 23 | Object.create(Person.prototype), 24 | Object.create(Machine.prototype) 25 | ); 26 | Android.prototype.constructor = Android; 27 | 28 | function Cyborg() { 29 | Machine.call(this); 30 | Person.call(this); 31 | } 32 | 33 | Cyborg.prototype = Object.assign( 34 | Object.create(Machine.prototype), 35 | Object.create(Person.prototype) 36 | ); 37 | Cyborg.prototype.constructor = Cyborg; 38 | 39 | 40 | var p = new Person(); 41 | p.info(); 42 | console.log("====================="); 43 | var m = new Machine(); 44 | m.info(); 45 | console.log("====================="); 46 | var a = new Android(); 47 | a.info(); 48 | console.log("====================="); 49 | var c = new Cyborg(); 50 | c.info(); 51 | console.log("====================="); 52 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/single/es5/app.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | var Student = require("./student.js"); 3 | var Professor = require("./professor.js"); 4 | 5 | var pe = new Person("person1"); 6 | var st = new Student("student1", 15); 7 | var pr = new Professor(); 8 | 9 | pe.luckyNumber = 10; 10 | st.luckyNumber = 20; 11 | pr.luckyNumber = 30; 12 | 13 | pe.info(); 14 | st.info(); 15 | pr.info(); 16 | 17 | //gs = new GradStudent("Student2", 12); 18 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/single/es5/person.js: -------------------------------------------------------------------------------- 1 | module.exports = Person; 2 | 3 | function Person(name) { 4 | this.luckyNumber = 0; 5 | this._name = name; 6 | } 7 | 8 | Person.prototype.info = function(){ 9 | console.log("My name: ", this._name); 10 | console.log("My lucky number is: ", this.luckyNumber); 11 | } 12 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/single/es5/professor.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | 3 | module.exports = Professor; 4 | 5 | function Professor() { 6 | //Person.call(this, "Professor"); 7 | } 8 | 9 | Professor.prototype = Object.create(Person.prototype); 10 | Professor.prototype.constructor = Professor; 11 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/single/es5/student.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | 3 | module.exports = Student; 4 | 5 | function Student(name, grade) { 6 | Person.call(this, name); 7 | this._grade = grade; 8 | } 9 | 10 | Student.prototype = Object.create(Person.prototype); 11 | Student.prototype.constructor = Student; 12 | 13 | Student.prototype.info = function(){ 14 | Person.prototype.info.call(this) 15 | console.log("My grade: ", this._grade); 16 | } 17 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/single/es6/app.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | var Student = require("./student.js"); 3 | var Professor = require("./professor.js"); 4 | 5 | var pe = new Person("person1"); 6 | var st = new Student("student1", 15); 7 | var pr = new Professor(); 8 | 9 | pe.luckyNumber = 10; 10 | st.luckyNumber = 20; 11 | pr.luckyNumber = 30; 12 | 13 | pe.info(); 14 | st.info(); 15 | pr.info(); 16 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/single/es6/person.js: -------------------------------------------------------------------------------- 1 | class Person { 2 | constructor(name) { 3 | this.luckyNumber = 0; 4 | this._name = name; 5 | } 6 | 7 | info() { 8 | console.log("My name: ", this._name); 9 | console.log("My lucky number is: ", this.luckyNumber); 10 | } 11 | } 12 | 13 | module.exports = Person; 14 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/single/es6/professor.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | 3 | class Professor extends Person {} 4 | 5 | module.exports = Professor; 6 | -------------------------------------------------------------------------------- /codes/javascript/inheritance/single/es6/student.js: -------------------------------------------------------------------------------- 1 | var Person = require("./person.js"); 2 | 3 | class Student extends Person { 4 | 5 | constructor(name, grade) { 6 | super(name); 7 | this._grade = grade; 8 | } 9 | 10 | info(){ 11 | super.info(); 12 | console.log("My grade: ", this._grade); 13 | } 14 | 15 | } 16 | 17 | module.exports = Student; 18 | -------------------------------------------------------------------------------- /codes/javascript/polymorphism/es5/overloading.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | function Person () {} 4 | Person.prototype.read = function(arg1){ 5 | switch (typeof arg1) { 6 | case "undefined": 7 | console.log("I am reading"); 8 | break; 9 | case "string": 10 | console.log("A text: ", arg1); 11 | break; 12 | default: 13 | } 14 | } 15 | 16 | function Student() {} 17 | Student.prototype = Object.create(Person.prototype); 18 | Student.prototype.constructor = Student; 19 | Student.prototype.read = function(arg1){ 20 | if (typeof arg1 == "number"){ 21 | console.log("I read on table n°: ", arg1); 22 | } else { 23 | Person.prototype.read.call(this, arg1); 24 | } 25 | } 26 | Student.prototype.count = function(){ 27 | console.log("number of arguments: ", arguments.length); 28 | } 29 | 30 | console.log("PERSON"); 31 | var pe = new Person(); 32 | pe.read(); 33 | pe.read("I am a person"); 34 | 35 | console.log("STUDENT"); 36 | var st = new Student(); 37 | st.read(); 38 | st.read("I am a student"); 39 | st.read(5); 40 | st.count(5, "text", 10); 41 | -------------------------------------------------------------------------------- /codes/javascript/polymorphism/es5/overriding.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | function Person() {} 4 | Person.prototype.talk = function(){ console.log("I am a person"); } 5 | 6 | function Student() {} 7 | Student.prototype = Object.create(Person.prototype); 8 | Student.prototype.constructor = Student; 9 | Student.prototype.talk = function(){ console.log("I am a student"); } 10 | 11 | var pe = new Person(), 12 | st = new Student(); 13 | pe.talk(); // I am a person 14 | st.talk(); // I am a student 15 | -------------------------------------------------------------------------------- /codes/javascript/polymorphism/es5/subtype.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | function Person() {} 4 | Person.prototype.talk = function() { 5 | console.log("I am a person"); 6 | } 7 | 8 | function Student() {} 9 | Student.prototype = Object.create(Person.prototype); 10 | Student.prototype.constructor = Student; 11 | 12 | function Professor() {} 13 | Professor.prototype = Object.create(Person.prototype); 14 | Professor.prototype.constructor = Professor; 15 | 16 | function Robot() { 17 | this.talk = function() { 18 | console.log("I am a robot"); 19 | } 20 | } 21 | 22 | function announce(msg, talker) { 23 | process.stdout.write(msg + ": "); 24 | if (typeof talker.talk == "function") talker.talk(); 25 | else console.log("Sorry! I do not talk!"); 26 | } 27 | 28 | let pe = new Person(), 29 | st = new Student(), 30 | pr = new Professor(); 31 | 32 | let ro = new Robot(); 33 | let cat = { 34 | talk: function(){ 35 | console.log("Meow!"); 36 | } 37 | }; 38 | 39 | announce("Person", pe); 40 | announce("Student", st); 41 | announce("Professor", pr); 42 | announce("Robot", ro); 43 | announce("a cat", cat); 44 | announce("a number", 25); 45 | 46 | console.log("A table with different types"); 47 | let elements = [pe, st, pr, ro, cat, 25]; 48 | for (let i = 0; i < elements.length; i++){ 49 | try { 50 | elements[i].talk(); 51 | } 52 | catch(error){ 53 | console.log("The element n° ", i, " does not talk()"); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /codes/javascript/polymorphism/es6/overloading.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | class Person { 4 | read(arg1) { 5 | switch (typeof arg1) { 6 | case "undefined": 7 | console.log("I am reading"); 8 | break; 9 | case "string": 10 | console.log("A text: ", arg1); 11 | break; 12 | default: 13 | } 14 | } 15 | } 16 | 17 | class Student extends Person { 18 | read(arg1){ 19 | if (typeof arg1 == "number"){ 20 | console.log("I read on table n°: ", arg1); 21 | } else { 22 | super.read(arg1); 23 | } 24 | } 25 | count(){ 26 | console.log("number of arguments: ", arguments.length); 27 | } 28 | } 29 | 30 | console.log("PERSON"); 31 | let pe = new Person(); 32 | pe.read(); 33 | pe.read("I am a person"); 34 | 35 | console.log("STUDENT"); 36 | let st = new Student(); 37 | st.read(); 38 | st.read("I am a student"); 39 | st.read(5); 40 | st.count(5, "text", 10); 41 | -------------------------------------------------------------------------------- /codes/javascript/polymorphism/es6/overriding.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | class Person { 4 | talk(){ console.log("I am a person"); } 5 | } 6 | 7 | class Student extends Person { 8 | talk(){ console.log("I am a student"); } 9 | } 10 | 11 | let pe = new Person(), 12 | st = new Student(); 13 | pe.talk(); // I am a person 14 | st.talk(); // I am a student 15 | -------------------------------------------------------------------------------- /codes/javascript/polymorphism/es6/subtype.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | class Person { 4 | talk() { 5 | console.log("I am a person"); 6 | } 7 | } 8 | 9 | class Student extends Person {} 10 | 11 | class Professor extends Person {} 12 | 13 | class Robot { 14 | talk() { 15 | console.log("I am a robot"); 16 | } 17 | } 18 | 19 | function announce(msg, talker) { 20 | process.stdout.write(msg + ": "); 21 | if (typeof talker.talk == "function") talker.talk(); 22 | else console.log("Sorry! I do not talk!"); 23 | } 24 | 25 | let pe = new Person(), 26 | st = new Student(), 27 | pr = new Professor(); 28 | 29 | let ro = new Robot(); 30 | let cat = { 31 | talk: function(){ 32 | console.log("Meow!"); 33 | } 34 | }; 35 | 36 | announce("Person", pe); 37 | announce("Student", st); 38 | announce("Professor", pr); 39 | announce("Robot", ro); 40 | announce("a cat", cat); 41 | announce("a number", 25); 42 | 43 | console.log("A table with different types"); 44 | let elements = [pe, st, pr, ro, cat, 25]; 45 | for (let i = 0; i < elements.length; i++){ 46 | try { 47 | elements[i].talk(); 48 | } 49 | catch(error){ 50 | console.log("The element n° ", i, " does not talk()"); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /codes/javascript/polymorphism/es6/type_manip.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | class Person { 4 | talk() { console.log("I am talking"); } 5 | } 6 | 7 | class Student extends Person { 8 | learn() { console.log("I am learning"); } 9 | } 10 | 11 | class Professor extends Person { 12 | constructor(){ super(); this.nbr = 5; } 13 | teach() { console.log("I am teaching"); } 14 | } 15 | 16 | function PhdStudent() { 17 | Object.assign(this, new Professor(), new Student()); 18 | } 19 | PhdStudent.prototype = Object.assign( 20 | Object.create(Professor.prototype), 21 | Object.create(Student.prototype) 22 | ); 23 | PhdStudent.prototype.constructor = PhdStudent; 24 | 25 | function testMembers(msg, obj) { 26 | process.stdout.write(msg + ": "); 27 | if (typeof obj.learn == "function") obj.learn(); 28 | else console.log("Method learn does not exist!"); 29 | 30 | process.stdout.write(msg + ": "); 31 | if (obj.hasOwnProperty("nbr")) console.log("Field nbr = " + obj.nbr); 32 | else console.log("Field nbr does not exist!"); 33 | } 34 | 35 | let pe = new Person(), 36 | st = new Student(), 37 | pr = new Professor(), 38 | phd = new PhdStudent(); 39 | 40 | 41 | console.log("=========================="); 42 | console.log("Object type"); 43 | console.log("=========================="); 44 | console.log("Typeof pe: " + (typeof pe) + ", class: " + (pe.constructor.name)); 45 | console.log("Typeof st: " + (typeof st) + ", class: " + (st.constructor.name)); 46 | console.log("Typeof pr: " + (typeof pr) + ", class: " + (pr.constructor.name)); 47 | console.log("Typeof phd: " + (typeof phd) + ", class: " + (phd.constructor.name)); 48 | 49 | console.log("=========================="); 50 | console.log("Instance of"); 51 | console.log("=========================="); 52 | console.log("pe instanceof Person: " + (pe instanceof Person)); //true 53 | console.log("st instanceof Person: " + (st instanceof Person)); //true 54 | console.log("st instanceof Student: " + (st instanceof Student)); //true 55 | console.log("st instanceof Professor: " + (st instanceof Professor)); //false 56 | console.log("pr instanceof Person: " + (pr instanceof Person)); //true 57 | console.log("pr instanceof Student: " + (pr instanceof Student)); //false 58 | console.log("pr instanceof Professor: " + (pr instanceof Professor)); //true 59 | console.log("phd instanceof Person: " + (phd instanceof Person)); //true 60 | console.log("phd instanceof PhdStudent: " + (phd instanceof PhdStudent)); //true 61 | console.log("phd instanceof Student: " + (phd instanceof Student)); //false 62 | console.log("phd instanceof Professor: " + (phd instanceof Professor)); //true 63 | 64 | console.log("=========================="); 65 | console.log("Member existance"); 66 | console.log("=========================="); 67 | testMembers("Person", pe); 68 | testMembers("Student", st); 69 | testMembers("rofessor", pr); 70 | testMembers("PhdStudent", phd); 71 | testMembers("25", 25); 72 | -------------------------------------------------------------------------------- /codes/lua/clsobj/objmeth.lua: -------------------------------------------------------------------------------- 1 | local Person = {} 2 | Person.__index = Person 3 | 4 | function Person:new(name, byear) -- The constructor 5 | return setmetatable({name = name, byear = byear}, Person) 6 | end 7 | 8 | function Person:__tostring() 9 | return "(" .. self.name .. ": " .. self.byear .. ")" 10 | end 11 | 12 | function Person.__eq (p1, p2) 13 | return p1.name == p2.name 14 | end 15 | 16 | function Person.__lt (p1, p2) 17 | return (p1.byear < p2.byear) or (p1.byear == p2.byear and p1.name < p2.name) 18 | end 19 | 20 | function Person.__le (p1, p2) 21 | return (p1.byear < p2.byear) or (p1.byear == p2.byear and p1.name <= p2.name) 22 | end 23 | 24 | local p = Person:new("Karim", 1986) 25 | local p2 = Person:new("Karim+1", 1986) 26 | local p3 = {name = "Karim", byear = 1987} 27 | 28 | print("As string: " .. tostring(p)) 29 | print("p == p3? " .. tostring(p==p3)) 30 | print("p ~= p3? " .. tostring(p~=p3)) 31 | print("p <= p2? " .. tostring(p <= p2)) 32 | print("p > p2? " .. tostring(p > p2)) 33 | print("p > p3? " .. tostring(p > p3)) 34 | -------------------------------------------------------------------------------- /codes/lua/clsobj/person.lua: -------------------------------------------------------------------------------- 1 | local Person = {} 2 | Person.__index = Person 3 | 4 | function Person.new(name, byear) -- The constructor 5 | Person.nbr = Person.nbr + 1 6 | return setmetatable({name = name, byear = byear}, Person) 7 | end 8 | 9 | Person.nbr = 0 10 | 11 | function Person:info() 12 | print("My name: " .. self.name .. ", My birth year: " .. self.byear) 13 | end 14 | 15 | function Person.population() 16 | return Person.nbr 17 | end 18 | 19 | local p = Person.new("Karim", 1986) 20 | local p2 = Person.new("Karim+1", 1987) 21 | 22 | p:info() 23 | p2.info(p2) 24 | local nbr = Person.population() 25 | 26 | print("The number of persons: " .. nbr) 27 | p2 = nil 28 | nbr = Person.population() 29 | print("The number of persons: " .. nbr) 30 | -------------------------------------------------------------------------------- /codes/lua/clsobj/person2.lua: -------------------------------------------------------------------------------- 1 | local Person = {} 2 | Person.__index = Person 3 | 4 | setmetatable(Person, { 5 | __call = function (cls, name, byear) 6 | Person.nbr = Person.nbr + 1 7 | return setmetatable({name = name, byear = byear}, Person) 8 | end, 9 | }) 10 | 11 | Person.nbr = 0 12 | 13 | function Person:info() 14 | print("My name: " .. self.name .. ", My birth year: " .. self.byear) 15 | end 16 | 17 | function Person.population() 18 | return Person.nbr 19 | end 20 | 21 | local p = Person("Karim", 1986) 22 | local p2 = Person("Karim+1", 1987) 23 | 24 | p:info() 25 | p2:info() 26 | local nbr = Person.population() 27 | 28 | print("The number of persons: " .. nbr) 29 | p2 = nil 30 | nbr = Person.population() 31 | print("The number of persons: " .. nbr) 32 | -------------------------------------------------------------------------------- /codes/lua/clsobj/property.lua: -------------------------------------------------------------------------------- 1 | local Rectangle = {} 2 | Rectangle.__index = function(self, key) 3 | local member = Rectangle[key] 4 | if member ~= nil then 5 | return member 6 | end 7 | if key == "w" then 8 | return self.width + 2 9 | elseif key == "h" then 10 | return self.height + 1 11 | end 12 | end 13 | 14 | Rectangle.__newindex = function(self, key, value) 15 | if key == "w" then 16 | if value < self.width then 17 | self.height = value 18 | else 19 | self.height = self.width 20 | self.width = value 21 | end 22 | end 23 | end 24 | 25 | 26 | function Rectangle.new() -- The constructor 27 | return setmetatable({width = 0, height = 0}, Rectangle) 28 | end 29 | 30 | function Rectangle:info() 31 | print("Width: " .. self.width .. ", Height: " .. self.height) 32 | end 33 | 34 | local r = Rectangle.new() 35 | r.w = 50 36 | r.w = 20 37 | r:info() 38 | print("w: " .. r.w .. ", h: " .. r.h) 39 | -------------------------------------------------------------------------------- /codes/lua/encapsulation/app.lua: -------------------------------------------------------------------------------- 1 | local Person = require "person" 2 | 3 | local p = Person.new("Karim_p") 4 | p:info() 5 | 6 | p.luckyNumber = 6 7 | -- print("name outside= " .. p.name) -- error: nil value 8 | p.name = "other name" 9 | p:info() 10 | -------------------------------------------------------------------------------- /codes/lua/encapsulation/person.lua: -------------------------------------------------------------------------------- 1 | local Person = {} 2 | Person.__index = Person 3 | 4 | function Person.new(name) -- The constructor 5 | local self = {} 6 | local _name = name; 7 | self.luckyNumber = 1; 8 | self.info1 = function() 9 | print("My name: " .. _name) 10 | end 11 | return setmetatable(self, Person) 12 | end 13 | 14 | function Person:info() 15 | self.info1() 16 | print("My lucky number: " .. self.luckyNumber) 17 | end 18 | 19 | return Person 20 | -------------------------------------------------------------------------------- /codes/lua/hello/entry.lua: -------------------------------------------------------------------------------- 1 | fact = function (i) 2 | if i <= 0 then return 1 end 3 | return i * fact(i-1) 4 | end 5 | 6 | if #arg < 1 then 7 | print ("Please enter an integer value") 8 | os.exit() 9 | end 10 | 11 | n = tonumber(arg[1]) 12 | print ("Fact(" .. n .. ")= " .. fact(n)) 13 | -------------------------------------------------------------------------------- /codes/lua/hello/except.lua: -------------------------------------------------------------------------------- 1 | function fact(i) 2 | if i < 0 then error ("Negative numbers don't have a factorial!") end 3 | if i > 5 then error "The number is too big!" end 4 | 5 | if i == 0 then return 1 end 6 | return i * fact(i-1) 7 | end 8 | 9 | if #arg < 1 then 10 | print ("Please enter an integer value") 11 | os.exit() 12 | end 13 | 14 | n = tonumber(arg[1]) 15 | success, f = pcall(fact, n) 16 | if success then 17 | print ("Fact(" .. n .. ")= " .. f) 18 | else 19 | print (f) 20 | end 21 | -------------------------------------------------------------------------------- /codes/lua/hello/func.lua: -------------------------------------------------------------------------------- 1 | function fact(i) 2 | if i <= 1 then 3 | return 1 4 | end 5 | return i * fact(i-1) 6 | end 7 | 8 | io.write("Please enter an integer value: ") 9 | n = io.read("*n") 10 | print ( "Fact(" .. n .. ")= " .. fact(n) ) 11 | -------------------------------------------------------------------------------- /codes/lua/hello/helloworld.lua: -------------------------------------------------------------------------------- 1 | print ("Hello world!") 2 | -------------------------------------------------------------------------------- /codes/lua/inheritance/abst/app.lua: -------------------------------------------------------------------------------- 1 | local Person = require "person" 2 | local Student = require "student" 3 | local Professor = require "professor" 4 | 5 | local pe = Person.new(); 6 | local st = Student.new(); 7 | local pr = Professor.new(); 8 | 9 | --pe:info(); -- method doesn't exist 10 | st:info(); 11 | -- pr:info(); -- cannot call abstract method 12 | -------------------------------------------------------------------------------- /codes/lua/inheritance/abst/person.lua: -------------------------------------------------------------------------------- 1 | local Person = {} 2 | Person.__index = Person 3 | 4 | function Person.new() -- The constructor 5 | local instance = {} 6 | --setmetatable(instance, Person) 7 | return instance 8 | end 9 | 10 | function Person:info() 11 | print("====INFO===="); 12 | self:info2() 13 | end 14 | 15 | function Person:info2() 16 | error("Cannot call abstract method") 17 | end 18 | 19 | return Person 20 | -------------------------------------------------------------------------------- /codes/lua/inheritance/abst/professor.lua: -------------------------------------------------------------------------------- 1 | local Person = require "person" 2 | 3 | local Professor = {} 4 | setmetatable(Professor, Person) -- Student extends Person 5 | Professor.__index = Professor 6 | 7 | function Professor.new() -- The constructor 8 | local instance = Person.new() 9 | setmetatable(instance, Professor) -- object extends Student 10 | return instance 11 | end 12 | 13 | return Professor 14 | -------------------------------------------------------------------------------- /codes/lua/inheritance/abst/student.lua: -------------------------------------------------------------------------------- 1 | local Person = require "person" 2 | 3 | local Student = {} 4 | setmetatable(Student, Person) -- Student extends Person 5 | Student.__index = Student 6 | 7 | function Student.new() -- The constructor 8 | local instance = Person.new() 9 | setmetatable(instance, Student) -- object extends Student 10 | return instance 11 | end 12 | 13 | function Student:info2() 14 | print("Student info2") 15 | end 16 | 17 | return Student 18 | -------------------------------------------------------------------------------- /codes/lua/inheritance/multiple/diamond.lua: -------------------------------------------------------------------------------- 1 | -- helper functions 2 | -- A function to merge objects from parents 3 | function get_object(parents) 4 | local object = {} 5 | for i=#parents, 1, -1 do 6 | local object2 = parents[i] 7 | for k,v in pairs(object2) do -- Merge the two objects 8 | object[k] = v 9 | end 10 | end 11 | return object 12 | end 13 | 14 | -- A function to return an index function based on parents 15 | function get_index(parents) 16 | return function(self, key) 17 | for i=1, #parents do 18 | member = parents[i][key] 19 | if member ~= nil then 20 | rawset(self, key, member) -- next time it will be its own key 21 | return member 22 | end 23 | end 24 | end 25 | end 26 | 27 | -- Person class 28 | local Person = {} 29 | Person.__index = Person 30 | 31 | function Person.new(name) -- The constructor 32 | local instance = {name = name} 33 | setmetatable(instance, Person) 34 | print("Person") 35 | return instance 36 | end 37 | 38 | function Person:info() 39 | print("my name is: " .. self.name) 40 | end 41 | 42 | -- Student class 43 | local Student = {} 44 | Student.__index = get_index({Student, Person}) 45 | 46 | function Student.new(name, mark) -- The constructor 47 | local instance = get_object({Person.new(name), {mark = mark}}) 48 | setmetatable(instance, Student) 49 | print("Student"); 50 | return instance 51 | end 52 | 53 | function Student:info() 54 | Person.info(self) 55 | print("my mark is: " .. self.mark) 56 | end 57 | 58 | -- Professor class 59 | local Professor = {} 60 | Professor.__index = get_index({Professor,Person}) 61 | 62 | function Professor.new(name, hours) -- The constructor 63 | local instance = get_object({Person.new(name), {hours=hours}}) 64 | setmetatable(instance, Professor) 65 | print("Professor"); 66 | return instance 67 | end 68 | 69 | -- PhdStudent class 70 | local PhdStudent = {} 71 | PhdStudent.__index = get_index({PhdStudent,Student, Professor}) 72 | 73 | function PhdStudent.new(name, hours, mark) -- The constructor 74 | local instance = get_object({Student.new(name, mark), Professor.new(name, hours)}) 75 | setmetatable(instance, PhdStudent) 76 | print("PhdStudent"); 77 | return instance 78 | end 79 | 80 | -- Main 81 | 82 | local a = Person.new("person1") 83 | a:info() 84 | print("=====================") 85 | local s = Student.new("student1", 15.5) 86 | s:info() 87 | print("=====================") 88 | local p = Professor.new("professor1", 8) 89 | p:info() 90 | print("=====================") 91 | local ps = PhdStudent.new("phdStudent1", 4, 12.5) 92 | ps:info() 93 | print("=====================") 94 | -------------------------------------------------------------------------------- /codes/lua/inheritance/multiple/multiple1.lua: -------------------------------------------------------------------------------- 1 | -- Person class 2 | local Person = {} 3 | Person.__index = Person 4 | 5 | function Person.new() -- The constructor 6 | local instance = {} 7 | setmetatable(instance, Person) 8 | print("I am a person"); 9 | return instance 10 | end 11 | 12 | function Person:talk() 13 | print("I can talk") 14 | end 15 | 16 | -- Machine class 17 | local Machine = {} 18 | Machine.__index = Machine 19 | 20 | function Machine.new() -- The constructor 21 | local instance = {} 22 | setmetatable(instance, Machine) 23 | print("I am a machine"); 24 | return instance 25 | end 26 | 27 | function Machine:analyse() 28 | print("I can analyse") 29 | end 30 | 31 | -- ... 32 | -- Index function 33 | function get_index(parents) 34 | return function(self, key) 35 | for i=1, #parents do 36 | local member = parents[i][key] 37 | if member ~= nil then 38 | rawset(self, key, member) -- next time it will be its own key 39 | return member 40 | end 41 | end 42 | end 43 | end 44 | 45 | -- Android class 46 | local Android = {} 47 | Android.__index = get_index({Android, Machine, Person}) 48 | 49 | function Android.new() -- The constructor 50 | local instance2 = Machine.new() -- first parent 51 | local instance = Person.new() -- second parent 52 | for k,v in pairs(instance2) do -- Merge the two objects 53 | instance[k] = v 54 | end 55 | setmetatable(instance, Android) 56 | print("I am an android"); 57 | return instance 58 | end 59 | 60 | -- Cyborg class 61 | local Cyborg = {} 62 | Cyborg.__index = get_index({Cyborg, Person, Machine}) 63 | 64 | function Cyborg.new() -- The constructor 65 | local instance2 = Person.new() -- first parent 66 | local instance = Machine.new() -- second parent 67 | for k,v in pairs(instance2) do -- Merge the two objects 68 | instance[k] = v 69 | end 70 | setmetatable(instance, Cyborg) 71 | print("I am a cyborg"); 72 | return instance 73 | end 74 | 75 | -- Main 76 | 77 | local p = Person.new() 78 | p:talk() 79 | print("=====================") 80 | local m = Machine.new() 81 | m:analyse() 82 | print("=====================") 83 | local a = Android.new() 84 | a:talk() 85 | a:analyse() 86 | print("=====================") 87 | local c = Cyborg.new() 88 | c:talk() 89 | c:analyse() 90 | print("=====================") 91 | -------------------------------------------------------------------------------- /codes/lua/inheritance/multiple/multiple2.lua: -------------------------------------------------------------------------------- 1 | -- helper functions 2 | -- A function to merge objects from parents 3 | function get_object(parents) 4 | local object = {} 5 | for i=#parents, 1, -1 do 6 | local object2 = parents[i] 7 | for k,v in pairs(object2) do -- Merge the two objects 8 | object[k] = v 9 | end 10 | end 11 | return object 12 | end 13 | 14 | -- A function to return an index function based on parents 15 | function get_index(parents) 16 | return function(self, key) 17 | for i=1, #parents do 18 | local member = parents[i][key] 19 | if member ~= nil then 20 | rawset(self, key, member) -- next time it will be its own key 21 | return member 22 | end 23 | end 24 | end 25 | end 26 | 27 | -- Person class 28 | local Person = {} 29 | Person.__index = Person 30 | 31 | function Person.new(name) -- The constructor 32 | local instance = { 33 | name = name, 34 | serial = 5 35 | } 36 | setmetatable(instance, Person) 37 | return instance 38 | end 39 | 40 | function Person:say_name() 41 | print("my name is: " .. self.name) 42 | end 43 | 44 | function Person:say_serial() 45 | print("my serial is: " .. self.serial) 46 | end 47 | 48 | -- Machine class 49 | local Machine = {} 50 | Machine.__index = Machine 51 | 52 | function Machine.new(serial) -- The constructor 53 | local instance = {serial = serial} 54 | setmetatable(instance, Machine) 55 | return instance 56 | end 57 | 58 | function Machine:say_serial() 59 | print("my serial is: " .. self.serial) 60 | end 61 | 62 | -- Android class 63 | local Android = {} 64 | Android.__index = get_index({Android, Machine, Person}) 65 | 66 | function Android.new(name, serial) -- The constructor 67 | local instance = get_object({Machine.new(serial), Person.new(name)}) 68 | setmetatable(instance, Android) 69 | return instance 70 | end 71 | 72 | -- Cyborg class 73 | local Cyborg = {} 74 | Cyborg.__index = get_index({Cyborg, Person, Machine}) 75 | 76 | function Cyborg.new(name, serial) -- The constructor 77 | local instance = get_object({Person.new(name), Machine.new(serial)}) 78 | setmetatable(instance, Cyborg) 79 | print("I am a cyborg"); 80 | return instance 81 | end 82 | 83 | -- Main 84 | 85 | local p = Person.new("person") 86 | p:say_name() 87 | p:say_serial() 88 | print("=====================") 89 | local m = Machine.new(15) 90 | m:say_serial() 91 | print("=====================") 92 | local a = Android.new("android", 16) 93 | a:say_name() 94 | a:say_serial() 95 | print("=====================") 96 | local c = Cyborg.new("cyborg", 17) 97 | c:say_name() 98 | c:say_serial() 99 | print("=====================") 100 | -------------------------------------------------------------------------------- /codes/lua/inheritance/single/app.lua: -------------------------------------------------------------------------------- 1 | local Person = require "person" 2 | local Student = require "student" 3 | local Professor = require "professor" 4 | 5 | local pe = Person.new("person1"); 6 | local st = Student.new("student1", 15); 7 | local pr = Professor.new(); 8 | local pr2 = Professor.new{name="professor2", luckyNumber = 13}; 9 | 10 | pe.luckyNumber = 10; 11 | st.luckyNumber = 20; 12 | 13 | pe:info(); 14 | st:info(); 15 | pr:info(); 16 | pr2:info(); 17 | -------------------------------------------------------------------------------- /codes/lua/inheritance/single/const_inherit.lua: -------------------------------------------------------------------------------- 1 | local Person = {luckyNumber = 0} 2 | Person.__index = Person 3 | 4 | function Person:new(name) -- The constructor 5 | local instance = {name = name} 6 | setmetatable(instance, self) 7 | return instance 8 | end 9 | 10 | function Person:info() 11 | print("My name: " .. self.name); 12 | end 13 | 14 | local Student = {} 15 | setmetatable(Student, Person) -- Student extends Person 16 | Student.__index = Student 17 | 18 | local pe = Person:new("person1"); 19 | local st = Student:new("student1"); 20 | 21 | pe:info(); 22 | st:info(); 23 | 24 | print(getmetatable(pe)) 25 | print(getmetatable(st)) 26 | 27 | 28 | -------------------------------------------------------------------------------- /codes/lua/inheritance/single/person.lua: -------------------------------------------------------------------------------- 1 | local Person = {luckyNumber = 0} 2 | Person.__index = Person 3 | 4 | function Person.new(name) -- The constructor 5 | local instance = {name = name} 6 | setmetatable(instance, Person) 7 | return instance 8 | end 9 | 10 | function Person:info() 11 | print("My name: " .. self.name); 12 | print("My lucky number: " .. self.luckyNumber) 13 | end 14 | 15 | return Person 16 | -------------------------------------------------------------------------------- /codes/lua/inheritance/single/professor.lua: -------------------------------------------------------------------------------- 1 | local Person = require "person" 2 | 3 | local Professor = {} 4 | setmetatable(Professor, Person) -- Professor extends Person 5 | Professor.__index = Professor 6 | 7 | function Professor.new(instance) -- The constructor 8 | instance = instance or {name = "professor"} 9 | setmetatable(instance, Professor) 10 | return instance 11 | end 12 | 13 | return Professor 14 | -------------------------------------------------------------------------------- /codes/lua/inheritance/single/student.lua: -------------------------------------------------------------------------------- 1 | local Person = require "person" 2 | 3 | local Student = {} 4 | setmetatable(Student, Person) -- Student extends Person 5 | Student.__index = Student 6 | 7 | function Student.new(name, grade) -- The constructor 8 | local instance = Person.new(name) --get the superclass's object 9 | instance.grade = grade 10 | setmetatable(instance, Student) -- object extends Student 11 | return instance 12 | end 13 | 14 | function Student:info() 15 | Person.info(self) 16 | print("My grade: " .. self.grade) 17 | end 18 | 19 | return Student 20 | -------------------------------------------------------------------------------- /codes/lua/polymorphism/overloading.lua: -------------------------------------------------------------------------------- 1 | -- Person class 2 | local Person = {} 3 | Person.__index = Person 4 | 5 | function Person:new() -- The constructor 6 | local instance = {} 7 | setmetatable(instance, self) --self, here, will refer to the calling class 8 | return instance 9 | end 10 | 11 | function Person:read(...) 12 | local arg1 = select(1, ...) 13 | if not arg1 then -- select("#", ...) for parameters number 14 | print("I am reading") 15 | else 16 | if type(arg1) == "string" then 17 | print("A text: " .. arg1) 18 | end 19 | end 20 | end 21 | 22 | -- Student class 23 | local Student = {} 24 | Student.__index = Student 25 | setmetatable(Student, Person) -- extends 26 | 27 | function Student:read(...) 28 | local args = {...} 29 | if type(args[1]) == "number" then 30 | print("I read on table n°: " .. args[1]) 31 | else 32 | Person.read(self, ...) 33 | end 34 | end 35 | 36 | print("PERSON"); 37 | local pe = Person:new() 38 | pe:read() 39 | pe:read("I am a person") 40 | 41 | print("STUDENT"); 42 | local st = Student:new() 43 | st:read() 44 | st:read("I am a student") 45 | st:read(5) 46 | -------------------------------------------------------------------------------- /codes/lua/polymorphism/overriding.lua: -------------------------------------------------------------------------------- 1 | -- Person class 2 | local Person = {} 3 | Person.__index = Person 4 | 5 | function Person:new() -- The constructor 6 | local instance = {} 7 | setmetatable(instance, self) --self, here, will refer to the calling class 8 | return instance 9 | end 10 | 11 | function Person:talk() 12 | print("I am a person") 13 | end 14 | 15 | -- Student class 16 | local Student = {} 17 | Student.__index = Student 18 | setmetatable(Student, Person) -- extends 19 | 20 | function Student:talk() 21 | print("I am a student") 22 | end 23 | 24 | local pe = Person:new() 25 | local st = Student:new() 26 | pe:talk() -- I am a person 27 | st:talk() -- I am a student 28 | -------------------------------------------------------------------------------- /codes/lua/polymorphism/subtype.lua: -------------------------------------------------------------------------------- 1 | -- Person class 2 | local Person = {} 3 | Person.__index = Person 4 | 5 | function Person:new() -- The constructor 6 | local instance = {} 7 | setmetatable(instance, self) --self, here, will refer to the calling class 8 | return instance 9 | end 10 | 11 | function Person:talk() 12 | print("I am a person") 13 | end 14 | 15 | -- Student class 16 | local Student = {} 17 | Student.__index = Student 18 | setmetatable(Student, Person) -- extends 19 | 20 | -- Professor class 21 | local Professor = {} 22 | Professor.__index = Professor 23 | setmetatable(Professor, Person) -- extends 24 | 25 | function announce(msg, talker) 26 | io.write(msg .. ": ") 27 | if type(talker) == "table" and type(talker.talk) == "function" then 28 | talker:talk() 29 | else 30 | print("Sorry! I do not talk!") 31 | end 32 | end 33 | 34 | local pe = Person:new() 35 | local st = Student:new() 36 | local pr = Professor:new() 37 | 38 | local cat = { 39 | talk = function(); print("Meow!"); end 40 | } 41 | 42 | announce("Person", pe) 43 | announce("Student", st) 44 | announce("Professor", pr) 45 | announce("a cat", cat) 46 | announce("a number", 25) 47 | 48 | print("A table with different types") 49 | local elements = {pe, st, pr, cat, 25} 50 | for i =1, #elements do 51 | success, res = pcall(function() elements[i].talk(); end) 52 | if not success then 53 | print("The element n°" .. i .. " does not talk()") 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /codes/perl/clsobj/objmeth.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | 3 | { 4 | package Person; 5 | 6 | sub new { 7 | my $class = shift; 8 | my $self = { 9 | _name => shift, 10 | _byear => shift, 11 | }; 12 | bless $self, $class; 13 | return $self; 14 | } 15 | 16 | use overload 17 | '""' => sub { 18 | my ($self) = @_; 19 | return "($self->{_name}: $self->{_byear})" 20 | }, 21 | '==' => sub { 22 | my ($self, $other) = @_; 23 | return ($self->{_name} eq $other->{_name}) 24 | } 25 | 26 | } 27 | 28 | $p = new Person("Karim", 1986); 29 | $p2 = new Person("Karim+1", 1986); 30 | $p3 = { 31 | _name => "Karim", 32 | _byear => 1987, 33 | }; 34 | 35 | sub boolval { 36 | my $bool = shift; 37 | return $bool? "true": "false"; 38 | } 39 | print "As string: $p\n"; 40 | print "As string: " . $p2 . "\n"; 41 | print "p == p2: " . boolval($p == $p2) . "\n"; 42 | print "p == p3: " . boolval($p == $p3) . "\n"; 43 | -------------------------------------------------------------------------------- /codes/perl/clsobj/person.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | 3 | { 4 | package Person; 5 | 6 | my $nbr = 0; 7 | 8 | sub new { 9 | my $class = shift; 10 | my $self = { 11 | _name => shift, 12 | _byear => shift, 13 | }; 14 | $nbr++; 15 | bless $self, $class; 16 | return $self; 17 | } 18 | 19 | sub DESTROY { 20 | my ($self) = @_; 21 | $nbr--; 22 | print "$self->{_name} is out\n"; 23 | } 24 | 25 | sub info { 26 | my( $self ) = @_; 27 | print "My name: $self->{_name}, My birth year: $self->{_byear}\n"; 28 | } 29 | 30 | sub population { 31 | return $nbr; 32 | } 33 | 34 | } 35 | 36 | $p = new Person("Karim", 1986); 37 | $p2 = Person->new("Karim+1", 1987); 38 | 39 | $p->info(); 40 | $p2->info(); 41 | $nbr = Person::population(); 42 | 43 | print "The number of persons: $nbr\n"; 44 | undef($p2); 45 | $nbr = Person::population(); 46 | print "The number of persons: $nbr\n"; 47 | -------------------------------------------------------------------------------- /codes/perl/clsobj/property.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | 3 | package Rectangle; 4 | 5 | sub new { 6 | my $class = shift; 7 | my $self = { 8 | _width => 0, 9 | _height => 0, 10 | }; 11 | bless $self, $class; 12 | return $self; 13 | } 14 | 15 | sub w { 16 | my $self = shift; 17 | if (my $value = shift) { # setter 18 | if ($value < $self->{_width}) { 19 | $self->{_height} = $value; 20 | } else { 21 | $self->{_height} = $self->{_width}; 22 | $self->{_width} = $value; 23 | } 24 | return $self; 25 | } else { # getter 26 | return $self->{_width} + 2; 27 | } 28 | } 29 | 30 | sub h { 31 | my $self = shift; 32 | return $self->{_height} + 1; 33 | } 34 | 35 | sub info { 36 | my( $self ) = @_; 37 | print "Width: $self->{_width}, Height: $self->{_height}\n"; 38 | } 39 | 40 | 1; 41 | 42 | $r = new Rectangle(); 43 | $r->w(50); 44 | $r->w(20); 45 | $r->info(); 46 | print "w: " . $r->w . ", h: " . $r->h . "\n"; 47 | -------------------------------------------------------------------------------- /codes/perl/encapsulation/Person.pm: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | sub new { 4 | my $class = shift; 5 | my $self = { 6 | name => shift, 7 | t => "person" 8 | }; 9 | 10 | my $closure = sub { 11 | die "cannot access fields outside the class" unless caller eq __PACKAGE__; 12 | my ($field, $value) = @_; 13 | die "no field called: $field" unless exists $self->{$field}; 14 | if ($value) {#setter 15 | $self->{$field} = $value; 16 | } 17 | return $self->{$field};#getter 18 | }; 19 | 20 | bless $closure, $class; 21 | return $closure; 22 | } 23 | 24 | sub t { 25 | die "t is protected!" unless caller->isa(__PACKAGE__); 26 | my($self, $value )= @_; 27 | return $self->("t", $value); 28 | } 29 | 30 | sub _info_private { 31 | die "_info_private is private!" unless caller eq __PACKAGE__; 32 | my( $self ) = @_; 33 | print "My name: ",$self->("name"),"\n"; 34 | } 35 | 36 | sub _info_protected { 37 | die "_info_protected is protected!" unless caller->isa(__PACKAGE__); 38 | my( $self ) = @_; 39 | print "I am a: ",$self->("t"),"\n"; 40 | } 41 | 42 | sub info { 43 | my( $self ) = @_; 44 | _info_private(@_); 45 | _info_protected(@_); 46 | print "----------------------------\n"; 47 | } 48 | 49 | 1; 50 | -------------------------------------------------------------------------------- /codes/perl/encapsulation/Student.pm: -------------------------------------------------------------------------------- 1 | package Student; 2 | use Person; 3 | our @ISA = qw(Person); 4 | 5 | sub new { 6 | my ($class, @args) = @_; 7 | $self = $class->SUPER::new(@args); 8 | #$self->{t} = "student";#error the object is not a HASH 9 | #$self->("t", "student");#limited the direct access 10 | $self->t("student"); 11 | bless $self, $class; 12 | return $self; 13 | } 14 | 15 | sub use_protected { 16 | my( $self, @args ) = @_; 17 | # $self->_info_private(@args); #_info_private is private! 18 | $self->_info_protected(@args); 19 | } 20 | 21 | 1; 22 | -------------------------------------------------------------------------------- /codes/perl/encapsulation/app.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | use Person; 3 | use Student; 4 | 5 | $p = new Person("Karim_p"); 6 | $s = Student->new("Karim_s"); 7 | 8 | $p->info(); 9 | # $p->_info_private(); #_info_private is private! 10 | # $p->_info_protected(); #_info_protected is protected! 11 | 12 | $p->info(); 13 | $s->info(); 14 | $s->use_protected(); 15 | -------------------------------------------------------------------------------- /codes/perl/encapsulation/private.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | { 3 | package Person; 4 | use Scalar::Util 'refaddr'; 5 | # ... 6 | my %private; 7 | my %protected; 8 | 9 | sub new { 10 | my $class = shift; 11 | my $self = {};#no public members 12 | bless $self, $class; 13 | 14 | $private{refaddr $self} = { 15 | name => shift 16 | }; 17 | 18 | $protected{refaddr $self} = { 19 | t => "person" 20 | }; 21 | 22 | return $self; 23 | } 24 | 25 | sub name { 26 | die "name is private!" unless caller eq __PACKAGE__; 27 | my $self = shift; 28 | if (my $value = shift) { # setter 29 | $private{refaddr $self}->{name} = $value; 30 | } 31 | return $private{refaddr $self}->{name}; #getter 32 | } 33 | 34 | sub t { 35 | die "t is protected!" unless caller->isa(__PACKAGE__); 36 | my $self = shift; 37 | if (my $value = shift) { # setter 38 | $protected{refaddr $self}->{t} = $value; 39 | } 40 | return $protected{refaddr $self}->{t}; #getter 41 | } 42 | 43 | sub copy { 44 | my( $self, $other ) = @_; 45 | $self->name($other->name); 46 | $self->t($other->t); 47 | } 48 | 49 | sub info { 50 | my( $self ) = @_; 51 | print "My name: ", $self->name,"\n"; 52 | print "I am a: ", $self->t,"\n"; 53 | print "----------------------------\n"; 54 | } 55 | 56 | 1; 57 | } 58 | 59 | # ============================================ 60 | # Student begin 61 | # ============================================ 62 | { 63 | package Student; 64 | our @ISA = qw(Person); 65 | 66 | sub new { 67 | my ($class, @args) = @_; 68 | $self = $class->SUPER::new(@args); 69 | #$self->name("subclass name");#name is private! 70 | $self->t("student"); 71 | bless $self, $class; 72 | return $self; 73 | } 74 | 75 | } 76 | 77 | # ============================================ 78 | # Main begin 79 | # ============================================ 80 | 81 | $p = new Person("Person1"); 82 | #$p->name("other name");#name is private! 83 | #$p->t("other type");#t is protected! 84 | $p2 = new Person("Person2"); 85 | $p3 = new Person("Person3"); 86 | 87 | $s = new Student("Student1"); 88 | $s2 = new Student("Student2"); 89 | $s3 = new Student("Student3"); 90 | 91 | $p->info(); 92 | $p2->info(); 93 | $p3->copy($s2); 94 | $p3->info(); 95 | 96 | $s->info(); 97 | $s2->info(); 98 | $s3->info(); 99 | -------------------------------------------------------------------------------- /codes/perl/encapsulation/public.pl: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | sub new { 4 | my $class = shift; 5 | my $self = { 6 | name => shift, 7 | }; 8 | bless $self, $class; 9 | return $self; 10 | } 11 | 12 | sub info { 13 | my( $self ) = @_; 14 | print "My name: ", $self->{name},"\n"; 15 | print "----------------------------\n"; 16 | } 17 | 18 | 1; 19 | 20 | $p = new Person("Karim_p"); 21 | $p->info(); 22 | $p->{name} = "other name"; 23 | $p->info(); 24 | -------------------------------------------------------------------------------- /codes/perl/hello/entry.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | 3 | my $n = $ARGV[0]; 4 | die "Please enter an integer value" unless (defined $n); 5 | 6 | print "Fact($n)= ${\fact($n)}\n"; 7 | print "Fact(" . $n . ")= ". fact($n) . "\n"; 8 | 9 | sub fact { 10 | my $i = shift; 11 | return 1 if ($i <= 0); 12 | return $i * fact($i - 1); 13 | } 14 | -------------------------------------------------------------------------------- /codes/perl/hello/except.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | 3 | my $n = $ARGV[0]; 4 | die "Please enter an integer value" unless (defined $n); 5 | 6 | eval { 7 | my $f = fact($n); 8 | print "Fact($n)= $f\n"; 9 | }; 10 | if ($@) { 11 | print "The error: $@\n"; 12 | }; 13 | 14 | sub fact { 15 | my $i = shift; 16 | die("Negative numbers don't have a factorial!") if ($i < 0); 17 | die "The number is too big!" if ($i > 5); 18 | 19 | return 1 if ($i == 0); 20 | return $i * fact($i - 1); 21 | } 22 | -------------------------------------------------------------------------------- /codes/perl/hello/func.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | print "Please enter an integer value: "; 3 | chomp($n = <>); 4 | $f = fact($n); 5 | print "Fact($n)= $f\n"; 6 | 7 | sub fact { 8 | my $i = shift; 9 | if ($i <= 1){ 10 | return 1; 11 | } 12 | return $i * fact($i - 1); 13 | } 14 | -------------------------------------------------------------------------------- /codes/perl/hello/helloworld.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | print "Hello world!\n"; -------------------------------------------------------------------------------- /codes/perl/inheritance/abst/Person.pm: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | sub new { 4 | die "cannot be instantiated" unless caller->isa(__PACKAGE__); 5 | my $class = shift; 6 | my $self = {}; #fields 7 | bless $self, $class; 8 | return $self; 9 | } 10 | 11 | sub info { 12 | my( $self ) = @_; 13 | print "====INFO====\n"; 14 | $self->info2(); 15 | } 16 | 17 | sub info2 { 18 | die "abstract method" 19 | } 20 | 21 | 1; 22 | -------------------------------------------------------------------------------- /codes/perl/inheritance/abst/Professor.pm: -------------------------------------------------------------------------------- 1 | package Professor; 2 | use Person; 3 | our @ISA = qw(Person); 4 | 5 | sub new { 6 | my ($class, @args) = @_; 7 | $self = $class->SUPER::new(@args); 8 | bless $self, $class; 9 | return $self; 10 | } 11 | 12 | 1; 13 | -------------------------------------------------------------------------------- /codes/perl/inheritance/abst/Student.pm: -------------------------------------------------------------------------------- 1 | package Student; 2 | use Person; 3 | our @ISA = qw(Person); 4 | 5 | sub new { 6 | my ($class, @args) = @_; 7 | $self = $class->SUPER::new(@args); 8 | bless $self, $class; 9 | return $self; 10 | } 11 | 12 | sub info2 { 13 | print "Student info2\n"; 14 | } 15 | 16 | 1; 17 | -------------------------------------------------------------------------------- /codes/perl/inheritance/abst/app.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | use Person; 3 | use Student; 4 | use Professor; 5 | 6 | #$pe = new Person();# cannot be instantiated 7 | $st = Student->new(); 8 | #$pr = Professor->new();#cannot be instantiated 9 | 10 | $st->info(); 11 | -------------------------------------------------------------------------------- /codes/perl/inheritance/fin/finalcls.pl: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | sub new { 4 | my $class = shift; 5 | die "final class" unless $class eq __PACKAGE__; 6 | my $self = {}; #fields 7 | bless $self, $class; 8 | return $self; 9 | } 10 | 11 | 1; 12 | 13 | package Student; 14 | our @ISA = qw(Person); 15 | 16 | sub new { 17 | my ($class, @args) = @_; 18 | $self = $class->SUPER::new(@args); 19 | bless $self, $class; 20 | return $self; 21 | } 22 | 23 | 1; 24 | 25 | $pe = new Person(); 26 | $st = new Student();# final class 27 | 28 | # didn't find this in any reference 29 | # since I'm not a Perl expert, 30 | # I don't know its limits 31 | -------------------------------------------------------------------------------- /codes/perl/inheritance/multiple/diamond.pl: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | sub new { 4 | my $class = shift; 5 | my $self = {name => shift}; 6 | bless $self, $class; 7 | print "$class\n"; 8 | return $self; 9 | } 10 | 11 | sub info { 12 | my ($self) = @_; 13 | print "Person name: $self->{name}\n"; 14 | } 15 | 16 | 1; 17 | 18 | package Student; 19 | our @ISA = qw(Person); 20 | 21 | sub new { 22 | my $class = shift; 23 | my $person = $class->Person::new(shift); 24 | my $self = { 25 | %$person, 26 | mark => shift 27 | }; 28 | bless $self, $class; 29 | return $self; 30 | } 31 | 32 | sub info { 33 | my ($self) = @_; 34 | Person::info($self); 35 | print "Student mark: $self->{mark}\n"; 36 | } 37 | 38 | 1; 39 | 40 | package Professor; 41 | our @ISA = qw(Person); 42 | 43 | sub new { 44 | my $class = shift; 45 | my $person = $class->Person::new(shift); 46 | my $self = { 47 | %$person, 48 | hours => shift 49 | }; 50 | bless $self, $class; 51 | return $self; 52 | } 53 | 54 | 1; 55 | 56 | package PhdStudent; 57 | our @ISA = qw(Student Professor); 58 | 59 | sub new { 60 | my ($class, $name, $hours, $mark) = @_; 61 | my $student = $class->Student::new($name, $mark); 62 | my $professor = $class->Professor::new($name, $hours); 63 | my $self = {%$student, %$professor}; 64 | bless $self, $class; 65 | return $self; 66 | } 67 | 68 | sub info { 69 | my ($self) = @_; 70 | $self->Student::info(); 71 | $self->Professor::info(); 72 | } 73 | 74 | 1; 75 | 76 | $a = Person->new("person1"); 77 | $a->info(); 78 | print "=====================\n"; 79 | $s = Student->new("student1", 15.5); 80 | $s->info(); 81 | print "=====================\n"; 82 | $p = Professor->new("professor1", 8); 83 | $p->info(); 84 | print "=====================\n"; 85 | $ps = PhdStudent->new("phdStudent1", 4, 12.5); 86 | $ps->info(); 87 | print "=====================\n"; 88 | -------------------------------------------------------------------------------- /codes/perl/inheritance/multiple/multiple1.pl: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | sub new { 4 | my $class = shift; 5 | my $self = {}; 6 | bless $self, $class; 7 | print "I am a person\n"; 8 | return $self; 9 | } 10 | 11 | 1; 12 | 13 | package Machine; 14 | 15 | sub new { 16 | my $class = shift; 17 | my $self = {}; 18 | bless $self, $class; 19 | print "I am a machine\n"; 20 | return $self; 21 | } 22 | 23 | 1; 24 | 25 | package Android; 26 | our @ISA = qw(Machine Person); 27 | 28 | sub new { 29 | my $class = shift; 30 | my $self = $class->Person::new(); 31 | my $self2 = $class->Machine::new(); 32 | $self = { %$self, %$self2}; 33 | bless $self, $class; 34 | print "So, I am an android\n"; 35 | return $self; 36 | } 37 | 38 | 1; 39 | 40 | package Cyborg; 41 | our @ISA = qw(Person Machine); 42 | 43 | sub new { 44 | my $class = shift; 45 | my $self = $class->Machine::new(); 46 | my $self2 = $class->Person::new(); 47 | $self = { %$self, %$self2}; 48 | bless $self, $class; 49 | print "So, I am a cyborg\n"; 50 | return $self; 51 | } 52 | 53 | 1; 54 | 55 | $p = Person->new(); 56 | print "=====================\n"; 57 | $m = Machine->new(); 58 | print "=====================\n"; 59 | $a = Android->new(); 60 | print "=====================\n"; 61 | $c = Cyborg->new(); 62 | print "=====================\n"; 63 | -------------------------------------------------------------------------------- /codes/perl/inheritance/multiple/multiple2.pl: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | sub new { 4 | my $class = shift; 5 | my $self = { 6 | name => shift, 7 | serial => 5 8 | }; 9 | bless $self, $class; 10 | return $self; 11 | } 12 | 13 | sub info { 14 | my ($self) = @_; 15 | print "Person name: $self->{name}\n"; 16 | print "Person serial: $self->{serial}\n"; 17 | } 18 | 19 | 1; 20 | 21 | package Machine; 22 | 23 | sub new { 24 | my $class = shift; 25 | my $self = {serial => shift}; 26 | bless $self, $class; 27 | return $self; 28 | } 29 | 30 | sub info { 31 | my ($self) = @_; 32 | print "Machine serial: $self->{serial}\n"; 33 | } 34 | 35 | 1; 36 | 37 | package Android; 38 | our @ISA = qw(Machine Person); 39 | 40 | sub new { 41 | my ($class, $name, $serial) = @_; 42 | my $self = $class->Person::new($name); 43 | my $self2 = $class->Machine::new($serial); 44 | $self = { %$self, %$self2}; 45 | bless $self, $class; 46 | return $self; 47 | } 48 | 49 | 1; 50 | 51 | package Cyborg; 52 | our @ISA = qw(Person Machine); 53 | 54 | sub new { 55 | my ($class, $name, $serial) = @_; 56 | my $self = $class->Machine::new($serial); 57 | my $self2 = $class->Person::new($name); 58 | $self = { %$self, %$self2}; 59 | bless $self, $class; 60 | return $self; 61 | } 62 | 63 | 1; 64 | 65 | $p = Person->new("person"); 66 | $p->info(); 67 | print "=====================\n"; 68 | $m = Machine->new(15); 69 | $m->info(); 70 | print "=====================\n"; 71 | $a = Android->new("android", 17); 72 | $a->info(); 73 | print "=====================\n"; 74 | $c = Cyborg->new("cyborg", 20); 75 | $c->info(); 76 | print "=====================\n"; 77 | -------------------------------------------------------------------------------- /codes/perl/inheritance/single/Person.pm: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | sub new { 4 | my $class = shift; 5 | my $self = { 6 | name => shift, 7 | luckyNumber => 0 8 | }; 9 | bless $self, $class; 10 | return $self; 11 | } 12 | 13 | sub info { 14 | my( $self ) = @_; 15 | print "My name: $self->{name}\n"; 16 | print "My luckyNumber: $self->{luckyNumber}\n"; 17 | } 18 | 19 | 1; 20 | -------------------------------------------------------------------------------- /codes/perl/inheritance/single/Professor.pm: -------------------------------------------------------------------------------- 1 | package Professor; 2 | use Person; 3 | our @ISA = qw(Person); 4 | 5 | 1; 6 | -------------------------------------------------------------------------------- /codes/perl/inheritance/single/Student.pm: -------------------------------------------------------------------------------- 1 | package Student; 2 | use Person; 3 | our @ISA = qw(Person); 4 | 5 | sub new { 6 | my ($class, $name, $grade) = @_; 7 | $self = $class->SUPER::new($name); 8 | $self->{grade} = $grade; 9 | bless $self, $class; 10 | return $self; 11 | } 12 | 13 | sub info { 14 | my( $self ) = @_; 15 | $self->SUPER::info(); 16 | print "My grade: $self->{grade}\n"; 17 | } 18 | 19 | 1; 20 | -------------------------------------------------------------------------------- /codes/perl/inheritance/single/app.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/perl -w 2 | use Person; 3 | use Student; 4 | use Professor; 5 | 6 | $pe = new Person("person1"); 7 | $st = Student->new("student1", 15); 8 | $pr = Professor->new("professor1"); 9 | 10 | $pe->{luckyNumber} = 10; 11 | $st->{luckyNumber} = 20; 12 | $pr->{luckyNumber} = 30; 13 | 14 | $pe->info(); 15 | $st->info(); 16 | $pr->info(); 17 | -------------------------------------------------------------------------------- /codes/perl/polymorphism/overloading.pl: -------------------------------------------------------------------------------- 1 | package Person; 2 | sub new { return bless {}, shift; } 3 | sub read { 4 | if (@_< 2 ) { 5 | print "I am reading\n"; 6 | return; 7 | } 8 | my ($self, $arg1) = @_; 9 | print "I am reading: $arg1\n"; 10 | } 11 | 1; 12 | 13 | package Student; 14 | our @ISA = qw(Person); 15 | sub read { 16 | if (@_ == 3) { 17 | my ($self, $arg1, $arg2) = @_; 18 | print "I am reading: $arg1 for $arg2 times\n"; 19 | } 20 | else { 21 | $self = shift; 22 | $self->SUPER::read(@_); 23 | } 24 | } 25 | 1; 26 | 27 | print "PERSON\n"; 28 | $pe = Person->new(); 29 | $pe->read(); 30 | $pe->read("Book"); 31 | $pe->read("Book", 5); 32 | 33 | print "STUDENT\n"; 34 | $st = Student->new(); 35 | $st->read(); 36 | $st->read("Book"); 37 | $st->read("Book", 5); 38 | -------------------------------------------------------------------------------- /codes/perl/polymorphism/overriding.pl: -------------------------------------------------------------------------------- 1 | package Person; 2 | sub new { return bless {}, shift; } 3 | sub talk { print "I am a person\n"; } 4 | 1; 5 | 6 | package Student; 7 | our @ISA = qw(Person); 8 | sub talk { print "I am a student\n"; } 9 | 1; 10 | 11 | $pe = Person->new(); 12 | $st = Student->new(); 13 | $pe->talk(); # I am a person 14 | $st->talk(); # I am a student 15 | -------------------------------------------------------------------------------- /codes/perl/polymorphism/subtype.pl: -------------------------------------------------------------------------------- 1 | package Person; 2 | sub new { return bless {}, shift; } 3 | sub talk { print "I am a person\n"; } 4 | 1; 5 | 6 | package Student; 7 | our @ISA = qw(Person); 8 | 1; 9 | 10 | package Professor; 11 | our @ISA = qw(Person); 12 | 1; 13 | 14 | package Robot; 15 | sub new { return bless {}, Robot; } 16 | sub talk { print "I am a robot\n";} 17 | 1; 18 | 19 | sub announce { 20 | my ($msg, $talker) = @_; 21 | print "${msg}: "; 22 | if ( $talker->can("talk")) {$talker->talk();} 23 | else {print "Sorry! I do not talk!\n";} 24 | } 25 | 26 | $pe = Person->new(); 27 | $st = Student->new(); 28 | $pr = Professor->new(); 29 | $ro = Robot->new(); 30 | 31 | announce("Person", $pe); 32 | announce("Student", $st); 33 | announce("Professor", $pr); 34 | announce("Robot", $ro); 35 | announce("a number", 25); 36 | 37 | print "A table with different types\n"; 38 | my @elements = ($pe, $st, $pr, $ro, 25); 39 | foreach my $i (0 .. scalar @elements - 1) { 40 | eval { $elements[$i]->talk(); }; 41 | if ($@) { print "The element n°$i does not talk()\n"; } 42 | } 43 | -------------------------------------------------------------------------------- /codes/perl/polymorphism/type_manip.pl: -------------------------------------------------------------------------------- 1 | package Person; 2 | sub new { return bless {}, shift; } 3 | sub talk { print "I am talking\n"; } 4 | 1; 5 | 6 | package Student; 7 | our @ISA = qw(Person); 8 | sub learn { print "I am learning\n"; } 9 | 1; 10 | 11 | package Professor; 12 | our @ISA = qw(Person); 13 | sub new { return bless {nbr => 5}, shift; } 14 | sub teach { print "I am teaching\n"; } 15 | 1; 16 | 17 | $pe = Person->new(); 18 | $st = Student->new(); 19 | $pr = Professor->new(); 20 | 21 | print "==========================\n"; 22 | print "Object type\n"; 23 | print "==========================\n"; 24 | print "ref(pe): " . ref($pe) . "\n"; 25 | print "ref(st): " . ref($st) . "\n"; 26 | print "ref(pr): " . ref($pr) . "\n"; 27 | print "ref(25): " . ref(25) . "\n"; 28 | 29 | print "==========================\n"; 30 | print "Instance of\n"; 31 | print "==========================\n"; 32 | 33 | use Scalar::Util qw(blessed); 34 | sub instanceof { 35 | my ($obj, $cls) = @_; 36 | return (blessed($obj) and $obj->isa($cls)); 37 | } 38 | 39 | print "instanceof(pe, Person) " . instanceof($pe, "Person") . "\n"; 40 | print "instanceof(st, Person) " . instanceof($st, "Person") . "\n"; 41 | print "instanceof(25, Person) " . instanceof(25, "Person") . "\n"; 42 | print "instanceof(st, Student) " . instanceof($st, "Student") . "\n"; 43 | print "instanceof(st, Professor) " . instanceof($st, "Professor") . "\n"; 44 | 45 | print "==========================\n"; 46 | print "Member existance\n"; 47 | print "==========================\n"; 48 | 49 | sub testMembers { 50 | my ($msg, $obj) = @_; 51 | print "$msg: "; 52 | if (blessed($obj) and $obj->can("learn")) {$obj->learn();} 53 | else { print "Method learn does not exist!\n";} 54 | 55 | print "$msg: "; 56 | if (blessed($obj) and exists($obj->{nbr})) { 57 | print "Field nbr = $obj->{nbr}\n"; 58 | } 59 | else { print "Field nbr does not exist!\n";} 60 | } 61 | 62 | testMembers("Person", $pe); 63 | testMembers("Student", $st); 64 | testMembers("Professor", $pr); 65 | testMembers("25", 25); 66 | -------------------------------------------------------------------------------- /codes/php/clsobj/objmeth.php: -------------------------------------------------------------------------------- 1 | name = $name; 9 | $this->byear = $byear; 10 | } 11 | 12 | public function __toString() { 13 | return "($this->name: $this->byear)"; 14 | } 15 | 16 | public function __clone() { 17 | $this->byear ++; 18 | } 19 | 20 | } 21 | 22 | $p = new Person("Karim", 1986); 23 | 24 | print "As string: $p\n"; 25 | 26 | $p3 = clone $p; 27 | print "P3: $p3\n"; 28 | 29 | $b = ($p==$p3)? "true": "false"; 30 | print "p == p3? " . $b . "\n"; 31 | 32 | ?> 33 | -------------------------------------------------------------------------------- /codes/php/clsobj/person.php: -------------------------------------------------------------------------------- 1 | name = $name; 10 | $this->byear = $byear; 11 | self::$nbr++; 12 | } 13 | 14 | function __destruct() { 15 | self::$nbr--; 16 | print "$this->name is out\n"; 17 | } 18 | 19 | public function info() { 20 | echo "My name: $this->name, My birth year: $this->byear \n"; 21 | } 22 | 23 | public function population() { 24 | return self::$nbr; 25 | } 26 | 27 | } 28 | 29 | $p = new Person("Karim", 1986); 30 | $p2 = new Person("Karim+1", 1987); 31 | 32 | $p->info(); 33 | $p2->info(); 34 | $nbr = Person::population(); 35 | 36 | print "The number of persons: $nbr\n"; 37 | unset($p2); 38 | $nbr = Person::population(); 39 | print "The number of persons: $nbr\n"; 40 | 41 | ?> 42 | -------------------------------------------------------------------------------- /codes/php/clsobj/property.php: -------------------------------------------------------------------------------- 1 | width, Height: $this->height \n"; 9 | } 10 | 11 | function __set($property, $value) { 12 | if ($property == 'w') { 13 | if ($value < $this->width) { 14 | $this->height = $value; 15 | echo "yay"; 16 | } 17 | else { 18 | $this->height = $this->width; 19 | $this->width = $value; 20 | } 21 | } 22 | } 23 | 24 | function __get($property) { 25 | switch ($property) { 26 | case 'w': return $this->width + 2; 27 | case 'h': return $this->height + 1; 28 | } 29 | } 30 | 31 | } 32 | 33 | $r = new Rectangle(); 34 | $r->w = 50; 35 | $r->w = 20; 36 | $r->info(); 37 | echo "w: $r->w, h: $r->h\n"; 38 | 39 | ?> 40 | -------------------------------------------------------------------------------- /codes/php/encapsulation/app.php: -------------------------------------------------------------------------------- 1 | info(); 10 | $s->info(); 11 | 12 | $p->luckyNumber = 5; 13 | $p->luckyNumber2 = 6; 14 | //$p->num = 6;//error: private 15 | //$p->t = "admin"; //error: protected 16 | 17 | $p->info(); 18 | $s->info(); 19 | 20 | $p->copy($s); 21 | $p->info(); 22 | 23 | ?> 24 | -------------------------------------------------------------------------------- /codes/php/encapsulation/person.php: -------------------------------------------------------------------------------- 1 | name = $name; 13 | $this->num = (self::$nbr++); 14 | $this->t = "person"; 15 | } 16 | 17 | public function info() { 18 | echo "My name: $this->name\n"; 19 | echo "My number: $this->num\n"; 20 | echo "I am a: $this->t\n"; 21 | echo "My lucky number is: $this->luckyNumber\n"; 22 | echo "My lucky number 2 is: $this->luckyNumber2\n"; 23 | echo "--------------------------\n"; 24 | } 25 | 26 | public function copy($other) { 27 | $this->name =$other->name; 28 | $this->t = $other->t; 29 | } 30 | 31 | } 32 | 33 | ?> 34 | -------------------------------------------------------------------------------- /codes/php/encapsulation/student.php: -------------------------------------------------------------------------------- 1 | t = "student"; 10 | $this->luckyNumber = 1; 11 | $this->luckyNumber2 = 2; 12 | //$this->name = "other name";//cannot be accessed: private 13 | } 14 | 15 | } 16 | 17 | ?> 18 | -------------------------------------------------------------------------------- /codes/php/hello/entry.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/php 2 | 17 | -------------------------------------------------------------------------------- /codes/php/hello/except.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/php 2 | 5) throw new BigException(); 19 | if ($i ==0) return 1; 20 | return $i * fact($i - 1); 21 | } 22 | 23 | if (sizeof($argv) < 2) { 24 | echo "Please enter an integer value\n"; 25 | exit(); 26 | } 27 | 28 | $n = $argv[1]; 29 | 30 | try { 31 | $f = fact($n); 32 | echo "Fact($n)= " . $f . "\n"; 33 | } 34 | catch (NegException $e) { 35 | echo $e->errorMessage() . "\n"; 36 | } 37 | catch (BigException $e) { 38 | echo $e->errorMessage() . "\n"; 39 | } 40 | ?> 41 | -------------------------------------------------------------------------------- /codes/php/hello/func.php: -------------------------------------------------------------------------------- 1 | 12 | -------------------------------------------------------------------------------- /codes/php/hello/helloworld.php: -------------------------------------------------------------------------------- 1 | Hello world!\n"; 3 | ?> 4 | -------------------------------------------------------------------------------- /codes/php/inheritance/abst/app.php: -------------------------------------------------------------------------------- 1 | info(); 14 | $gs->info(); 15 | 16 | ?> 17 | -------------------------------------------------------------------------------- /codes/php/inheritance/abst/gradstudent.php: -------------------------------------------------------------------------------- 1 | 14 | -------------------------------------------------------------------------------- /codes/php/inheritance/abst/person.php: -------------------------------------------------------------------------------- 1 | info2(); 7 | } 8 | 9 | abstract protected function info2(); 10 | 11 | } 12 | 13 | ?> 14 | -------------------------------------------------------------------------------- /codes/php/inheritance/abst/professor.php: -------------------------------------------------------------------------------- 1 | 14 | -------------------------------------------------------------------------------- /codes/php/inheritance/abst/student.php: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /codes/php/inheritance/fin/finalcls.php: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /codes/php/inheritance/fin/finalmth.php: -------------------------------------------------------------------------------- 1 | method(); 22 | $st->method(); 23 | 24 | $pe->finalMethod(); 25 | $st->finalMethod(); 26 | 27 | ?> 28 | -------------------------------------------------------------------------------- /codes/php/inheritance/multiple/diamond.php: -------------------------------------------------------------------------------- 1 | name = $name; 13 | } 14 | public function info() { echo "my name: $this->name\n"; } 15 | } 16 | 17 | trait tStudent { 18 | protected $mark; 19 | public function __construct($name, $mark) { 20 | parent::__construct($name); 21 | $this->mark = $mark; 22 | } 23 | public function info() { echo "my mark: $this->mark\n"; } 24 | } 25 | 26 | trait tProfessor { 27 | protected $hours; 28 | public function __construct($name, $hours) { 29 | parent::__construct($name); 30 | $this->hours = $hours; 31 | } 32 | public function info() { echo "my work hours: $this->hours\n"; } 33 | } 34 | 35 | class Student extends Person implements iStudent { 36 | use tStudent {tStudent::info as private sinfo;} 37 | public function info() { 38 | parent::info(); 39 | $this->sinfo(); 40 | } 41 | } 42 | 43 | class Professor extends Person implements iProfessor { 44 | use tProfessor {tProfessor::info as private pinfo;} 45 | public function info() { 46 | parent::info(); 47 | $this->pinfo(); 48 | } 49 | } 50 | 51 | class PhdStudent extends Person implements iStudent, iProfessor { 52 | use tStudent { 53 | tStudent::__construct as private __sconstruct; 54 | tStudent::info as private sinfo; 55 | } 56 | use tProfessor { 57 | tProfessor::__construct as private __pconstruct; 58 | tProfessor::info as private pinfo; 59 | } 60 | public function __construct($name, $hours, $mark) { 61 | $this->__pconstruct($name, $hours); 62 | $this->__sconstruct("$name modified", $mark); 63 | } 64 | public function info() { 65 | parent::info(); 66 | $this->sinfo(); 67 | $this->pinfo(); 68 | } 69 | } 70 | 71 | 72 | $a = new Person("person1"); 73 | $a->info(); 74 | echo "=====================\n"; 75 | $s = new Student("student1", 15.5); 76 | $s->info(); 77 | echo "=====================\n"; 78 | $p = new Professor("professor1", 8); 79 | $p->info(); 80 | echo "=====================\n"; 81 | $ps = new PhdStudent("phdStudent1", 4, 12.5); 82 | $ps->info(); 83 | echo "=====================\n"; 84 | 85 | ?> 86 | -------------------------------------------------------------------------------- /codes/php/inheritance/multiple/multiple1.php: -------------------------------------------------------------------------------- 1 | info(); 19 | $this->moreInfo(); 20 | } 21 | public function moreInfo(){} 22 | } 23 | 24 | class Person implements iPerson { use tPerson, tConstruct;} 25 | 26 | class Machine implements iMachine { use tMachine, tConstruct; } 27 | 28 | class Android extends Machine implements iPerson { 29 | use tPerson, tConstruct; 30 | public function moreInfo(){ echo "So, I am an android\n";} 31 | } 32 | 33 | class Cyborg extends Person implements iMachine { 34 | use tMachine, tConstruct; 35 | public function moreInfo(){ echo "So, I am a cyborg\n";} 36 | } 37 | 38 | 39 | $p = new Person(); 40 | echo "=====================\n"; 41 | $m = new Machine(); 42 | echo "=====================\n"; 43 | $a = new Android(); 44 | echo "=====================\n"; 45 | $c = new Cyborg(); 46 | echo "=====================\n"; 47 | 48 | 49 | 50 | ?> 51 | -------------------------------------------------------------------------------- /codes/php/inheritance/multiple/multiple2.php: -------------------------------------------------------------------------------- 1 | name\n"; 14 | } 15 | } 16 | 17 | trait tMachine { 18 | public function info() { 19 | echo "My serial number: $this->serial\n"; 20 | } 21 | } 22 | 23 | class Person implements iPerson { 24 | use tPerson; 25 | private $name; 26 | public function __construct($name) { 27 | $this->name = $name; 28 | } 29 | } 30 | 31 | class Machine implements iMachine { 32 | use tMachine; 33 | private $serial; 34 | public function __construct($serial) { 35 | $this->serial = $serial; 36 | } 37 | } 38 | 39 | class Android extends Machine implements iPerson { 40 | use tPerson { info as private pinfo;} 41 | private $name; 42 | 43 | public function __construct($name, $serial) { 44 | parent::__construct($serial); 45 | $this->name = $name; 46 | } 47 | 48 | public function info() { 49 | parent::info(); 50 | $this->pinfo(); 51 | } 52 | 53 | } 54 | 55 | class Cyborg extends Person implements iMachine { 56 | use tMachine { info as private minfo;} 57 | private $serial; 58 | 59 | public function __construct($name, $serial) { 60 | parent::__construct($name); 61 | $this->serial = $serial; 62 | } 63 | 64 | public function info() { 65 | parent::info(); 66 | $this->minfo(); 67 | } 68 | 69 | } 70 | 71 | $p = new Person("person1"); 72 | $p->info(); 73 | echo "=====================\n"; 74 | $m = new Machine(15); 75 | $m->info(); 76 | echo "=====================\n"; 77 | $a = new Android("android1", 16); 78 | $a->info(); 79 | echo "=====================\n"; 80 | $c = new Cyborg("cyborg1", 17); 81 | $c->info(); 82 | echo "=====================\n"; 83 | 84 | ?> 85 | -------------------------------------------------------------------------------- /codes/php/inheritance/single/app.php: -------------------------------------------------------------------------------- 1 | luckyNumber = 10; 13 | $st->luckyNumber = 20; 14 | $pr->luckyNumber = 30; 15 | 16 | $pe->info(); 17 | $st->info(); 18 | $pr->info(); 19 | $pr->info2(); 20 | 21 | $gs = new GradStudent("gradstudent1", 14); 22 | 23 | ?> 24 | -------------------------------------------------------------------------------- /codes/php/inheritance/single/gradstudent.php: -------------------------------------------------------------------------------- 1 | grade = $grade + 2; 12 | //parent::parent::info(); //cannot do this 13 | parent::info(); 14 | //echo parent::luckyNumber; 15 | } 16 | 17 | } 18 | 19 | ?> 20 | -------------------------------------------------------------------------------- /codes/php/inheritance/single/person.php: -------------------------------------------------------------------------------- 1 | name = $name; 9 | } 10 | 11 | public function info() { 12 | echo "My name: $this->name\n"; 13 | echo "My lucky number is: $this->luckyNumber\n"; 14 | } 15 | 16 | private function info2() { 17 | echo "Person.info2\n"; 18 | } 19 | 20 | } 21 | 22 | ?> 23 | -------------------------------------------------------------------------------- /codes/php/inheritance/single/professor.php: -------------------------------------------------------------------------------- 1 | 14 | -------------------------------------------------------------------------------- /codes/php/inheritance/single/student.php: -------------------------------------------------------------------------------- 1 | grade = $grade; 12 | } 13 | 14 | public function info(){ 15 | parent::info(); 16 | echo "My grade is: $this->grade\n"; 17 | } 18 | 19 | } 20 | 21 | ?> 22 | -------------------------------------------------------------------------------- /codes/php/polymorphism/overloading.php: -------------------------------------------------------------------------------- 1 | read(); 32 | $pe->read("I am a person"); 33 | 34 | echo "STUDENT\n"; 35 | $st = new Student(); 36 | $st->read(); 37 | $st->read("I am a student"); 38 | $st->read(5); 39 | 40 | ?> 41 | -------------------------------------------------------------------------------- /codes/php/polymorphism/overriding.php: -------------------------------------------------------------------------------- 1 | talk(); // I am a person 14 | $st->talk(); // I am a student 15 | 16 | ?> 17 | -------------------------------------------------------------------------------- /codes/php/polymorphism/subtype.php: -------------------------------------------------------------------------------- 1 | talk(); 22 | else echo "Sorry! I do not talk!\n"; 23 | } 24 | 25 | $pe = new Person(); 26 | $st = new Student(); 27 | $pr = new Professor(); 28 | $ro = new Robot(); 29 | 30 | announce("Person", $pe); 31 | announce("Student", $st); 32 | announce("Professor", $pr); 33 | announce("Robot", $ro); 34 | announce("a number", 25); 35 | 36 | echo "A table with different types\n"; 37 | $elements = array($pe, $st, $pr, $ro, 25); 38 | foreach($elements as $i=>$element) { 39 | try { 40 | $element->talk(); 41 | } 42 | catch (Error $e) { 43 | echo "The element n°$i does not talk()\n"; 44 | } 45 | } 46 | 47 | ?> 48 | -------------------------------------------------------------------------------- /codes/php/polymorphism/type_manip.php: -------------------------------------------------------------------------------- 1 | learn();} 50 | else { echo "Method learn does not exist!\n";} 51 | 52 | echo "$msg: "; 53 | if (is_object($obj) && property_exists($obj, "nbr")) { 54 | echo "Field nbr = $obj->{nbr}\n"; 55 | } 56 | else { echo "Field nbr does not exist!\n";} 57 | } 58 | 59 | testMembers("Person", $pe); 60 | testMembers("Student", $st); 61 | testMembers("Professor", $pr); 62 | testMembers("a number", 25); 63 | 64 | ?> 65 | -------------------------------------------------------------------------------- /codes/python/clsobj/objmeth.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Person(object): 5 | 6 | def __init__(self, name, byear): 7 | self.name = name 8 | self.byear = byear 9 | 10 | def __str__(self): 11 | return "(" + self.name + ": " + str(self.byear) + ")" 12 | 13 | def __copy__(self): 14 | return Person(self.name, self.byear + 2) 15 | 16 | def __eq__(self, other): 17 | return (self.name == other.name) 18 | 19 | def __lt__(self, other): 20 | return (self.byear < other.byear) 21 | 22 | def __hash__(self): 23 | return hash(self.name) 24 | 25 | import copy 26 | 27 | p = Person("Karim", 1986) 28 | p2 = Person("Karim+1", 1986) 29 | print ("As string: " + str(p)) 30 | print ("HashCode: " + str(hash(p))) 31 | 32 | p3 = copy.copy(p) 33 | print ("Copy: " + str(p3)) 34 | print("p == p3? " + str(p==p3)) 35 | print("p == p2? " + str(p==p2)) 36 | print("p < p3? " + str(p 5: 18 | raise BigException 19 | 20 | if i == 0: 21 | return 1 22 | return i * fact(i-1) 23 | 24 | if len(sys.argv) < 2: 25 | print("Please enter an integer value") 26 | sys.exit() 27 | n = 0 28 | try: 29 | n = int(sys.argv[1]) 30 | except ValueError: 31 | print("The value must be an integer") 32 | sys.exit() 33 | 34 | try: 35 | f = fact(n) 36 | print("Fact(" + str(n) + ")= " + str(f)) 37 | except Exception as e: 38 | print(str(e)) 39 | -------------------------------------------------------------------------------- /codes/python/hello/func.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | if __name__ == "__main__": 5 | from func import fact 6 | import sys 7 | sys.stdout.write("Please enter an integer value: ") 8 | n = int(raw_input()) 9 | sys.stdout.write("Fact(" + str(n) + ")= ") 10 | print(str(fact(n))) 11 | 12 | def fact(i): 13 | if i <= 1: 14 | return 1 15 | return i * fact(i-1) -------------------------------------------------------------------------------- /codes/python/hello/helloworld.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | print ("Hello World") -------------------------------------------------------------------------------- /codes/python/inheritance/abst/app.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from person import Person 5 | from student import Student 6 | from gradstudent import GradStudent 7 | from professor import Professor 8 | 9 | # pe = Person() # abstract 10 | # st = Student() # abstract 11 | gs = GradStudent() 12 | pr = Professor() 13 | 14 | gs.info() 15 | pr.info() 16 | -------------------------------------------------------------------------------- /codes/python/inheritance/abst/gradstudent.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from student import Student 5 | 6 | class GradStudent(Student): 7 | 8 | def info2(self): 9 | print("GradStudent info2") 10 | -------------------------------------------------------------------------------- /codes/python/inheritance/abst/person.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from abc import ABCMeta, abstractmethod 5 | 6 | class Person(object): 7 | __metaclass__ = ABCMeta 8 | 9 | def info(self): 10 | print("====INFO====") 11 | self.info2() 12 | 13 | @abstractmethod 14 | def info2(self): 15 | pass 16 | -------------------------------------------------------------------------------- /codes/python/inheritance/abst/professor.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from person import Person 5 | 6 | class Professor(Person): 7 | 8 | def info2(self): 9 | print("Professor info2") 10 | -------------------------------------------------------------------------------- /codes/python/inheritance/abst/student.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from person import Person 5 | 6 | class Student(Person): 7 | pass 8 | -------------------------------------------------------------------------------- /codes/python/inheritance/fin/finalcls.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Final(type): 5 | def __new__(cls, name, bases, classdict): 6 | for b in bases: 7 | if isinstance(b, Final): 8 | raise TypeError("'{0}' is final class".format(b.__name__)) 9 | return type.__new__(cls, name, bases, dict(classdict)) 10 | 11 | # class Person(metaclass=Final): pass #Python 3 12 | class Person(object): 13 | __metaclass__ = Final 14 | 15 | #class Student(Person): pass # error: final 16 | 17 | pe = Person() 18 | -------------------------------------------------------------------------------- /codes/python/inheritance/fin/finalmth.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # https://stackoverflow.com/a/321334 4 | 5 | def final(f): 6 | f.final = True 7 | return f 8 | 9 | class FinalMethods(type): 10 | def __new__(cls, name, bases, dct): 11 | finals = get_finals(bases) 12 | for name in dct: 13 | if name in finals: 14 | raise Exception ("You can not override %s, it is final" % name) 15 | return type.__new__(cls, name, bases, dct) 16 | 17 | def get_finals(bases): 18 | ret = [] 19 | for source in bases: 20 | for name, attr in source.__dict__.items(): 21 | if getattr(attr, "final", False): 22 | ret.append(name) 23 | ret.extend(get_finals(source.__bases__)) 24 | return ret 25 | 26 | # class Person(metaclass=FinalMethods): #Python 3 27 | class Person(object): 28 | __metaclass__ = FinalMethods 29 | 30 | def method(self): 31 | print("Person method") 32 | 33 | @final 34 | def final_method(self): 35 | print("Person final method") 36 | 37 | class Student(Person): 38 | def method(self): 39 | print("Student method") 40 | 41 | #def final_method(self): 42 | # print("Student final method") 43 | 44 | pe = Person() 45 | st = Student() 46 | 47 | pe.method() 48 | st.method() 49 | 50 | pe.final_method() 51 | st.final_method() 52 | -------------------------------------------------------------------------------- /codes/python/inheritance/multiple/diamond.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Person(object): 5 | def __init__(self, name): 6 | self.name = name 7 | print("Person") 8 | 9 | def info(self): 10 | print("my name: " + self.name) 11 | 12 | class Student(Person): 13 | def __init__(self, name, mark): 14 | Person.__init__(self, name) 15 | self.mark = mark 16 | print("Student") 17 | 18 | def info(self): 19 | Person.info(self) 20 | print("my mark: " + str(self.mark)) 21 | 22 | class Professor(Person): 23 | def __init__(self, name, hours): 24 | Person.__init__(self, name) 25 | self.hours = hours 26 | print("Professor") 27 | 28 | class PhdStudent(Student, Professor): 29 | def __init__(self, name, hours, mark): 30 | Professor.__init__(self, name, mark) 31 | Student.__init__(self, name + "-1", mark) 32 | print("PhdStudent") 33 | 34 | a = Person("person1") 35 | a.info() 36 | print("=====================") 37 | s = Student("student1", 15.5) 38 | s.info() 39 | print("=====================") 40 | p = Professor("professor1", 8) 41 | p.info() 42 | print("=====================") 43 | ps = PhdStudent("phdStudent1", 4, 12.5) 44 | ps.info() 45 | print("=====================") 46 | -------------------------------------------------------------------------------- /codes/python/inheritance/multiple/multiple1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Person(object): 5 | def __init__(self): 6 | print("I am a person") 7 | 8 | class Machine(object): 9 | def __init__(self): 10 | print("I am a machine") 11 | 12 | class Android(Machine, Person): 13 | def __init__(self): 14 | Machine.__init__(self) 15 | Person.__init__(self) 16 | print("So, I am an android") 17 | 18 | class Cyborg(Person, Machine): 19 | def __init__(self): 20 | Person.__init__(self) 21 | Machine.__init__(self) 22 | print("I am a cyborg") 23 | 24 | 25 | p = Person() 26 | print("=====================") 27 | m = Machine() 28 | print("=====================") 29 | a = Android() 30 | print("=====================") 31 | c = Cyborg() 32 | print("=====================") 33 | -------------------------------------------------------------------------------- /codes/python/inheritance/multiple/multiple2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Person(object): 5 | def __init__(self, name): 6 | self.name = name 7 | self.serial = 5 8 | 9 | def info(self): 10 | print("Person name: " + self.name) 11 | print("Person serial number: " + str(self.serial)) 12 | 13 | class Machine(object): 14 | def __init__(self, serial): 15 | self.serial = serial 16 | 17 | def info(self): 18 | print("Machine serial number: " + str(self.serial)) 19 | 20 | class Android(Machine, Person): 21 | def __init__(self, name, serial): 22 | Person.__init__(self, name) 23 | Machine.__init__(self, serial) 24 | 25 | class Cyborg(Person, Machine): 26 | def __init__(self, name, serial): 27 | Machine.__init__(self, serial) 28 | Person.__init__(self, name) 29 | 30 | 31 | p = Person("person1") 32 | p.info() 33 | print("=====================") 34 | m = Machine(17) 35 | m.info() 36 | print("=====================") 37 | a = Android("android1", 21) 38 | a.info() 39 | print("=====================") 40 | c = Cyborg("cyborg1", 27) 41 | c.info() 42 | print("=====================") 43 | -------------------------------------------------------------------------------- /codes/python/inheritance/single/app.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from person import Person 5 | from student import Student 6 | from professor import Professor 7 | 8 | pe = Person("person1") 9 | st = Student("student1", 15) 10 | pr = Professor("professor1") 11 | 12 | pe.info() 13 | st.info() 14 | pr.info() 15 | -------------------------------------------------------------------------------- /codes/python/inheritance/single/person.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Person(object): 5 | 6 | def __init__(self, name): 7 | self.name = name 8 | 9 | def info(self): 10 | print("My name: " + self.name) 11 | -------------------------------------------------------------------------------- /codes/python/inheritance/single/professor.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from person import Person 5 | 6 | class Professor(Person): 7 | pass 8 | -------------------------------------------------------------------------------- /codes/python/inheritance/single/student.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from person import Person 5 | 6 | class Student(Person): 7 | 8 | def __init__(self, name, grade): 9 | Person.__init__(self, name) 10 | #super(Student, self).__init__(name)#the same 11 | self.grade = grade 12 | 13 | def info(self): 14 | super(Student, self).info() 15 | #Person.info(self) #the same 16 | print("My grade: " + str(self.grade)) 17 | -------------------------------------------------------------------------------- /codes/python/polymorphism/overloading.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Person(object): 5 | def read(self, text=None): 6 | if text == None: 7 | print("I am reading") 8 | elif type(text) is str: 9 | print("A text: " + text) 10 | 11 | class Student(Person): 12 | def read(self, *args): 13 | if len(args) == 0: 14 | Person.read(self) 15 | elif len(args) == 1: 16 | Person.read(self, args[0]) 17 | else: 18 | print("I am reading: " + str(args[0]) + " for " + str(args[1]) + " times") 19 | 20 | print("PERSON") 21 | pe = Person() 22 | pe.read() 23 | pe.read("I am a person") 24 | 25 | print("STUDENT") 26 | st = Student() 27 | st.read() 28 | st.read("I am a student") 29 | st.read("a book", 5) 30 | -------------------------------------------------------------------------------- /codes/python/polymorphism/overriding.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Person(object): 5 | def talk(self): 6 | print("I am a person") 7 | 8 | class Student(Person): 9 | def talk(self): 10 | print("I am a student") 11 | 12 | pe = Person() 13 | st = Student() 14 | pe.talk() # I am a person 15 | st.talk() # I am a student 16 | -------------------------------------------------------------------------------- /codes/python/polymorphism/subtype.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Person(object): 5 | def talk(self): 6 | print("I am a person") 7 | 8 | class Student(Person): pass 9 | 10 | class Professor(Person): pass 11 | 12 | class Robot(object): 13 | def talk(self): 14 | print("I am a robot") 15 | 16 | import sys 17 | def announce(msg, talker): 18 | sys.stdout.write(msg + ": ") 19 | talk = getattr(talker, "talk", None) 20 | if talk != None and callable(talk): 21 | talk() 22 | else: 23 | print("Sorry! I do not talk!") 24 | 25 | pe = Person() 26 | st = Student() 27 | pr = Professor() 28 | ro = Robot() 29 | 30 | announce("Person", pe) 31 | announce("Student", st) 32 | announce("Professor", pr) 33 | announce("Robot", ro) 34 | announce("a number", 25) 35 | 36 | print ("A table with different types") 37 | elements = [pe, st, pr, ro, 25] 38 | for i, element in enumerate(elements): 39 | try: 40 | element.talk() 41 | except Exception: 42 | print("The element n°" + str(i) + " does not talk()") 43 | -------------------------------------------------------------------------------- /codes/python/polymorphism/type_manip.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class Person(object): 5 | def talk(self): 6 | print("I am a talking") 7 | 8 | class Student(Person): 9 | def learn(self): 10 | print("I am learning") 11 | 12 | class Professor(Person): 13 | def __init__(self): 14 | self.nbr = 5 15 | def teach(self): 16 | print("I am teaching") 17 | 18 | class PhdStudent(Student, Professor): pass 19 | 20 | pe = Person() 21 | st = Student() 22 | pr = Professor() 23 | phd = PhdStudent() 24 | 25 | print("==========================") 26 | print("Object type") 27 | print("==========================") 28 | print("type(pe): " + type(pe).__name__) 29 | print("st.__class__: " + st.__class__.__name__) 30 | print("type(pr): " + type(pr).__name__) 31 | print("type(phd): " + type(phd).__name__) 32 | print("type(25): " + type(25).__name__) 33 | 34 | print("==========================") 35 | print("Instance of") 36 | print("==========================") 37 | 38 | print ("isinstance(pe, Person) " + str(isinstance(pe, Person))) 39 | print ("isinstance(st, Person) " + str(isinstance(st, Person))) 40 | print ("isinstance(25, Person) " + str(isinstance(25, Person))) 41 | print ("isinstance(st, Student) " + str(isinstance(st, Student))) 42 | print ("isinstance(st, Professor) " + str(isinstance(st, Professor))) 43 | print ("isinstance(pr, Professor) " + str(isinstance(pr, Professor))) 44 | print ("isinstance(phd, Professor) " + str(isinstance(phd, Professor))) 45 | print ("isinstance(phd, Student) " + str(isinstance(phd, Student))) 46 | 47 | print("==========================") 48 | print("Member existance") 49 | print("==========================") 50 | import sys 51 | def testMembers(msg, obj): 52 | sys.stdout.write(msg + ": ") 53 | learn = getattr(obj, "learn", None) 54 | if learn != None and callable(learn): 55 | learn() 56 | else: 57 | print("Method learn does not exist!") 58 | sys.stdout.write(msg + ": ") 59 | if hasattr(obj, "nbr"): 60 | print("Field nbr = " + str(obj.nbr)) 61 | else: 62 | print("Field nbr does not exist!") 63 | 64 | testMembers("Person", pe) 65 | testMembers("Student", st) 66 | testMembers("Professor", pr) 67 | testMembers("PhdStudent", phd) 68 | testMembers("a number", 25) 69 | -------------------------------------------------------------------------------- /codes/ruby/clsobj/objmeth.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | class Person 4 | include Comparable 5 | attr_accessor :byear 6 | attr_reader :name 7 | 8 | def initialize(name, byear) 9 | @name, @byear = name, byear 10 | end 11 | 12 | def initialize_clone(source) 13 | c = super 14 | c.byear += 2 15 | c 16 | end 17 | 18 | def to_s 19 | "(#{@name}: #{@byear})" 20 | end 21 | 22 | def <=>(other) 23 | cmp = byear <=> other.byear 24 | if cmp == 0 25 | name <=> other.name 26 | else 27 | cmp 28 | end 29 | end 30 | 31 | def eql?(other) 32 | name == other.name 33 | end 34 | 35 | def hash 36 | name.hash 37 | end 38 | 39 | end 40 | 41 | p = Person.new("Karim", 1986) 42 | p2 = Person.new("Karim+1", 1986) 43 | 44 | puts "As string: #{p}" 45 | puts "HashCode: #{p.hash}" 46 | 47 | p3 = p.clone 48 | puts "Copy: #{p3}" 49 | puts "p.eql? p3? #{p.eql? p3}" 50 | puts "p <=> p3? #{p <=> p3}" 51 | puts "p <=> p2? #{p <=> p2}" 52 | puts "p < p2? #{p < p2}" 53 | -------------------------------------------------------------------------------- /codes/ruby/clsobj/person.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | class Person 4 | 5 | @@nbr = 0 6 | 7 | def initialize(name, byear) 8 | @name, @byear = name, byear 9 | @@nbr += 1 10 | ObjectSpace.define_finalizer( self, proc {Person.finalize(@name)}) 11 | end 12 | 13 | def self.finalize(n) 14 | @@nbr -= 1 15 | puts "#{n} is out" 16 | end 17 | 18 | def info 19 | puts "My name: #@name, My birth year: #@byear" 20 | end 21 | 22 | def self.population 23 | @@nbr 24 | end 25 | end 26 | 27 | p = Person.new("Karim", 1986) 28 | p2 = Person.new("Karim+1", 1987) 29 | 30 | p.info() 31 | p2.info 32 | nbr = Person.population 33 | 34 | puts "The number of persons: #{nbr}" 35 | p2 = nil 36 | nbr = Person.population 37 | puts "The number of persons: #{nbr}" 38 | -------------------------------------------------------------------------------- /codes/ruby/clsobj/property.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | class Rectangle 4 | 5 | def initialize 6 | @width, @height = 0, 0 7 | end 8 | 9 | def w 10 | @width + 2 11 | end 12 | 13 | def h 14 | @height + 1 15 | end 16 | 17 | def w=(value) 18 | if value < @width 19 | @height = value 20 | else 21 | @height = @width 22 | @width = value 23 | end 24 | end 25 | 26 | def info 27 | puts "Width: #@width, Height: #@height" 28 | end 29 | 30 | end 31 | 32 | r = Rectangle.new 33 | r.w = 50 34 | r.w = 20 35 | r.info 36 | puts "w: #{r.w}, h: #{r.h}" 37 | -------------------------------------------------------------------------------- /codes/ruby/encapsulation/app.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | require_relative "person" 4 | require_relative "student" 5 | 6 | p = Person.new("Karim_p") 7 | s = Student.new("Karim_s") 8 | 9 | p.luckyNumber = 5 10 | #p.num = 6 #can't access the attribute 11 | #puts "p.name = #{p.name}" #private 12 | 13 | puts "======= initial info =========" 14 | p.info 15 | s.info 16 | 17 | # p.info1 #private 18 | # p.info2 #protected 19 | 20 | p.copy(s) 21 | puts "====== info() after copy() ======" 22 | p.info 23 | puts "======= p.other_info(s) =========" 24 | p.other_info(s) 25 | puts "======= s.info_student =========" 26 | s.info_student 27 | -------------------------------------------------------------------------------- /codes/ruby/encapsulation/person.rb: -------------------------------------------------------------------------------- 1 | class Person 2 | 3 | @@nbr = 0 4 | 5 | attr_accessor :luckyNumber #attr_reader, attr_writer 6 | attr_reader :t, :name 7 | protected :name 8 | 9 | def initialize(name) 10 | @name = name 11 | @num = @@nbr 12 | @t = "person" 13 | @luckyNumber = 0 14 | @@nbr += 1 15 | end 16 | 17 | def info 18 | puts "My name: #@name" 19 | info1 20 | info2 21 | puts "My luckyNumber is: #@luckyNumber" 22 | end 23 | 24 | private 25 | def info1 26 | puts "My number: #@num" 27 | end 28 | 29 | protected 30 | def info2 31 | puts "I am a: #@t" 32 | end 33 | 34 | public 35 | def copy (other) 36 | @luckyNumber = other.luckyNumber 37 | @name = other.name 38 | #@num = other.num #can not access num of other 39 | end 40 | 41 | def other_info (other) 42 | puts "other info" 43 | #other.info1 #private 44 | other.info2 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /codes/ruby/encapsulation/student.rb: -------------------------------------------------------------------------------- 1 | require_relative "person" 2 | 3 | class Student < Person 4 | 5 | def initialize(name) 6 | super(name) 7 | @t = "student" 8 | @luckyNumber = 1 9 | @name = "#@name 2" 10 | @num = 5 11 | end 12 | 13 | def info_student 14 | puts "info student" 15 | info1 16 | info2 17 | end 18 | 19 | end 20 | -------------------------------------------------------------------------------- /codes/ruby/hello/entry.rb: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/ruby -w 2 | 3 | def fact(i) 4 | if i <= 1 5 | 1 6 | else 7 | i * fact(i-1) 8 | end 9 | end 10 | 11 | if __FILE__ == $0 12 | if ARGV.length < 1 13 | print "Please enter an integer value\n" 14 | exit 0 15 | end 16 | 17 | n = ARGV[0].to_i 18 | puts "Fact(" + n.to_s + ")= " + fact(n).to_s 19 | end 20 | -------------------------------------------------------------------------------- /codes/ruby/hello/except.rb: -------------------------------------------------------------------------------- 1 | def fact(i) 2 | case 3 | when i < 0 4 | raise "Negative numbers don't have a factorial!" 5 | when i > 5 6 | raise "The number is too big!" 7 | when i == 0 8 | 1 9 | else 10 | i * fact(i-1) 11 | end 12 | end 13 | 14 | if ARGV.length < 1 15 | print "Please enter an integer value\n" 16 | exit 0 17 | end 18 | 19 | n = ARGV[0].to_i 20 | 21 | begin 22 | f = fact(n) 23 | puts "Fact(" + n.to_s + ")= " + f.to_s 24 | rescue Exception => e 25 | puts e.message + "\n" 26 | else 27 | puts "No exception\n" 28 | ensure 29 | puts "That's all!\n" 30 | end 31 | -------------------------------------------------------------------------------- /codes/ruby/hello/except2.rb: -------------------------------------------------------------------------------- 1 | module Errors 2 | class NegException < StandardError 3 | def initialize() 4 | super("Negative numbers don't have a factorial!") 5 | end 6 | end 7 | class BigException < StandardError 8 | def initialize() 9 | super("The number is too big!") 10 | end 11 | end 12 | end 13 | 14 | def fact(i) 15 | case 16 | when i < 0 17 | raise Errors::NegException.new() 18 | when i > 5 19 | raise Errors::BigException.new() 20 | when i == 0 21 | 1 22 | else 23 | i * fact(i-1) 24 | end 25 | end 26 | 27 | if ARGV.length < 1 28 | print "Please enter an integer value\n" 29 | exit 0 30 | end 31 | 32 | n = ARGV[0].to_i 33 | 34 | begin 35 | f = fact(n) 36 | puts "Fact(" + n.to_s + ")= " + f.to_s 37 | rescue Errors::NegException => e 38 | puts e.message + "\n" 39 | rescue Errors::BigException => e 40 | puts e.message + "\n" 41 | else 42 | puts "No exception\n" 43 | ensure 44 | puts "That's all!\n" 45 | end 46 | -------------------------------------------------------------------------------- /codes/ruby/hello/func.rb: -------------------------------------------------------------------------------- 1 | if __FILE__ == $0 2 | require './func' 3 | print "Please enter an integer value: " 4 | n = gets.chomp.to_i 5 | r = fact(n) 6 | puts "Fact(" + n.to_s + ")= " + r.to_s 7 | end 8 | 9 | def fact(i) 10 | if i <= 1 11 | 1 12 | else 13 | i * fact(i-1) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /codes/ruby/hello/helloworld.rb: -------------------------------------------------------------------------------- 1 | puts "Hello World!" -------------------------------------------------------------------------------- /codes/ruby/inheritance/abst/app.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | require_relative "person" 4 | require_relative "student" 5 | require_relative "professor" 6 | require_relative "gradstudent" 7 | 8 | #pe = Person.new()#abstract class 9 | st = Student.new() 10 | pr = Professor.new() 11 | gs = GradStudent.new() 12 | 13 | #st.info #abstract method 14 | pr.info 15 | gs.info 16 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/abst/gradstudent.rb: -------------------------------------------------------------------------------- 1 | require_relative "student" 2 | require_relative "person" 3 | 4 | class GradStudent < Student 5 | 6 | def info2 7 | puts "GradStudent info2" 8 | end 9 | 10 | end 11 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/abst/person.rb: -------------------------------------------------------------------------------- 1 | class Person 2 | 3 | def initialize() 4 | if self.class == Person then 5 | raise "Abstract class" 6 | end 7 | end 8 | 9 | def info 10 | puts "====INFO====" 11 | info2 12 | end 13 | 14 | def info2 15 | raise "abstract method" 16 | end 17 | 18 | end 19 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/abst/professor.rb: -------------------------------------------------------------------------------- 1 | require_relative "person" 2 | 3 | class Professor < Person 4 | 5 | def info2 6 | puts "Professor info2" 7 | end 8 | 9 | end 10 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/abst/student.rb: -------------------------------------------------------------------------------- 1 | require_relative "person" 2 | 3 | class Student < Person 4 | 5 | def initialize() 6 | super 7 | end 8 | 9 | end 10 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/fin/finalmth.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | # https://stackoverflow.com/a/40247731 3 | 4 | class Class 5 | def frozen_method(method) 6 | if class_variable_defined?(:@@__frozen_methods__) 7 | add= class_variable_get(:@@__frozen_methods__) | [method] 8 | class_variable_set(:@@__frozen_methods__,add) 9 | else 10 | class_variable_set(:@@__frozen_methods__,[method]) 11 | end 12 | class << self 13 | def inherited(child) 14 | def method_added(method) 15 | if class_variable_get(:@@__frozen_methods__).include? method 16 | send(:remove_method, method) 17 | error="Cannot change method #{method} because it's not overridde" 18 | raise TypeError, error 19 | end 20 | end 21 | end 22 | end 23 | end 24 | end 25 | 26 | class Person 27 | 28 | def method 29 | puts "Person method" 30 | end 31 | 32 | def final_method 33 | puts "Person final_method" 34 | end 35 | 36 | frozen_method :final_method 37 | 38 | end 39 | 40 | class Student < Person 41 | 42 | def method 43 | puts "Student method" 44 | end 45 | 46 | #def final_method 47 | # puts "Student final_method" 48 | #end 49 | #error 50 | 51 | end 52 | 53 | pe = Person.new() 54 | st = Student.new() 55 | 56 | pe.method 57 | st.method 58 | 59 | pe.final_method 60 | st.final_method 61 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/multiple/multiple1.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | module PersonMixin 4 | def info; puts "I am a person"; end 5 | end 6 | 7 | module MachineMixin 8 | def info; puts "I am a machine"; end 9 | end 10 | 11 | module InitMixin 12 | def initialize() 13 | info2 = method(:info).super_method 14 | if info2 != nil then info2.call end 15 | info 16 | moreInfo 17 | end 18 | def moreInfo; end 19 | end 20 | 21 | class Person include PersonMixin, InitMixin end 22 | 23 | class Machine include MachineMixin, InitMixin end 24 | 25 | class Android < Machine 26 | include PersonMixin, InitMixin 27 | def moreInfo; puts "So, I am an android"; end 28 | end 29 | 30 | class Cyborg < Person 31 | include MachineMixin, InitMixin 32 | def moreInfo; puts "So, I am an cyborg"; end 33 | end 34 | 35 | # ====== main ======= 36 | # =================== 37 | 38 | p = Person.new 39 | puts "=====================" 40 | m = Machine.new 41 | puts "=====================" 42 | a = Android.new 43 | puts "=====================" 44 | c = Cyborg.new 45 | puts "=====================" 46 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/multiple/multiple2.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | module PersonMixin 4 | def info; puts "my name is #{@name}"; end 5 | end 6 | 7 | module MachineMixin 8 | def info; puts "my serial number: #{@serial}"; end 9 | end 10 | 11 | class Person 12 | include PersonMixin 13 | def initialize(name) 14 | @name = name 15 | end 16 | end 17 | 18 | class Machine 19 | include MachineMixin 20 | def initialize(serial) 21 | @serial = serial 22 | end 23 | end 24 | 25 | class Android < Machine 26 | alias :parentinfo :info 27 | include PersonMixin 28 | alias :info2 :info 29 | def initialize(name, serial) 30 | super(serial) 31 | @name = name 32 | end 33 | def info 34 | parentinfo 35 | info2 36 | end 37 | end 38 | 39 | class Cyborg < Person 40 | alias :parentinfo :info 41 | include MachineMixin 42 | alias :info2 :info 43 | def initialize(name, serial) 44 | super(name) 45 | @serial = serial 46 | end 47 | def info 48 | parentinfo 49 | info2 50 | end 51 | end 52 | 53 | # ====== main ======= 54 | # =================== 55 | 56 | p = Person.new("person1") 57 | p.info 58 | puts "=====================" 59 | m = Machine.new(21) 60 | m.info 61 | puts "=====================" 62 | a = Android.new("android1", 28) 63 | a.info 64 | puts "=====================" 65 | c = Cyborg.new("cyborg1", 32) 66 | c.info 67 | puts "=====================" 68 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/single/app.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | require_relative "person" 4 | require_relative "student" 5 | require_relative "professor" 6 | require_relative "gradstudent" 7 | 8 | pe = Person.new("person1") 9 | st = Student.new("student1", 15) 10 | pr = Professor.new("professor1") 11 | 12 | pe.info 13 | st.info 14 | pr.info 15 | pr.info1 16 | 17 | gs = GradStudent.new("gradstudent1", 14) 18 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/single/gradstudent.rb: -------------------------------------------------------------------------------- 1 | require_relative "student" 2 | require_relative "person" 3 | 4 | class GradStudent < Student 5 | 6 | alias_method :parent_info, :info 7 | 8 | def initialize(name, grade) 9 | super(name, grade) 10 | 11 | puts "======================" 12 | parent_info # calling info of the parent class 13 | info # calling info of the current class 14 | Person.instance_method(:info).bind(self).call 15 | end 16 | 17 | def info 18 | puts "GradStudent info" 19 | end 20 | 21 | end 22 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/single/person.rb: -------------------------------------------------------------------------------- 1 | class Person 2 | 3 | def initialize(name) 4 | @name = name 5 | end 6 | 7 | def info 8 | puts "My name: #@name" 9 | end 10 | 11 | private 12 | def info1 13 | puts "Person info1" 14 | end 15 | 16 | end 17 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/single/professor.rb: -------------------------------------------------------------------------------- 1 | require_relative "person" 2 | 3 | class Professor < Person 4 | 5 | def info1 6 | puts "Professor info1" 7 | end 8 | 9 | end 10 | -------------------------------------------------------------------------------- /codes/ruby/inheritance/single/student.rb: -------------------------------------------------------------------------------- 1 | require_relative "person" 2 | 3 | class Student < Person 4 | 5 | def initialize(name, grade) 6 | super(name) 7 | @grade = grade 8 | end 9 | 10 | def info 11 | super 12 | puts "My grade: #@grade" 13 | end 14 | 15 | end 16 | -------------------------------------------------------------------------------- /codes/ruby/polymorphism/overloading.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | class Person 4 | def read(text=nil) 5 | if text == nil then 6 | puts "I am reading" 7 | else 8 | if text.is_a?(String) then 9 | puts "A text: #{text}" 10 | end 11 | end 12 | end 13 | end 14 | 15 | class Student < Person 16 | def read(*args) 17 | case args.length 18 | when 0 19 | super 20 | when 1 21 | super(args[0]) 22 | when 2 23 | puts "I am reading: " + args[0].to_s + " for " + args[1].to_s + " times" 24 | end 25 | end 26 | end 27 | 28 | puts "PERSON" 29 | pe = Person.new 30 | pe.read 31 | pe.read("I am a person") 32 | 33 | puts "STUDENT" 34 | st = Student.new 35 | st.read 36 | st.read("I am a person") 37 | st.read("a book", 5) 38 | -------------------------------------------------------------------------------- /codes/ruby/polymorphism/overriding.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | class Person 4 | def talk() 5 | puts "I am a person" 6 | end 7 | end 8 | 9 | class Student < Person 10 | def talk() 11 | puts "I am a student" 12 | end 13 | end 14 | 15 | pe = Person.new 16 | st = Student.new 17 | pe.talk # I am a person 18 | st.talk # I am a student 19 | -------------------------------------------------------------------------------- /codes/ruby/polymorphism/subtype.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | class Person 4 | def talk 5 | puts "I am a person" 6 | end 7 | end 8 | 9 | class Student < Person; end 10 | 11 | class Professor < Person; end 12 | 13 | class Robot 14 | def talk 15 | puts "I am a robot" 16 | end 17 | end 18 | 19 | def announce (msg, talker) 20 | print "#{msg}: " 21 | if talker.class.method_defined? :talk then 22 | talker.talk 23 | else 24 | puts "Sorry! I do not talk!" 25 | end 26 | end 27 | 28 | pe = Person.new 29 | st = Student.new 30 | pr = Professor.new 31 | ro = Robot.new 32 | 33 | announce("Person", pe) 34 | announce("Student", st) 35 | announce("Professor", pr) 36 | announce("Robot", ro) 37 | announce("a number", 25) 38 | 39 | puts "A table with different types" 40 | elements = [pe, st, pr, ro, 25] 41 | elements.each_with_index do |element, i| 42 | begin 43 | element.talk 44 | rescue Exception => e 45 | puts "The element n°#{i} does not talk()" 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /codes/ruby/polymorphism/type_manip.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby -w 2 | 3 | class Person 4 | def talk; puts "I am talking" end 5 | end 6 | 7 | class Student < Person 8 | def learn; puts "I am learning" end 9 | end 10 | 11 | class Professor < Person 12 | attr_reader :nbr 13 | def initialize; @nbr = 5 end 14 | def teach; puts "I am teaching" end 15 | end 16 | 17 | module LearnerMixin 18 | def learn; puts "I am a learner" end 19 | end 20 | 21 | class Robot 22 | include LearnerMixin 23 | end 24 | 25 | pe = Person.new 26 | st = Student.new 27 | pr = Professor.new 28 | ro = Robot.new 29 | 30 | puts "==========================" 31 | puts "Object type" 32 | puts "==========================" 33 | puts "pe.class: " + pe.class.to_s 34 | puts "st.class: " + st.class.to_s 35 | puts "pr.class: " + pr.class.to_s 36 | puts "ro.class: " + ro.class.to_s 37 | puts "25.class: " + 25.class.to_s 38 | 39 | puts "==========================" 40 | puts "Instance of" 41 | puts "==========================" 42 | puts "pe.is_a?(Person): " + pe.is_a?(Person).to_s 43 | puts "st.is_a?(Person): " + st.is_a?(Person).to_s 44 | puts "st.kind_of?(Person): " + st.kind_of?(Person).to_s 45 | puts "st.instance_of?(Person): " + st.instance_of?(Person).to_s 46 | puts "25.is_a?(Person): " + 25.is_a?(Person).to_s 47 | puts "st.is_a?(Student): " + st.is_a?(Student).to_s 48 | puts "st.instance_of?(Student): " + st.instance_of?(Student).to_s 49 | puts "st.is_a?(Professor): " + st.is_a?(Professor).to_s 50 | puts "ro.is_a?(Robot): " + ro.is_a?(Robot).to_s 51 | puts "ro.is_a?(Student): " + ro.is_a?(Student).to_s 52 | puts "ro.is_a?(LearnerMixin): " + ro.is_a?(LearnerMixin).to_s 53 | puts "ro.instance_of?(LearnerMixin): " + ro.instance_of?(LearnerMixin).to_s 54 | puts "ro.kind_of?(LearnerMixin): " + ro.kind_of?(LearnerMixin).to_s 55 | 56 | puts "==========================" 57 | puts "Member existance" 58 | puts "==========================" 59 | 60 | def testMembers (msg, obj) 61 | print "#{msg}: " 62 | if obj.class.method_defined? :learn then obj.learn 63 | else puts "Method learn does not exist!" end 64 | 65 | print "#{msg}: " 66 | if obj.class.method_defined? :nbr then puts "Field nbr = #{obj.nbr}" 67 | else puts "Field nbr does not exist!" end 68 | end 69 | 70 | testMembers("Person", pe) 71 | testMembers("Student", st) 72 | testMembers("Professor", pr) 73 | testMembers("Robot", ro) 74 | testMembers("a number", 25) 75 | -------------------------------------------------------------------------------- /img/C++.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/C++.pdf -------------------------------------------------------------------------------- /img/Java.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/Java.pdf -------------------------------------------------------------------------------- /img/Javascript.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/Javascript.pdf -------------------------------------------------------------------------------- /img/Lua.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/Lua.pdf -------------------------------------------------------------------------------- /img/PHP.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/PHP.pdf -------------------------------------------------------------------------------- /img/Python.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/Python.pdf -------------------------------------------------------------------------------- /img/Ruby.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/Ruby.pdf -------------------------------------------------------------------------------- /img/book.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/book.jpeg -------------------------------------------------------------------------------- /img/by-sa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/by-sa.png -------------------------------------------------------------------------------- /img/by.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/by.png -------------------------------------------------------------------------------- /img/cc-by-sa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/cc-by-sa.png -------------------------------------------------------------------------------- /img/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/cover.jpg -------------------------------------------------------------------------------- /img/sa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/projeduc/oop4coders/4fba9cbcad03760c8ec6295aa0e448543ac2fe92/img/sa.png -------------------------------------------------------------------------------- /tools/install: -------------------------------------------------------------------------------- 1 | sudo apt-get install pdf2djvu 2 | sudo apt-get install ebook2epub 3 | -------------------------------------------------------------------------------- /tools/publish.sh: -------------------------------------------------------------------------------- 1 | 2 | pdf2djvu -o oop4coders.djvu ../book/oop4coders.pdf 3 | ebook2epub ../book/oop4coders.pdf oop4coders.epub 4 | --------------------------------------------------------------------------------