私はポリゴン計算を行うには、仮想関数を使用してプログラムを書くつもりが、私はプログラムを終了した後、BLOCK_TYPE_IS_VALIDありますよ(PHEAD - > nBlockUse)エラーBLOCK_TYPE_IS_VALID(PHEAD - > nBlockUse)エラー
// pointers to base class
#include <iostream>
#include "Polygon.h"
#include "Rectangle.h"
#include "Triangle.h"
using namespace std;
int main() {
Rectangle rect1(4, 5);
Rectangle rect2(3, 3);
Triangle tri(4, 4, false);
int triLength[3] = { 5, 4, 3 };
tri.setsideLength(triLength);
Polygon * p = &rect1;
cout << "Rectangle 1: " << endl;
cout << "\tArea: " << p->area() << endl;
cout << "\tSide: ";
p->printsideLength();
rect1.printsideLength();
cout << "\tTotal Side Length: " << p->totalsideLength() << endl;
p = &rect2;
cout << "Rectangle 2: " << endl;
cout << "\tArea: " << p->area() << endl;
cout << "\tSide: ";
p->printsideLength();
rect2.printsideLength();
cout << "\tTotal Side Length: " << p->totalsideLength() << endl;
p = &tri;
cout << "Triangle: " << endl;
cout << "\tArea: " << p->area() << endl;
cout << "\tSide: ";
p->printsideLength();
cout << "\tTotal Side Length: " << p->totalsideLength() << endl;
system("pause");
return 0;
}
ここではプログラムのmain.cppですが、プログラムは何も入力する必要はなく、結果を表示する必要があります。ここ
三クラス(CPP & H)
#ifndef Polygon_H
#define Polygon_H
#include <iostream>
using namespace std;
class Polygon{
private:
int noOfSide;
bool isAllSideEqual;
int* sideLength;
public:
Polygon();
Polygon(int n,bool s);
~Polygon();
void setsideLength(int* sl);
void printsideLength();
int totalsideLength();
virtual int area();
};
#endif
あります。
#include "Polygon.h"
#include <iostream>
using namespace std;
Polygon::Polygon(){
noOfSide = 3;
isAllSideEqual = false;
sideLength = new int[noOfSide];
sideLength = &sideLength[noOfSide];
};
Polygon::Polygon(int n,bool s){
if (n<3)
{
noOfSide = 3;
isAllSideEqual = false;}
else
{
noOfSide = n;
isAllSideEqual = s;
};
sideLength = new int[noOfSide];
sideLength = &sideLength[noOfSide];
};
Polygon::~Polygon(){
delete[] sideLength;
};
void Polygon::setsideLength(int* sl){
for(int i=0;i<noOfSide;i++)
sideLength[i] = sl[i];
};
void Polygon::printsideLength(){
for(int i=0;i<noOfSide;i++)
cout << sideLength[i] <<" ";
};
int Polygon::totalsideLength(){
int total = 0;
for(int i=0;i<noOfSide;i++)
total += sideLength[i];
return total;
};
int Polygon::area(){
return 0;
};
#ifndef Triangle_H
#define Triangle_H
#include <iostream>
#include "Polygon.h"
using namespace std;
class Triangle:public Polygon
{
private:
int width;
int height;
public:
Triangle(int w,int h,bool s);
virtual int area();
};
#endif
#include "Triangle.h"
#include <iostream>
using namespace std;
Triangle::Triangle(int w,int h,bool s){
width = w;
height = h;
Polygon(3,s);
};
int Triangle::area(){
int total = 0;
total = (width*height)/2;
return total;
};
#ifndef Rectangle_H
#define Rectangle_H
#include <iostream>
#include "Polygon.h"
using namespace std;
class Rectangle:public Polygon{
private:
int width;
int height;
public:
Rectangle(int w,int h);
void printsideLength();
virtual int area();
};
#endif
#include "Rectangle.h"
#include <iostream>
using namespace std;
Rectangle::Rectangle(int w,int h){
width=w;
height=h;
if(width = height)
Polygon(4,true);
else
Polygon(4,false);
int* size =new int[4];
size[0] = width;
size[1] = height;
size[2] = width;
size[3] = height;
Polygon::setsideLength(size);
};
void Rectangle::printsideLength(){
for(int i=0;i<2;i++)
cout<< width<<" "<<height <<" ";
};
int Rectangle::area(){
int total =0;
total = width*height;
return total;
};
プログラムは、コンパイル・エラーなしなので、唯一の問題は、メモリ についてですが、どこが間違っていますか?仮想関数の部分が間違っているか、他の部分ですか?
は、プログラムサイズはに等しいと動的な整数配列を作成する必要があります辺の数を計算し、配列のアドレスをポインタsideLengthに格納します。 –
この要件を満たすには、新しい配列を作成してsideLengthにアドレスを割り当てる必要がありますか? int *のように=新しいint [noOfSide]; sideLength =&array? –
@TomFong "新しい配列を作成してからsideLengthにアドレスを代入する"というのは、sideLength = new int [noOfSide];ということとまったく同じです。 – molbdnilo