Welcome to LHY's Blog!

C++面向对象课堂案例

1. Myarray and Mystring

Mystring.cpp

#include <bits/stdc++.h>

using namespace std;

class Mystring
{
public:
	Mystring() : s_(NULL), len_(0), siz_(0) {} //无参构造 
	Mystring(const char* s); //有参构造 
	Mystring(const Mystring& p); //拷贝构造 
	~Mystring() { delete []s_; } //析构函数 
	
	void show(); //输出 
	Mystring& operator = (const Mystring& p); //重载 =  
	friend ostream& operator << (ostream& os, const Mystring& p); //重载cout 
	
public:
	char* s_;
	size_t len_; //字符串的长度 
	size_t siz_; //实际分配的大小	
};

Mystring::Mystring(const char* s) : s_(NULL), len_(0), siz_(0)
{
	while (s[len_ ] != 0) len_ ++ ; //计算s串的长度 
	siz_ = 2 * len_; 
	s_ = new char [siz_]; //分配空间 
	for (int i = 0; s[i] != '\0'; i ++ ) //拷贝数据 
		s_[i] = s[i]; 
}

void Mystring::show()
{
	for (int i = 0; i < len_; i ++ ) cout << s_[i];
	puts("");
}

Mystring::Mystring(const Mystring& p) : s_(NULL), len_(p.len_), siz_(p.siz_)
{
	s_ = new char [siz_]; //开辟空间 
	for (int i = 0; i < p.len_; i ++ ) //复制数据 
		s_[i] = p.s_[i];
}

Mystring& Mystring::operator = (const Mystring& p) //写作用域 
{
	//如果空间不够
	if (len_ < p.len_) 
	{
		delete[] s_; //释放原有的空间 
		s_ = new char[p.siz_]; //开辟新空间 
	}
	
	len_ = p.len_;
	siz_ = p.siz_;
	for (int i = 0; i < len_; i ++ )
		s_[i] = p.s_[i];
	
	return *this;
}

ostream& operator << (ostream& os, const Mystring& p) //全局函数不写作用域限定符 
{
	os << p.s_ << endl;
	return os;
}


//int main()
//{
//	Mystring str1("Hello");
//	Mystring str2(str1);
//	str1.show();
//	str2.show();
//	Mystring str3 = str1;
//	str3.show();
//	cout << str1 << endl << str2 << endl << str3 << endl;
//	return 0;
//}

Myarray.cpp

#include <bits/stdc++.h>
#include "Mystring.cpp"

using namespace std;

template<class T>
class Myarray
{
public:
	Myarray() : el_(NULL), len_(0), siz_(0) {} //无参构造 
	Myarray(int n) : el_(NULL), len_(n), siz_(2 * n) //有参构造 
	{
		el_ = new T[siz_];
	}
	~Myarray() {delete [] el_;} //析构 
	T& operator [] (int index) //重载[] 
	{
		return el_[index];
	}
	
private:
	T* el_; //数组 
	int len_; //数组长度 
	int siz_; //实际分配的长度 
};

void test01()
{
	Myarray<Mystring> a1(5);
	Mystring s("Hello");
	a1[0] = s;
	cout << a1[0] << endl;
}

int main()
{
	test01();
	
	return 0;
}

2. Shape

#include <bits/stdc++.h>

using namespace std;

//图形 
class Shape
{
public:
	virtual void show() = 0;
};

//平面图形 
class Plane : virtual public Shape
{
public:
	virtual double area() = 0;
};

//立体图形 
class Solid : virtual public Shape
{
public:
	virtual double volume() = 0;
};

//正方体 
class Square : public Plane
{
protected:
	double length_;
	
public:
	Square(double l) : length_(l) {}
	double length() {return length_;}
	double area() {return length_ * length_;}
	void show() {cout << "Square:" << endl << "面积:" << area() << endl;}
};

//圆形 
class Circle : public Plane
{
protected:
	double r_;
	
public:
	Circle(double r) : r_(r) {}
	double area() {return 3.14 * r_ * r_;}
	void show() {cout << "Circle:" << endl << "面积:" << area() << endl;}
};

//长方形 
class Rectangle : public Square
{
protected:
	double width_;
	
public:
	Rectangle(double l, double w) : Square(l), width_(w) {}
	double area() {return length() * width_;}
	void show() {cout << "Rectangle:" << endl << "面积:" << area() << endl;}
};

//立方体 
class Cube : public Square, public Solid
{
public:
	//继承关系中如果有虚基类,一定要先构造最顶层的虚基类,此题中的Shape有默认构造函数,所以不需要自己写了
	Cube(double l) : Square(l) {}
	double area() {return 6 * length_ * length_;}
	double volume() {return length_ * length_ * length_;}
	void show() {cout << "Cube:" << endl << "面积:"<< area() << ",体积: " << volume() << endl;}	
};

//球体 
class Ball : public Circle, public Solid
{
public:
	Ball(double r) : Circle(r) {}
	double area() {return 4 * 3.14 * r_ * r_;}
	double volume() {return 3 * 3.14 / 4 * r_ * r_ * r_;}
	void show() {cout << "Ball:" << endl << "面积:"<< area() << ",体积:" << volume() << endl;}
};



//传入派生类的地址 
void area_show(Plane* p)
{
	cout << "面积是:" << endl;
	cout << p->area() << endl;
}


void test01()
{
	Square s1(5), s2(2);
	Rectangle r1(2, 3), r2(5, 2);
	Circle c1(1), c2(10);
	area_show(&s1);
	area_show(&s2);
	area_show(&r1);
	area_show(&r2);
	area_show(&c1);
	area_show(&c2);
}

void test02()
{
	Square s1(5), s2(2);
	Rectangle r1(2, 3), r2(5, 2);
	Circle c1(1), c2(10);
	Plane* arr[6] = {&s1, &s2, &r1, &r2, &c1, &c2};
	double sum_area = 0;
	for (int i = 0; i < 6; i ++ )
		sum_area += arr[i]->area();
	cout << "总面积是:" << sum_area << endl;
}

void test03()
{
	Plane* arr[6];
	arr[0] = new Square(5);
	arr[1] = new Square(2);
	arr[2] = new Rectangle(2, 3);
	arr[3] = new Rectangle(5, 2);
	arr[4] = new Circle(1);
	arr[5] = new Circle(10);
	
	double sum_area = 0;
	for (int i = 0; i < 6; i ++ )
		sum_area += arr[i]->area();
	
	cout << "总面积是:" << endl;
	cout << sum_area <<	endl;
	
	delete [] arr; 
	
	for (int i = 0; i < 6; i ++ )
		delete arr[i]; 
}

void test04()
{
	Square s1(5), s2(2);
	Rectangle r1(2, 3), r2(5, 2);
	Circle c1(1), c2(10);
	
	Shape* p;
	Cube cb1(5);
	Ball ba1(1);
	p = &cb1;
	p ->show();
}

int main()
{
	//test01();
	//test02();
	//test03();
	test04();
	return 0;
}
posted @ 2023-05-23 15:49  LiHaoyu  阅读(96)  评论(0)    收藏  举报