#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");
}
*/
C++面向对象程序设计第四章答案
最新推荐文章于 2021-06-24 23:16:04 发布