当前位置:编程学习 > C/C++ >>

C++课程设计 高分悬赏

不希望有雷同的答案 谢谢

 

C++课程设计题:

1.按以下要求编写程序:

设有以下一个抽象类:(参考书上例题8.18)            

       class shape

       { public: virtual double area( )=0;           //求图形面积

               virtual void printshapename( )=0;   //输出图形的名称

        };

    由该类派生的类分别求:圆形、三角形、矩形、梯形和正方形的面积,

输出各图形的面积及图形的名称。

以上程序的测试数据自定。

 

答案:我郁闷,一直复制粘贴复制粘贴,好类啊。给个好评吧。

//#include <stdio.h>
//
//int a[3][4]={
// {1,3,5,7},
// {9,11,13,15},
// {17,19,21,23}
//};
//
//int main()
//{
// int i,(*b)[4];
// b=a+1; /* b指向二维数组的第1行, 此时*b[0]是a[1][0] */
// printf("*(b[0]+2)=%d\n",*(b[0]+2));
// for(i=1;i<=4;b=b+2,i++)//这个循环的b=b+2使到b越界了,所指内容根本不是数组a的内容。
//  printf("%d\t",*b[0]);
// printf("\n");
// for(i=0; i<3; i++)
// {
//  b=a+i; /* 修改b的指向,每次跳过二维数组的一行 */
//  printf("%d\t",*(b[i]+1));
// }
// printf ("\n");
//}

//
//#include <iostream>
//using namespace std;
//
//
//class Triangle
//{
//public:
// Triangle();
// Triangle(int height, int width);
// Triangle(const Triangle& tri);
// bool operator>(const Triangle& tri);
//private:
// double _height;
// double _width;
//};
//
//
//Triangle::Triangle()
//:_height(0.0)
//,_width(0.0)
//{
// //Empty
//}
//
//
//Triangle::Triangle(int height, int width)
//:_height(height)
//,_width(width)
//{
// //Empty
//}
//
//
//Triangle::Triangle(const Triangle &tri)
//:_height(tri._height)
//,_width(tri._width)
//{
// //Empty
//}
//
//
//
//bool Triangle::operator >(const Triangle &tri)
//{
// return (_height * _width / 2) > (tri._height * tri._width / 2);
//}
//
//
//
//int main()
//{
// Triangle t1(4,6),t2(3,6);
// cout << "Triangle1-->4,6 || Triangle2-->3,6" << endl;
// if (t1 > t2)
// {
//  cout << "Triangle1>Triangle2" << endl;
// }
// else
// {
//  cout << "Triangle1<=Triangle2" << endl;
// }
// return 0;
//}


//设有以下一个抽象类:(参考书上例题8.18)            
//
//class shape
//
//{ public: virtual double area( )=0;           //求图形面积
//
//virtual void printshapename( )=0;   //输出图形的名称
//
//};
//
//由该类派生的类分别求:圆形、三角形、矩形、梯形和正方形的面积,
//
//输出各图形的面积及图形的名称。
//
//以上程序的测试数据自定。

 

#include <iostream>

using namespace std;


class shape
{
//题目要求
public:
 virtual double area() = 0;
 virtual void printshapename() = 0;
};

//declare circle class
class circle : public shape
{
public:
 circle();
 circle(int r);
 circle(const circle& cir);
public:
 virtual double area();
 virtual void printshapename();
private:
 double _r;
};


//declare triangle class
class triangle : public shape
{
public:
 triangle();
 triangle(int height, int width);
 triangle(const triangle& tri);
public:
 virtual double area();
 virtual void printshapename();
private:
 double _height;
 double _width;
};

 


//declare rectangle
class rectangle : public shape
{
public:
 rectangle();
 rectangle(int length, int width);
 rectangle(const rectangle& tri);
public:
 virtual double area();
 virtual void printshapename();
private:
 double _length;
 double _width;
};


//declare square
class square : public shape
{
public:
 square();
 square(int length);
 square(const square& cir);
public:
 virtual double area();
 virtual void printshapename();
private:
 double _length;
};

 

 

//class circle
circle::circle()
:_r(0.0)
{
 //Empty
}

circle::circle(int r)
:_r(r)
{
 //Empty
}


circle::circle(const circle& cir)
:_r(cir._r)
{
 //Empty
}


double circle::area()
{
 return _r * 3.14159;
}


void circle::printshapename()
{
 cout << "This is a circle" << endl;
}

//class triangle
triangle::triangle()
:_height(0.0)
,_width(0.0)
{
 //Empty
}

triangle::triangle(int height, int width)
:_height(height)
,_width(width)
{
 //Empty
}


triangle::triangle(const triangle& tri)
:_height(tri._height)
,_width(tri._width)
{
 //Empty
}


double triangle::area()
{
 return _height * _width / 2;
}


void triangle::printshapename()
{
 cout << "This is a triangle" << endl;
}

 

 

//class rectangle
rectangle::rectangle()
:_length(0.0)
,_width(0.0)
{
 //Empty
}

rectangle::rectangle(int length, int width)
:_length(length)
,_width(width)
{
 //Empty
}


rectangle::rectangle(const rectangle& tri)
:_length(tri._length)
,_width(tri._width)
{
 //Empty
}


double rectangle::area()
{
 return _length * _width;
}


void rectangle::printshapename()
{
 cout << "This is a rectangle" << endl;
}

 

 

class trapezoidal : public shape
{
public:
 trapezoidal();
 trapezoidal(int upperLen, int lowerLen, int height);
 trapezoidal(const trapezoidal& tri);
public:
 virtual double area();
 virtual void printshapename();
private:
 double _upperLen;
 double _lowerLen;
 double _height;
};

 

trapezoidal::trapezoidal()
:_upperLen(0.0)
,_lowerLen(0.0)
,_height(0.0)
{
 //Empty
}

trapezoidal::trapezoidal(int upperLen,int lowerLen, int height)
:_upperLen(upperLen)
,_lowerLen(lowerLen)
,_height(height)
{
 //Empty
}


trapezoidal::trapezoidal(const trapezoidal& tri)
:_upperLen(tri._upperLen)
,_lowerLen(tri._lowerLen)
,_height(tri._height)
{
 //Empty
}


double trapezoidal::area()
{
 return (_upperLen + _lowerLen) * _height / 2;
}


void trapezoidal::printshapename()
{
 cout << "This is a trapezoidal" << endl;
}


//class square
square::square()
:_length(0.0)
{
 //Empty
}

square::square(int length)
:_length(length)
{
 //Empty
}


square::square(const square& cir)
:_length(cir._length)
{
 //Empty
}


double square::area()
{
 return _length * _length;
}


void square::printshapename()
{
 cout << "This is a square" << endl;
}

 

int main()
{
 shape *pShape[5]={new circle(5), new triangle(4,6), new rectangle(3,7), new trapezoidal(3,7,5), new square(7)};
 for (int i=0;i<5;++i)
 {
  pShape[i]->printshapename();
  cout<<"The Area is:"<<pShape[i]->area()<<endl<<endl;
 }
}

上一个:c++试题,求答案
下一个:c++,directx程序错误

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,