例子 vc6.0 编译通过

来源:互联网 发布:橙名网络 编辑:程序博客网 时间:2024/05/16 14:38

dma.cpp

 

#include <stdafx.h>

#include   "dma.h "
#include <cstring>
#include <string>


//baseDMA   methods
baseDMA::baseDMA(const   char   *l,int   r)
{
        label=new   char[strlen(l)+1];
        strcpy(label,l);
        rating=r;
}

baseDMA::baseDMA(const   baseDMA   &   rs)
{
        //label=new   char[strlen(l)+1];
     label=new   char[strlen(rs.label)+1];
        strcpy(label,rs.label);
        rating=rs.rating;
}

baseDMA::~baseDMA()
{
        delete   []   label;
}

baseDMA   &   baseDMA::operator=(const   baseDMA   &rs)
{
        if   (this==&rs)
                return   *this;
        delete   []   label;
        label=new   char[strlen(rs.label)+1];
        strcpy(label,rs.label);
        rating=rs.rating;
        return   *this;
}

std::ostream   &operator <<(std::ostream   &os,const   baseDMA   &rs)
{
        os << "label   : " <<rs.label << ",   rating: " <<rs.rating;
        return   os;
}

//lacksDMA   methods
//lacksDMA(const   char   *   c,const   char   *   l,int   r):baseDMA(l,r)
lacksDMA::lacksDMA(const   char   *   c,const   char   *   l,int   r):baseDMA(l,r)
{
        strncpy(color,c,39);
        color[39]= '\0 ';
}


lacksDMA::lacksDMA(const   char   *c,const   baseDMA   &rs):baseDMA(rs)
{
        strncpy(color,c,COL_LEN   -1);
        color[COL_LEN-1]= '\0 ';
}

std::ostream   &operator <<(std::ostream   &   os,const   lacksDMA   &   ls)
{
        os <<(const   baseDMA   &)ls;
        os << "color:   " <<ls.color <<std::endl;
        return   os;
}

//hasDMA   methods
hasDMA::hasDMA(const   char   *s,const   char   *l,int   r):baseDMA(l,r)
{
        style=new   char[strlen(s)+1];
        strcpy(style,s);
}

hasDMA::hasDMA(const   char   *s,const   baseDMA   &rs):baseDMA(rs)
{
        style=new   char[strlen(s)+1];
        strcpy(style,s);
}

hasDMA::hasDMA(const   hasDMA   &   hs):baseDMA(hs)
{
        style=new   char[strlen(hs.style)+1];
        strcpy(style,hs.style);
}

hasDMA::~hasDMA()
{
        delete   []   style;
}

hasDMA   &   hasDMA::operator=(const   hasDMA   &   hs)
{
        if(this==&hs)
                return   *this;
        baseDMA::operator=(hs);
        style=new   char[strlen(hs.style)+1];
        strcpy(style,hs.style);
        return   *this;
}

std::ostream   &   operator <<(std::ostream   &os,const   hasDMA   &hs)
{
        os <<(const   baseDMA   &)   hs;
        os << "style:   " <<hs.style <<std::endl;
        return   os;
}

 

 

//dma.h

#ifndef   DMA_H_
#define   DMA_H_
#include <iostream>

class   baseDMA
{
private:
        char   *   label;
        int   rating;
public:
        baseDMA(const   char   *   l= "null ",int   r=0);
        baseDMA(const   baseDMA   &   rs);
        virtual   ~baseDMA();
        baseDMA   &operator   =(const   baseDMA   &   rs);
        friend   std::ostream   &   operator <<(std::ostream   &os,const   baseDMA   &rs);
};


class   lacksDMA:public   baseDMA
{
private:
        enum   {COL_LEN=40};
        char   color[COL_LEN];
public:
        lacksDMA(const   char   *   c= "blank ",const   char   *   l= "null ",
                                int   r=0);
        lacksDMA(const   char   *c,const   baseDMA   &rs);
        friend   std::ostream   &operator <<(std::ostream   &os,
                                                const   lacksDMA   &rs);
};

class   hasDMA:public   baseDMA
{
private:
        char   *   style;
public:
        hasDMA(const   char   *s= "none ",const   char   *l= "null ",
                        int   r=0);
        hasDMA(const   char   *s,const   baseDMA   &rs);
        hasDMA(const   hasDMA   &   hs);
        ~hasDMA();
        hasDMA   &operator=(const   hasDMA   &   hs);
        friend   std::ostream   &   operator <<(std::ostream   &os,const   hasDMA   &hs);
};

#endif

 

 

//调用测试
// dd.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include "dma.h"
#include <tchar.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 char ch;
    //using std::cout;
   // using std::endl;
    baseDMA shirt("Protabelly",8);
    lacksDMA balloon("red","Blimpo",4);
    hasDMA map("Mercator","Buffalo Keys",5);
    cout<<shirt<<endl;
    cout<<balloon<<endl;
    cout<<map<<endl;
    lacksDMA balloon2(balloon);
    hasDMA map2;
    map2=map;
    cout<<balloon2<<endl;
    cout<<map2<<endl;
 //std::cin>>ch;
 cin>>ch;
    return 0;
}

 

 

 

原创粉丝点击