Commit d1d98a22 authored by Felix Huther's avatar Felix Huther
Browse files

cin >> operator gebaut

parent dab60261
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
Lecturer.cpp \
hdamember.cpp \
lecturemember.cpp \
main.cpp \
student.cpp
HEADERS += \
Lecturer.h \
hdamember.h \
lecturemember.h \
student.h
#include "Lecturer.h"
Lecturer::Lecturer(std::string name) : hdaMember(name)
{
}
void Lecturer::talk()
{
static int count = 0;
if(count < 5){
std::cout << "PG2 ist wichtig!\n";
count++;
talk();
}
else{
count = 0;
}
}
void Lecturer::PrintName()
{
std::cout << "My name is " << name << " and I'm teaching " << std::endl;
}
#ifndef LECTURER_H
#define LECTURER_H
#include "hdamember.h"
class Lecturer : virtual public hdaMember
{
public:
Lecturer(std::string name);
void talk();
void PrintName()override;
};
#endif // LECTURER_H
#include "hdamember.h"
hdaMember::hdaMember(std::string name)
:isHappy(true),name(name)
{
}
hdaMember::~hdaMember()
{
}
void hdaMember::PrintName()
{
std::cout << "My name is " << name << std::endl;
}
#ifndef HDAMEMBER_H
#define HDAMEMBER_H
#include <iostream>
class hdaMember
{
public:
hdaMember();
explicit hdaMember(std::string name);
virtual ~hdaMember(); // really important, otherwise we get alot of data leaks
virtual void PrintName();
protected:
const bool isHappy;
std::string name;
};
#endif // HDAMEMBER_H
#include "lecturemember.h"
lectureMember::lectureMember(std::string name,bool isListening)
:hdaMember(name),Student(name,isListening),Lecturer(name)
{
}
void lectureMember::PrintName()
{
if(typeid (this) == typeid (Student)){
Student::PrintName();
}
else if(typeid (this)== typeid (Lecturer)){
Lecturer::PrintName();
}
else std::cout << "My name is " << name <<
" and i'm a simple member\n";
}
#ifndef LECTUREMEMBER_H
#define LECTUREMEMBER_H
#include "student.h"
#include "Lecturer.h"
class lectureMember : public Student, public Lecturer
{
public:
lectureMember(std::string name,bool isListening);
// hdaMember interface
public:
void PrintName() override;
};
#endif // LECTUREMEMBER_H
#include <iostream>
#include <vector>
#include "student.h"
#include "Lecturer.h"
#include "hdamember.h"
#include "lecturemember.h"
using namespace std;
int main()
{
Student stud1;
cin >> stud1;
hdaMember* dozent = new Lecturer ("Herr Mayer");
hdaMember* member = new lectureMember("SimpleGuy",true);
std::vector<hdaMember*> hoersaal = { dozent,member};
for(auto members: hoersaal){
members->PrintName();
}
Student* Felix = new Student("Felix",true,3);
Student* Sven = new Student("Sven",true,3);
Student* Atta = new Student("Atta",false);
std::vector<Student*> students ={Felix,Sven,Atta};
for(auto student : students)
{
cout << *(student) << endl;
++(*student);
}
for(auto student : students) // every student got up one semester check
{
cout << *(student) << endl;
++(*student);
}
return 0;
}
#include "student.h"
Student::Student()
:hdaMember("noName")
{
}
Student::Student(std::string name, bool isListening)
: hdaMember(name),isListening(isListening),currentSemester(0)
{
}
Student::Student(std::string name, bool isListening, int currentSemester)
:hdaMember(name),isListening(isListening),currentSemester(currentSemester)
{
}
void Student::listens()const
{
isListening? std::cout << "aha\n" : std::cout << "zZz\n";
}
void Student::PrintName()
{
std::cout << "My name is " << name << " and i study " << std::endl;
}
bool Student::operator==(const Student &compareStudent)const
{
return (compareStudent.currentSemester == this->currentSemester);
}
bool Student::operator<(const Student &other) const
{
return this->currentSemester < other.currentSemester;
}
//prefix
Student& Student::operator++()
{
++currentSemester;
return *this;
}
//postfix
Student Student::operator++(int){
Student temp = *this;
++temp.currentSemester;
return temp;
}
void Student::setCurrentSemester(int newCurrentSemester)
{
currentSemester = newCurrentSemester;
}
int Student::getCurrentSemester() const
{
return currentSemester;
}
std::ostream& operator<<(std::ostream& os, const Student& student)
{
os << "Name of Student : " << student.name << std::endl;
os << "Semester : " << student.currentSemester;
return os;
}
std::istream& operator>>(std::istream& is,Student& student)
{ std::cout << "Enter Name and Semester\n";
is >> student.name >> student.currentSemester;
return is;
}
#ifndef STUDENT_H
#define STUDENT_H
#include "hdamember.h"
class Student : virtual public hdaMember
{
public:
Student();
Student(std::string name, bool isListening);
Student(std::string name, bool isListening,int CurrentSemester);
void listens() const ;
void PrintName() override;
bool operator==(const Student& compareStudent) const;
bool operator<(const Student& other) const;
Student& operator++();
Student operator++(int);
friend std::ostream& operator<<(std::ostream& ,const Student&);
friend std::istream& operator>>(std::istream& ,Student&);
void setCurrentSemester(int newCurrentSemester);
int getCurrentSemester() const;
private:
bool isListening;
int currentSemester;
};
#endif // STUDENT_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment