#pragma once
#ifndef AXXF_XXXX_ISUBJECT_H
#define AXXF_XXXX_ISUBJECT_H
#include "IObserver.h"
class IObserver;
class ISubject
{
public:
ISubject(){}
virtual char * GetContent() const = 0 ;
virtual void Add(IObserver *observer) = 0;
virtual void Delete(IObserver *observer) = 0;
virtual void Talk() = 0;
~ISubject(){}
};
#endif
#pragma once
#include "ISubject.h"
#include <list>
#include <iterator>
using namespace std;
class CTeacher:public ISubject
{
private:
char m_strName[40];
char m_content[40];
list<IObserver*> m_observers;
public:
CTeacher(void);
CTeacher(char *pContent);
CTeacher(char *pName, char *pContent);
~CTeacher(void);
virtual char * GetName() const;
virtual char * GetContent() const;
virtual void SetContent(char *pContent);
virtual void Add( IObserver *observer );
virtual void Delete( IObserver *observer );
virtual void Talk();
};
#include "Teacher.h"
CTeacher::CTeacher(void)
{
}
CTeacher::CTeacher(char *pContent)
{
strcpy(this->m_content, pContent);
}
CTeacher::CTeacher(char *pName, char *pContent)
{
strcpy(this->m_content, pContent);
strcpy(this->m_strName, pName);
}
CTeacher::~CTeacher(void)
{
}
void CTeacher::Add( IObserver *observer )
{
this->m_observers.push_back(observer);
}
void CTeacher::Delete( IObserver *observer )
{
this->m_observers.remove(observer);
}
void CTeacher::Talk()
{
list<IObserver*>::iterator observer;
for (observer = m_observers.begin(); observer!=m_observers.end(); observer++)
{
((IObserver*)(*observer))->Listen(this);
}
}
void CTeacher::SetContent( char *pContent )
{
strcpy(m_content,pContent);
}
char * CTeacher::GetContent() const
{
int len =strlen(m_content);
char *pContent = (char *)malloc(sizeof(char)*(len+1));
strcpy(pContent,m_content);
return pContent;
}
char * CTeacher::GetName() const
{
int len =strlen(m_strName);
char *pName = (char *)malloc(sizeof(char)*(len+1));
strcpy(pName,m_strName);
return pName;
}
//#ifndef AFX_XXXXXX_HHHHHHHHHHHHH_IOBSERVER_H
//#define AFX_XXXXXX_HHHHHHHHHHHHH_IOBSERVER_H
#pragma once
#include "ISubject.h"
class ISubject;
class IObserver
{
public:
IObserver(){};
virtual void Listen(ISubject *suj) = 0;
~IObserver(){};
};
//#endif
#pragma once
#include "IObserver.h"
#include <iostream>
using namespace std;
class CStudent:public IObserver
{
public:
CStudent(void);
CStudent(char *pName);
~CStudent(void);
char m_strName[40];
virtual void Listen( ISubject *suj );
};
#include "Student.h"
#include "Teacher.h"
CStudent::CStudent(void)
{
}
CStudent::CStudent( char *pName )
{
strcpy(m_strName, pName);
}
CStudent::~CStudent(void)
{
}
void CStudent::Listen( ISubject *suj )
{
CTeacher *pTeacher = (CTeacher *)suj;
cout <<this->m_strName<<":" << pTeacher->GetName()<< "say:" << suj->GetContent() << endl;
}
//#include "Function.h"
//#include <vector>
//#include "Student.h"
//#include "Teacher.h"
//void main()
//{
//
//
// CStudent s1 = CStudent("甘全福");
// CStudent s2 =CStudent("李刚");
//
// CTeacher teacher = CTeacher("梁毅", "上课了,请同学们打开课本");
// teacher.Add(&s1);
// teacher.Add(&s2);
// teacher.Talk();
// teacher.SetContent("学*中国*代史");
// teacher.Talk();
//
// teacher.Delete(&s2);
// teacher.Talk();
//
// int tem ;
// cin >>tem;
//}
#include <iostream>
using namespace std;
class ReturnBase
{
protected:
int a;
public:
ReturnBase()
{
}
ReturnBase(int a)
{
this->a = a;
}
virtual void print_info()
{
cout << a << endl;
}
virtual ~ReturnBase()
{
}
};
class ReturnDerived : public ReturnBase
{
public:
ReturnDerived()
{
}
ReturnDerived(int a)
{
this->a = a;
}
virtual void print_info()
{
cout << a << endl;
}
};
class Base
{
private:
ReturnBase* rb;
public:
Base()
{
rb = new ReturnBase(10);
}
virtual ReturnBase* some_fun()
{
return rb;
}
virtual ~Base()
{
delete rb;
}
};
class Derived : public Base
{
private:
ReturnDerived* rd;
public:
Derived()
{
rd = new ReturnDerived(20);
}
ReturnDerived* some_fun()
{
return rd;
}
virtual ~Derived()
{
delete rd;
}
};
int main(int argc, char** argv)
{
Base* pb = new Derived;
(pb->some_fun())->print_info();
int tem;
cin >> tem;
return 0;
}