2016级面向对象程序设计(C++)

2017-9-14 16:39
请先登录。
嘿嘿嘿,日常忘记作业

//任务12-01
#include<iostream>
#include<string>
using namespace std;
class Shape
{
public:
	Shape(double a = 0, double p = 0){
		m_dArea=a;
		m_dPerimeter=p;
	}
	virtual void displayArea() = 0;
	virtual void displayPerimeter() = 0;
private:
	double m_dArea;
	double m_dPerimeter;
};
class Circle:public Shape
{
public:
	Circle(double r = 0)
	{
		m_dR=r;
	}
	virtual void displayArea()
	{
		cout<<"The Circle's Area is"<<3.14*m_dR*m_dR<<endl;
	}
	virtual void displayPerimeter()
	{
		cout<<"The Circle's Perimeter is "<<2*3.14*m_dR<<endl;
	}
private:
	double m_dR;
};
class Rectangle:public Shape
{
public:
	Rectangle(double l = 0, double w = 0)
	{
		m_dLength=l;
		m_dWidth=w;
	}
	virtual void displayArea()
	{
		cout<<"The Rectangle's Area is"<<m_dLength*m_dWidth<<endl;
	}
	virtual void displayPerimeter()
	{
		cout<<"The Rectangle's Perimeter is"<<2*m_dWidth+2*m_dLength<<endl;
	}
private:
	double m_dLength;
	double m_dWidth;
};
void displayAreaAndPerimeter(Shape &r)
{
	r.displayArea();
	r.displayPerimeter();
}
int main(void){
	Circle c1(9);
	Rectangle r1(2,4);
	displayAreaAndPerimeter(c1);
	displayAreaAndPerimeter(r1);
	return 0;
}
//任务12-02
#include<iostream>
#include<string>
using namespace std;
template <typename T>
inline void mySwap(T & a,T & b)
{
	T t;
	t=a;
	a=b;
	b=t;
}
template <typename T>
void bubbleSort(T* const arr,int size)
{
	int i,j=1,m=1;
	for(;m&&j<size;j++)
	{
		m=0;
		for(i=0;i<size-j;i++)
		{
			if(arr[i]>arr[i+1])
			{
				mySwap<T>(arr[i],arr[i+1]);
				m=1;
			}
		}
	}
}
template <typename T >
void printResult(T* const arr, int size)
{
	int i=1;
	for(i=0;i<size;i++)
	{
		cout<<arr[i];
	}
	cout<<endl;
}
int main(void){
         int arri[10] = { 4, 2, 5, 7, 9, 1, 3, 8, 0, 6 };
         bubbleSort<int>(arri, 10);
         printResult<int>(arri,10);
 
         double arrd[10] = {4.4,2.2,5.5,7.7,9.9,1.1,3.3,8.8,0,6.6};
         bubbleSort<double>(arrd, 10);
         printResult<double>(arrd,10);
 
         char arrc[10] = "aefdighcb";
         bubbleSort<char>(arrc, strlen(arrc));
         printResult<char>(arrc, strlen(arrc));   
         return 0;
}