C++学习历程(六)构造函数与析构函数(纯虚函数)


基类为图形类:包含长,宽,半径;
派生类Circle  :用来求圆的面积;
派生类Square :用来求矩形面积;

#include <iostream>
#define PI 3.1415926
using namespace std;

class Graph
{
    protected:
        int m_a,m_b;
        int m_r;
    public:
        Graph(int a,int b,int r);
        virtual void CircleArea() = 0; //纯虚函数i
        virtual void SquareArea() = 0;  
        ~Graph();
};

Graph::Graph(int a,int b,int r)
{
    cout << "Graph Consturctor!" << endl;
    m_a = a;
    m_b = b;
    m_r = r;
}

Graph::~Graph()
{
    cout << "Graph Desconstructor!" << endl;
}

class Circle : public Graph
{
    public:
        Circle(int a,int b,int r);
        void CircleArea();  //继承下来的纯虚函数必须有函数体,否则此类也会成为抽象类
        void SquareArea()    //抽象类则无法创建对象
        {
            
        }
        ~Circle();
};

Circle::Circle(int a,int b,int r) : Graph(a,b,r)
{
    cout << "Circle Constructor!" << endl;
}

void Circle::CircleArea()
{
    cout << "The Circle's mianji is : " << m_r * m_r * PI << endl;
}

Circle::~Circle()
{
    cout << "Circle Desstructor!" << endl;
}

class Square : public Graph
{
    public:
        Square(int a,int b,int r);
        void CircleArea()
        {

        }
        void SquareArea();
        ~Square();
};

Square::Square(int a,int b,int r) : Graph(a,b,r)
{
    cout << "Square Constructor!" << endl;
}

void Square::SquareArea()
{
    cout << "The Square's mianji is : " << m_a * m_b << endl; 
}

Square::~Square()
{
    cout << "Square Desconstructor!" << endl;
}


int main(int argc, char **argv)
{
    /*Circle a(3,5,7);
    Square b(3,5,8);

    a.CircleArea();
    b.SquareArea();*/
    Graph *pg = new Circle(3, 5, 7);  //向上转型,派生类地址赋值给基类指针,则基类可以调用派生类函数
    return 0;
}

纯虚函数构造函数问题,求助

04-04

我定义了如下纯虚类:rn[code=C/C++]//rn//rn//线线表的抽象基类定义rn//rn//rnrnrntemplate rnclass LinearListrn public:rn LinearList();rn ~LinearList();rn virtual int Size() const=0; //求表最大的容量rn virtual int Length() const=0; //求表长度rn virtual int Search(T x) const=0; //在表中搜索给定值xrn virtual T* getData(int i) const=0; //取第i个元素 rn virtual void setData(int i,T x)=0; //修改第i个元素的值为xrn virtual int Insert(int i,T x)=0; //在第i个表项后插入xrn virtual int Remove(int i,T& x)=0; //删除第i个表项,通过x返回rn virtual int IsEmpty() const=0; //判断表空rn virtual int IsFull() const=0; //判断表满rn virtual void input()=0; //输入表项rn virtual void output()=0 ; //输出表项rn rn rn;[/code]rnrn虽然我的纯虚函数中的构造函数没有定义,但是在继承后也不至出错吧,rn派生类:rn[code=C/C++]#includern#includern#include"LinearList.H"rnrnconst int defaultSize = 100;rnrntemplate rnclass SeqList: public LinearListrnrn protected:rn T* data;rn int maxSize;rn int last;rn void reSize(int newSize);rn public:rn SeqList(int sz=defaultSize);rn SeqList(SeqList& L);rn rn ~SeqList()rn delete[] data;rn rn int Size()constrn return maxSize;rn rn int Length()constrn return last+1;rn rn int Search(T x)const;rn rn T* getData(int i)constrn return( i > 0 && i <= last+1 )?&data[i-1]:NULL;rn rn void setData(int i, T x)rn if( i > 0 && i <= last+1 ) data[i-1]=x;rn rn int Insert(int i,T x);rn int Remove(int i,T& x);rn int IsEmpty()constreturn (last==-1)?1:0;rnrn int IsFull()constreturn (last==maxSize-1)?1:0;rnrn void input();rn void output();rn rn;rnrnrntemplate rnSeqList::SeqList(int sz=defaultSize)rnrn if(sz>0)rn rn maxSize = sz; last = -1;rn data= new T[maxSize];rn if(data==NULL)rn rn cerr<<"内存分配出错!"<rnSeqList::SeqList(SeqList& L)rnrn maxSize = L.Size();rn last=L.Length-1;rn data=new T[maxSize];rn if(data == NULL)rn cerr<<"内存分配出错!"<rnvoid SeqList::reSize(int newSize)rnrn if(newSize <= 0)rn cerr<<"无效的数组大小!"<rnint SeqList::Search(T x)constrn for(int i=0 ;i<=last; i++)rn if(data[i]==x) return i+1;rn return 0;rnrnrnrntemplate rnint SeqList::Insert(int i, T x)rnrn if(last == maxSize-1) return 0;rn if(i<0 || i>last+1 ) return 0;rn for(int j=last ; j>=i ; j--)rn data[j+1]=data[j];rn data[i]=x;rn last++;rn return 1;rnrnrntemplate rnint SeqList::Remove(int i, T &x)rnrn if(last == -1) return 0;rn if( i<1 || i>last+1) return 0;rn x=data[i-1];rn for(int j=i ;j<=last ;j++)rn data[j-1]=data[j];rn last--;rn return 1;rnrnrntemplate rnvoid SeqList::input()rnrn cout<<"开始建立顺序表,请输入表中元素最后一个的下标数:";rn while(1)rn rn cin>>last;rn if( last <= maxSize-1 ) break;rn cout<<"表最后元素下标输入有误,范围不能超过"<>data[i];rn rnrnrnrntemplate rnvoid SeqList::output()rnrn cout<<"顺序表当前最后位置为:"< temp(10);rn temp.input();rnrn temp.output();rn cout<<"请输入你要插入的位置和数据:";rn int i;rn char dat;rn cin>>i>>dat;rn rn temp.Insert(i,dat);rn temp.output();rn rn cout<<"请输入你要删除的位置:";rn cin>>i;rn temp.Remove(i,dat);rn temp.output();rn cout<<"\n删除数据:"<>dat;rn i=temp.Search(dat);rn if(i)cout<<"\n搜索成功!数据位于下标:"<::~LinearList(void)" (??1?$LinearList@D@@QAE@XZ)rnTestSeqList.obj : error LNK2001: unresolved external symbol "public: __thiscall LinearList::LinearList(void)" (??0?$LinearList@D@@QAE@XZ)rnDebug/TestSeqList.exe : fatal error LNK1120: 2 unresolved externalsrnrn我也不知道为何,虽然说这里的基类构造函数没有作用.但是我在其它应用中还是有可能需要抽象基类的构造函数的

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试