//8-4
#include<iostream>
using namespace std;
#define PI 3.14
class Counter
{
	friend ostream& operator<<(ostream& out, Counter& c);
	
public:
	Counter(){}
	Counter(int n):value(n){}
	Counter& operator+(const Counter &c)
	{
		this->value += c.value;
		return *this;
	}
	
private:
	int value;
};
ostream& operator<<(ostream& out, Counter& c)
{
	cout << c.value << endl;
	return out;
}
int main()
{
	Counter c1(2);
	Counter c2(3);
	Counter c3 = c1 + c2;
	cout << c3 << endl;
} 


//8-5
#include<iostream>
using namespace std;
#define PI 3.14
class Mamal
{
public:
	Mamal(){}
	virtual void speak()
	{
		cout << "动物在叫" << endl;
	}

	
};
class Dog :public Mamal
{
public:
	Dog(){}
	void speak()
	{
		cout << "小狗" << name << "在叫" << endl;
	}
	Dog(string n):name(n){}
private:
     string name;
};
void test01()
{
Dog g("大毛");
	Mamal* p = &g;
	g.speak();
	p->speak();
}
void test02()
{
	Dog p("dijia");
	Mamal* abs = new Dog;
	abs->speak();

}
int main()
{
	test01();
	test02();
} 

//8-6
#include<iostream>
using namespace std;
#define PI 3.14
class Shape
{
public:
	virtual float getArea() = 0;
	virtual float getPerim() = 0;
};
class Rectange :public Shape
{
public:
	Rectange() {}
	Rectange(double a, double b) :x(a), y(b) {}
	void set(double a, double b)
	{
		x = a;
		y = b;
	}
	float getArea()
	{
		return x * y;
	}
	float getPerim()
	{
		return 2 * (x + y);
	}
protected:
	double x, y;
};
class Cricle :public Shape
{
public:
	Cricle() {}
	Cricle(double m) :r(m) {}
	void set(double r)
	{
		this->r = r;
	}
	float getArea()
	{
		return PI * r * r;
	}
	float getPerim()
	{
		return 2 * PI * r;
	}
protected:
	double r;
};
int main()
{
	Rectange r(3, 4);
	Cricle c(5);
	Shape* s1 = &r;
	Shape* s2 = &c;
	cout << s1->getArea() << endl;
	cout << s1->getPerim() << endl;
	cout << s2->getArea()<<endl;
	cout << s2->getPerim()<< endl;
}
//8-7
#include<iostream>
using namespace std;
#define PI 3.14
class Point
{
public:
	Point(int m_x,int m_y):x(m_x),y(m_y){}
	Point& operator++()
	{
		x++;
		y++;
		return *this;
	}
	Point& operator++(int)
	{
		Point temp = *this;
		x++;
		y++;
		return temp;
	}

	int x,y;
};
ostream& operator<<(ostream& out, Point p)
{
	cout << p.x<<" "<<p.y;
	return out;
}
int main()
{
	Point p(1,2);
	cout << ++p << endl;
	cout << p++ << endl;
	cout << p << endl;
} 
//8-8
#include<iostream>
using namespace std;
class BaseClass
{
public:
	virtual void fun1()
	{
		cout << "父类fun1调用" << endl;
	}
	void fun2()
	{
		cout << "父类fun2的调用" << endl;
	}
};
class DerivedCllass :public BaseClass
{
public:
	void fun1()
	{
		cout << "子类fun1调用" << endl;
	}
	void fun2()
	{
		cout << "子类fun2调用" << endl;
	}
};
int main()
{
	DerivedCllass m;
	BaseClass* b = &m;
	DerivedCllass* d = &m;
	b->fun1();
	b->fun2();
	d->fun1();
	d->fun2();
}
//8-9
#include<iostream>
using namespace std;
class BaseClass
{
public:
	virtual void fun1()
	{
		cout << "父类fun1调用" << endl;
	}
	void fun2()
	{
		cout << "父类fun2的调用" << endl;
	}
	 virtual ~BaseClass()
	{
		cout << "父类析构函数的调用" << endl;
	}
};
class DerivedCllass :public BaseClass
{
public:
	void fun1()
	{
		cout << "子类fun1调用" << endl;
	}
	void fun2()
	{
		cout << "子类fun2调用" << endl;
	}
	~DerivedCllass()
	{
		cout << "子类析构函数的调用" << endl;
	}
};
int main()
{
	BaseClass* m = new DerivedCllass;
	delete m;
}

//8-10
#include<iostream>
using namespace std;
class Point
{
	friend Point& operator+(Point& p1, Point& p2);
private:
	double x, y;
public:
	Point(){}
	Point(double a,double b):x(a),y(b){}
	void display()
	{
		cout << "(" << x << "," << y << ")" << endl;
	}
};
Point& operator+(Point &p1,Point &p2)
{
	Point temp;
	temp.x = p1.x + p2.x;
	temp.y = p1.y + p2.y;
	cout << "两坐标之和为:";
	return temp;
}
int main()
{
	Point p1(3, 4);
	Point p2(4, 3);
	p1.display();
	p2.display();
	Point p3 = p1 + p2;
	p3.display();
	return 0;
}
posted on 2023-04-22 21:22  许七安gyg  阅读(16)  评论(0编辑  收藏  举报
$(document).ready(function() { // 禁止右键 $(document).bind("contextmenu", function(){return false;}); // 禁止选择 $(document).bind("selectstart", function(){return false;}); // 禁止Ctrl+C 和Ctrl+A $(document).keydown(function(event) { if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) { //alert("对不起,版权所有,禁止复制"); return false; } }); });