总共有五个文件,Graph.h ,Graph.cpp,diamond.h.diamond.cpp,new.cpp
编译成功的是我把diamond类的声明和定义(Graph的声明和定义也放在了一个文件里,但是没有影响)都放在diamond.cpp里。
new.cpp 就是包含了"Graph.h"和"diamond.",然后用diamond创建了一个对象,调用ShapePrint()函数。
Graph.h
#ifndef _Graph_H_
#define _Graph_H_
class shape
{
public:
void ShapePrint();
void ShapePrint(int x);
void ShapePrint(int x,int y);
int GetLength_x();
int GetLength_y();
protected:
void SetLength_x(int length_x);
void SetLength_y(int length_y);
private:
int length_x ;
int length_y ;
};
#endif
diamond.h
#include <iostream>
#include <iomanip>
#include "Graph.h"
#ifndef _diamond_
#define _diamond_
class diamond:public shape{
public:
diamond();
diamond(int x);
int GetLength();
protected:
void SetLength(int l);
private:
int length;
};
#endif
diamond.cpp
#include "diamond.h"
using namespace std;
void diamond::SetLength(int l)
{
this->length = l;
}
int diamond::GetLength()
{
return length;
}
diamond::diamond()
{
SetLength(7);
}
diamond::diamond(int l)
{
SetLength(l);
}
void diamond::ShapePrint()
{
int length=GetLength();
for(int i=1;i<=length;++i)
{
cout<<setw(length-i+1)<<"*";
if(i!=1)
cout<<setw(2*i-2)<<"*"<<endl;
else
cout<<endl;
}
for(int k=1;k<=length-1;k++)
{
cout<<setw(k+1)<<"*";
if(k==length-1)
cout<<endl;
else
cout<<setw(2*(length-k)-2)<<"*"<<endl;
}
}
void diamond::ShapePrint(int x)
{
int length=GetLength();
for(int i=1;i<=length;++i)
{
cout<<setw(x+(length-i+1))<<"*";
if(i!=1)
cout<<setw(2*i-2)<<"*"<<endl;
else
cout<<endl;
}
for(int k=1;k<=length-1;k++)
{
cout<<setw(x+k+1)<<"*";
if(k==length-1)
cout<<endl;
else
cout<<setw(2*(length-k)-2)<<"*"<<endl;
}
}
void diamond::ShapePrint(int x,int y)
{
int length=GetLength();
for(int i=1;i<=y;i++)
cout<<endl;
for(int i=1;i<=length;++i)
{
cout<<setw(x+(length-i+1))<<"*";
if(i!=1)
cout<<setw(2*i-2)<<"*"<<endl;
else
cout<<endl;
}
for(int k=1;k<=length-1;k++)
{
cout<<setw(x+k+1)<<"*";
if(k==length-1)
cout<<endl;
else
cout<<setw(2*(length-k)-2)<<"*"<<endl;
}
}