User Tools

Site Tools


cpp:stl:intro

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cpp:stl:intro [2020/05/29 15:47] – created gthanoscpp:stl:intro [2023/05/30 18:30] (current) – [Η κλάση Student] gthanos
Line 4: Line 4:
  
 Βασικό χαρακτηριστικό της //STL// είναι ότι οι κλάσεις (//containers// και //iterators//) και οι συναρτήσεις των αλγορίθμων είναι γενικευμένες, ώστε να μπορούν να εφαρμοστούν με ασφάλεια σε οποιονδήποτε τύπο δεδομένων. Για να το επιτύχουν αυτό, χρησιμοποιούν [[cpp:templates|templates]]. Βασικό χαρακτηριστικό της //STL// είναι ότι οι κλάσεις (//containers// και //iterators//) και οι συναρτήσεις των αλγορίθμων είναι γενικευμένες, ώστε να μπορούν να εφαρμοστούν με ασφάλεια σε οποιονδήποτε τύπο δεδομένων. Για να το επιτύχουν αυτό, χρησιμοποιούν [[cpp:templates|templates]].
 +
 +===== Η κλάση Student =====
 +
 +Στα παραδείγματα που ακολουθούν  στις επόμενες ενότητες συχνά χρησιμοποιείται η παρακάτω κλάση //Student//.
 +
 +<code cpp Student.hpp>
 +#ifndef _STUDENT_HPP_
 +#define _STUDENT_HPP_
 +#include <cstring>
 +#include <iostream>
 +
 +class Student {
 +private:
 +  char *name;
 +  int aem;
 +public:
 +  Student();
 +  Student(const char *name, int aem);
 +  Student(const Student& );
 +  ~Student();
 +  
 +  char* getName() const;
 +  int getAEM() const;  
 +  void setName(char*);
 +  void setAEM(int);
 +  
 +  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;
 +  std::cerr << "Argu Construct : " << *this << std::endl;
 +}
 +
 +Student::Student(const Student& st) {
 +  name = new char [strlen(st.name) + 1];
 +  strcpy(name, st.name);
 +  aem = st.aem;
 +  std::cerr << "Copy Construct : " << *this << std::endl;
 +}
 +
 +Student::Student() {
 +  this->name = nullptr;
 +  this->aem = 0;
 +}
 +
 +Student::~Student() {
 +  if(name != nullptr) {
 +    std::cerr << "Destruct: " << *this << std::endl;
 +    delete []name;
 +  }
 +}
 +
 +char* Student::getName() const {
 +  return name;
 +}
 +
 +int Student::getAEM() const {
 +  return aem;
 +}
 +
 +void Student::setName(char* name) {
 +  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;
 +  std::cerr << "Copy : " << *this << std::endl;
 +  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/stl/intro.1590767273.txt.gz · Last modified: 2020/05/29 14:47 (external edit)