C++面向对象程序设计第四章答案

#include<iostream>
#include<string>
using namespace std;

/*
class Point {
public:
	int getX()const { return X; }
	int getY()const { return Y; }
private:
	int X, Y;
};

int main()
{
	Point P;
	cout << P.getX() << ',' << P.getY() << endl;
	system("pause");
}

*/
/*
class Text {
public:
	Text();
	Text(int n);
private:
	int num;
};


Text::Text()
{
	cout << "默认构造函数!" << endl;
}

Text::Text(int n):num(n)
{
	cout << "自定义构造函数!" << endl;
}

int main()
{
	Text t1[2];
	Text t2(2);
	system("pause");
}

*/


/*
class Xx {
public:
	Xx(int n): num(n) {};
	~Xx() { cout << "调用了析构函数!" << endl; }
private:
	int num;
};

int main()
{
	Xx x(10);
	system("pause!");
}
*/

/*
class Book {
public:
	Book(int n) :num(n) { cout << "新对象借走了" << n << "本书" << endl; sumnum -= n; }
	static int sumnum;
private:
	int num;
};

int Book::sumnum=500;

int main()
{
	Book b1(10);
	Book b2(20);
	cout << "存书数量:" << Book::sumnum << endl;
	system("pause");
}
*/

/*
class Box {
private:
	int a, volume, area;
public:
	Box(int l) :a(l) {}
	void seta(int l);
	void getvolume();
	void getarea();
	void disp();
};

void Box::seta(int l)
{
	a = l;
}

void Box::getvolume()
{
	volume = pow(a, 3);
}

void Box::getarea()
{
	area = 6 * a*a;
}

void Box::disp()
{
	cout << "a:" << a<<",volume:" << volume << ",area:" << area << endl;
}

int main()
{
	Box box(10);
	box.getarea();
	box.getvolume();
	box.disp();
	system("pause");

}
*/

/*
class Distance;
class Point {
private:
	int X, Y, Z,distance;
public:
	Point(int x, int y, int z) :X(x), Y(y), Z(z) {};
	friend class Distance;
	void operator-(Point x);
	void disp() { cout << "distance:"<<distance << endl; }
};

class Distance {
private:
	double distance;
public:
	void GetDistance(Point X, Point Y);
	void disp();
};

void Point::operator-(Point x){
	distance = sqrt(pow((X - x.X), 2) + pow((Y - x.Y), 2) + pow((Z - x.Z), 2));
}

void Distance::GetDistance(Point X, Point Y)
{
	distance = sqrt(pow((X.X - Y.X), 2) + pow((X.Y - Y.Y), 2) + pow((X.Z - Y.Z), 2));
}



void Distance::disp()
{
	cout << "distance:" << distance << endl;
}

int main()
{
	Point X[2] = { Point(1,1,1),Point(2,2,2) };
	Distance L;
	L.GetDistance(X[0], X[1]);
	L.disp();
	X[0] - X[1];
	X[0].disp();
	system("pause");
}
*/

/*
class Dog {
private:
	static int dog;
	int id;
	string name;
public:
	Dog(int new_id, string new_name) :id(new_id), name(new_name) { dog++; };
	static void countofdogs();
};

int Dog::dog = 0;
void Dog::countofdogs()
{
	cout << "countofdogs:" << dog << endl;
}

int main()
{
	Dog::countofdogs();
	Dog dog1(1, "jack");

	Dog dog2(2, "mark");
	dog1.countofdogs();;
	system("pause");
}

*/
/*
#include<iomanip>

class Complex {
private:
	double rel, img;
public:
	Complex() { cout << "默认构造函数" << endl; };
	Complex(double x, double y) :rel(x), img(y) { cout << "构造函数" << endl; };
	Complex(const Complex&C) { rel = C.rel;img = C.img; cout << "赋值构造函数" << endl; };
	void disp();
	friend Complex add(const Complex&x,const Complex&y);
	friend Complex disp(Complex c1, Complex c2);
};

void Complex::disp()
{
	cout << rel;
	if (img != 0)cout << setiosflags(ios::showpos)<<img << "i" << endl;
}

Complex disp(Complex c1, Complex c2)
{
	cout << c1.rel << setiosflags(ios::showpos) << c1.img << 'i' << endl<<resetiosflags(ios::showpos);
	cout << c2.rel << setiosflags(ios::showpos) << c2.img << 'i' << endl;
	return c1;
}



Complex add(const Complex&x,const Complex&y)
{
	Complex z;
	z.rel = x.rel + y.rel;
	z.img = x.img + y.img;
	return z;
}



int main()
{
	Complex x1(1.1, -1.1);
	Complex x2(1.2, 1.2);
	Complex x3 = add(x1, x2);
	x3.disp();
	system("pause");
}

*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值