User Tools

Site Tools


cpp:templates

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cpp:templates [2019/05/02 13:30] – created gthanoscpp:templates [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 2: Line 2:
  
   - [[cpp::function_templates| Templates συναρτήσεων]]   - [[cpp::function_templates| Templates συναρτήσεων]]
-  - [[cpp::function_templates_using_classes|Templates συναρτήσεων με παραμέτρους κλάσεις]] 
   - [[cpp::class_templates| Templates κλάσεων]]   - [[cpp::class_templates| Templates κλάσεων]]
-  - [[cpp::class_templates_with_non_type_parameters| Templates με αριθμητικές παραμέτρους]] 
   - [[cpp::class_templates_specialization| Εξειδίκευση ενός template]]   - [[cpp::class_templates_specialization| Εξειδίκευση ενός template]]
  
 +Στα παραδείγματα που ακολουθούν χρησιμοποιούμε τη βοηθητική κλάση **Student** που δίνεται παρακάτω:
 +
 +<code cpp Student.hpp>
 +
 +#ifndef _STUDENT_HPP_
 +#define _STUDENT_HPP_
 +#include<cstring>
 +
 +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::ostream& operator<<(std::ostream& out, const Student & st);
 +  bool operator>(const Student& st) const;
 +  bool operator<(const Student& st) const;
 +  Student& operator=(const Student& st);
 +};
 +
 +Student::Student(const char *name, int aem) {
 +  this->name = new char [strlen(name) + 1];
 +  strcpy(this->name, name);
 +  this->aem = aem;
 +}
 +
 +Student::Student(const Student& st) {
 +  name = new char [strlen(st.name) + 1];
 +  strcpy(name, st.name);
 +  aem = st.aem;
 +}
 +
 +Student::Student(const Student* st) {
 +  name = new char [strlen(st->name) + 1];
 +  strcpy(name, st->name);
 +  aem = st->aem;
 +}
 +
 +Student::Student() {
 +  this->name = nullptr;
 +  this->aem = 0;
 +}
 +
 +Student::~Student() {
 +  if(name != nullptr)
 +    delete []name;
 +}
 +
 +char* Student::getName() const {
 +  return name;
 +}
 +
 +int Student::getAEM() const {
 +  return aem;
 +}
 +
 +void Student::setName(const char *name) {
 +  if(this->name != nullptr)
 +    delete this->name;
 +  this->name = new char [strlen(name) + 1];
 +  strcpy(this->name, name);
 +}
 +
 +void Student::setAEM(int aem) {
 +  this->aem = aem;
 +}
 +
 +Student& Student::operator=(const Student& st) {
 +  if(name != nullptr)
 +    delete name;
 +  name = new char [strlen(st.name) + 1];
 +  strcpy(name, st.name);
 +  aem = st.aem;
 +  return *this;
 +}
 +
 +std::ostream& operator<<(std::ostream& out, const Student& st) {
 +  if(st.name != nullptr)
 +    out << st.name << " " << st.aem;
 +  return out;
 +}
 +
 +bool Student::operator>(const Student& st) const {
 +  if(aem > st.aem)
 +    return true;
 +  return false;
 +}
 +
 +bool Student::operator<(const Student& st) const {
 +  if(aem < st.aem)
 +    return true;
 +  return false;
 +}
 +#endif
 +</code>
cpp/templates.1556803825.txt.gz · Last modified: 2019/05/02 12:30 (external edit)