This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
|
cpp:templates [2019/05/14 10:24] gthanos |
cpp:templates [2020/05/30 17:45] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Templates ====== | ||
| - | - [[cpp:: | ||
| - | - [[cpp:: | ||
| - | - [[cpp:: | ||
| - | |||
| - | Στα παραδείγματα που ακολουθούν χρησιμοποιούμε τη βοηθητική κλάση **Student** που δίνεται παρακάτω: | ||
| - | |||
| - | <code cpp Student.hpp> | ||
| - | |||
| - | #ifndef _STUDENT_HPP_ | ||
| - | #define _STUDENT_HPP_ | ||
| - | # | ||
| - | |||
| - | class Student { | ||
| - | char *name; | ||
| - | int aem; | ||
| - | | ||
| - | public: | ||
| - | Student(const char *name, int aem); | ||
| - | Student(); | ||
| - | Student(const Student& | ||
| - | Student(const Student* ); | ||
| - | ~Student(); | ||
| - | char *getName() const; | ||
| - | int getAEM() const; | ||
| - | void setName(const char *name); | ||
| - | void setAEM(int aem); | ||
| - | friend std:: | ||
| - | bool operator> | ||
| - | Student& | ||
| - | }; | ||
| - | |||
| - | Student:: | ||
| - | this-> | ||
| - | strcpy(this-> | ||
| - | this-> | ||
| - | } | ||
| - | |||
| - | Student:: | ||
| - | name = new char [strlen(st.name) + 1]; | ||
| - | strcpy(name, | ||
| - | aem = st.aem; | ||
| - | } | ||
| - | |||
| - | Student:: | ||
| - | name = new char [strlen(st-> | ||
| - | strcpy(name, | ||
| - | aem = st->aem; | ||
| - | } | ||
| - | |||
| - | Student:: | ||
| - | this-> | ||
| - | this-> | ||
| - | } | ||
| - | |||
| - | Student:: | ||
| - | if(name != nullptr) | ||
| - | delete []name; | ||
| - | } | ||
| - | |||
| - | char* Student:: | ||
| - | return name; | ||
| - | } | ||
| - | |||
| - | int Student:: | ||
| - | return aem; | ||
| - | } | ||
| - | |||
| - | void Student:: | ||
| - | if(this-> | ||
| - | delete this-> | ||
| - | this-> | ||
| - | strcpy(this-> | ||
| - | } | ||
| - | |||
| - | void Student:: | ||
| - | this-> | ||
| - | } | ||
| - | |||
| - | Student& | ||
| - | if(name != nullptr) | ||
| - | delete name; | ||
| - | name = new char [strlen(st.name) + 1]; | ||
| - | strcpy(name, | ||
| - | aem = st.aem; | ||
| - | return *this; | ||
| - | } | ||
| - | |||
| - | std:: | ||
| - | if(st.name != nullptr) | ||
| - | out << st.name << " " << st.aem; | ||
| - | return out; | ||
| - | } | ||
| - | |||
| - | bool Student:: | ||
| - | if(aem > st.aem) | ||
| - | return true; | ||
| - | return false; | ||
| - | } | ||
| - | #endif | ||
| - | </ | ||