设计模式中的相关基础类文件

这个文件是设计模式中可能要用到的数据结构,将它根据需要补全,在后绪的学习中将会根据需要慢慢补,不断更新中。

 

//  BasicClass.h: interface for the BasicClass class.
//
//

#if  !defined(AFX_BASICCLASS_H__459C19CB_0047_4A50_855E_B7FDCFC6F3B0__INCLUDED_)
#define  AFX_BASICCLASS_H__459C19CB_0047_4A50_855E_B7FDCFC6F3B0__INCLUDED_

#if  _MSC_VER > 1000
#pragma  once
#endif   //  _MSC_VER > 1000
//  
#include  < iostream >
/*using namespace std::;*/
using  std::cout;
using  std::ostream;
using  std::istream;
// #include <iostream.h> 
// std::list<int>

#define  DEFAULT_LIST_CAPACITY 1000
template
< class  T >   class  List;

template
< class  T >   class  ListNode // T便是一个指针
{
public:
    friend 
class List<T>;
    ListNode()
{ _data = NULL; _next = NULL; }

    ListNode(
const T& item);
    ListNode
& operator=(const ListNode& );
    T _data;
    ListNode
<T>* _next;

}
;



template
< class  T >   class  List // T便是一个指针
{
public:
    List(
long size = DEFAULT_LIST_CAPACITY){ _last = NULL;_first = NULL,_current = NULL; _size = 0; }
    List(List
& list);
    
~List(){}
    List
& operator=(const List&);

    
long Count() const
    
{
        
return _size;
    }

    T
& Get(long index) const
    
{
        ListNode
<T>* p = _first;
        
for(int i=0; i<index; i++)
        
{
            p 
= p->_next;
        }

        
return p->_data;
    }

    T
& First() const
    
{
        
return _first->_data;
    }

    ListNode
<T>* FirstPoint()
    
{
        
return _first;
    }

    T
& Last() const
    
{
        
return _last->_data;
    }

    
bool Includes(const T& data) const
    
{
        ListNode
<T>* p = _first;
        
while ( p != NULL )
        
{
            
if(data == p->_data)
            
{
                
return true;
            }

        }


        
return false;
    }


    
void Append(const T& data)
    
{
        ListNode
<T> * p = new ListNode<T>;

        
if(_current == NULL)
        
{
            _current 
= p;
            _current
->_data = data;
            
if(_first == NULL)
            
{//空链表
                _first = _last = _current;
            }

        }

        
else
        
{
            p
->_next = _current->_next;
            _current
->_next = p;
            _current 
= _current->_next;//将当前指针指向插入的节点

            
if(_current->_next == NULL)
            
{//如果是在链尾加,
                _last = _current;
            }

        }

    }

    
//     void Prepend(const T&data)
    
//     {
    
//         ListNode<T>* p = new ListNode<T>;
    
// 
    
//         if(_current == NULL)
    
//         {
    
// 
    
//         }
    
//     }


    
void Remove(const T& data)
    
{

    }

    
void RemoveLast();
    
void RemoveFirst();
    
void RemoveAll();

    T
& Top() const;
    
void Push(const T&);
    T
& Pop();

    ListNode
<T>* _first;
    ListNode
<T>* _last;
    ListNode
<T>* _current;
    
int _size;
}
;



template
< class  T >
class  Iterator
{
    friend 
class List<T>;
public:
    
virtual void First() = 0;
    
virtual void Next() = 0;
    
virtual bool IsDone() const = 0;
    
virtual T CurrentItem() const = 0;
}
;

template
< class  T > //  ListNode<equment*>*
class  ListIterator:  public  Iterator < T >
{
public:
    T _ptr;
    T _first;

    ListIterator()
{ _ptr = 0; _first = 0; }
    ListIterator(T alist)
{_ptr = alist; _first = alist; }

    
//ListIterator(const ListIterator<T> aList& );

    
virtual void First() { _ptr = _first; }
    
virtual void Next(){ _ptr = _ptr->_next; }
    
virtual bool IsDone() constreturn _ptr != NULL ;}

    
virtual T CurrentItem() const
    
{
        
return _ptr;
    }

}
;


typedef 
float  Coord;
class  Point {
public:
    
static const Point Zero;

    Point(Coord x 
= 0.0,Coord y = 0.0);

    Coord X() 
const;//得到x值
    Coord Y() const;
    
void X(Coord x);//设置x值
    void Y(Coord y);

    Point 
operator+(const Point&);
    Point 
operator-(const Point&);
    Point 
operator*(const Point&);
    Point 
operator/(const Point&);
    
    
void operator+=(const Point&);
    
void operator-=(const Point&);
    
void operator*=(const Point&);
    
void operator/=(const Point&);

    
bool operator==(const Point&);
    
bool operator!=(const Point&);

     friend ostream
& operator<<(ostream&const Point&);
     friend istream
& operator>>(istream&, Point&);
private:
    Coord m_x;
    Coord m_y;
}
;

class  Rect
{
public:
    
static const Rect Zero;

    Rect(Coord x, Coord y, Coord w, Coord h);
    Rect(
const Point& origin, const Point& extent);

    Coord Width() 
const;
    
void Width(Coord);
    Coord Height() 
const;
    
void Height(Coord);
    Coord Left() 
const;
    
void Left(Coord);
    Coord Bottom() 
const;
    
void Bottom(Coord);

    Point
& Origin() const;
    
void Origin(const Point&);
    Point
& Extent() const;
    
void Extent(const Point&);

    
void MoveTo(const Point&);
    
void MoveBy(const Point&);

    
bool IsEmpty() const;
    
bool Contains(const Point&const;
}
;

#endif   //  !defined(AFX_BASICCLASS_H__459C19CB_0047_4A50_855E_B7FDCFC6F3B0__INCLUDED_)






//  BasicClass.cpp: implementation of the BasicClass class.
//
//

#include 
" stdafx.h "
#include 
" BasicClass.h "

Point::Point(Coord x 
/* = 0.0 */ ,Coord y  /* = 0.0 */ )
{
    m_x 
= x;
    m_y 
= y;
}


Coord Point::X() 
const
{
    
return m_x;
}


Coord Point::Y() 
const
{
    
return m_y;
}


void  Point::X(Coord x)
{
    m_x 
= x;
}


void  Point::Y(Coord y)
{
    m_y 
= y;
}


Point Point::
operator   + ( const  Point &  p)
{
    
return Point(m_x+p.m_x, m_y + p.m_y);
}


Point Point::
operator   - ( const  Point &  p)
{
    
return Point(m_x-p.m_x, m_y-p.m_y);
}


Point Point::
operator   * ( const  Point &  p)
{
    
return Point(m_x* p.m_x, m_y* p.m_y);
}


Point Point::
operator   / ( const  Point &  p)
{
    
return Point(m_x/p.m_x, m_y/p.m_y);
}



void  Point:: operator   += ( const  Point &  p)
{
    m_x 
+= p.m_x;
    m_y 
+= p.m_y;
}

// CPoint
void  Point:: operator   -= ( const  Point &  p)
{
    m_x 
-= p.m_x;
    m_y 
-= p.m_y;
}


void  Point:: operator   *= ( const  Point &  p)
{
    m_x 
*= p.m_x;
    m_y 
*= p.m_y;
}


void  Point:: operator   /= ( const  Point &  p)
{
    m_x 
/= p.m_x;
    m_y 
/= p.m_y;
}


bool  Point:: operator == ( const  Point &  p)
{
    
return (m_x == p.m_x && m_y == p.m_y);
}


bool  Point:: operator != ( const  Point &  p)
{
    
return !(*this== p);
}


ostream
&   operator << (ostream &  cout, const  Point &  p)
{
    cout
<<p.m_x<<","<<p.m_y;
    
return cout;
}


istream
&   operator >> (istream &   in , Point &  p)
{
    
in>>p.m_x>>p.m_y;
    
return in;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++ Qt设计模式(第2版)是美国萨福克大学已使用十余年的经典教程,利用跨平台开源软件开发框架Qt阐释了C++和设计模式的主要思想。全书共分四个部分:第一部分介绍C++、UML、Qt、模型-视图、SQL、XML、设计模式基础知识,目的是为零基础的C++初学者铺垫一条学习面向对象编程的快捷之路;第二部分讲解内存访问、继承等重要的C++特性,是前一部分的延伸和拓展;第三部分使用Phonon编写了一个多媒体播放器,展示了主要技术理念的应用方法;附录部分给出了C++保留关键字、Debian和Qt程序开发环境的配置等内容。每节的练习题和各章后面的复习题,既可作为课堂上的讨论题,也可进一步启发读者对于关键知识点的思考。 C++ Qt设计模式(第2版)目录 第一部分 设计模式Qt 第1章 C++简介 2 第2章 与对象 46 第3章 Qt简介 78 第4章 列表 85 第5章 函数 94 第6章 继承与多态 116 第7章 库与设计模式 163 第8章 QObject, QApplication,信号和槽 179 第9章 窗件和设计师 195 第10章 主窗口和动作 225 第11章 范型和容器 246 第12章 元对象,属性和反射编程 262 第13章 模型和视图 277 第14章 验证和正则表达式 302 第15章 XML解析 318 第16章 更多的设计模式 335 第17章 并发 353 第18章 数据库编程 376 第二部分 C++语言规范 第19章 型与表达式 386 第20章 作用域与存储 416 第21章 内存访问 431 第22章 继承详解 443 第三部分 编 程 作 业 第23章 MP3自动点唱机作业 456
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远的麦田

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值