1.声明复数的类,complex,使用友元函数add实现复数加法。
#include < iostream >
using namespace std;
class Complex
{
private:
double real, image;
public :
Complex(){}
Complex(double a,double b)
{
real = a;
image = b;
}
void setRI(double a, double b)
{
real = a;
image = b;
}
double getReal()
{ return real;
}
double getImage()
{ return image;
}
void print(){
if(image>0)
cout<<"复数:"<< real <<" + "<< image <<"i"<< endl;
if(image<0)
cout<<"复数:"<< real <<" - "<< image <<"i"<< endl;
}
friend Complex add(Complex ,Complex);//声明友元函数
};
Complex add(Complex c1, Complex c2)//定义友元函数
{
Complex c3;
c3.real = c1.real + c2.real;//访问Complex类中的私有成员
c3.image = c1.image + c2.image;
return c3;
}
void main()
{
Complex c1(19, 0.864), c2, c3;
c2.setRI(90,125.012);
c3 = add(c1, c2);
cout<<"复数一:";c1.print();
cout<<"复数二:";c2.print();
cout<<"相加后:";c3.print();
}
2.编写一个程序,该程序建立一个动态数组,为动态数组的元素赋值,显示动态数组的值并删除动态数组。
#include < iostream >
using namespace std;
void main()
{
int i, n, temp=0;
cout<<"输入数组大小:";
cin>>n;
double *array = new double[n]; //用指针,动态申请数组大小
cout<<"给每个数组元素赋值:"<< endl;
for(i=0; i < n; i++)
{
cout<<"array["<< i <<"] = ";
cin>>temp;
*(array+i) = temp;//给数组元素赋值
}
cout<<"动态数组个元素的值如下:"<< endl;
for(i=0; i < n; i++)
{
cout<<"array["<< i <<"] = "<< array[i] << endl;//打印数组元素
}
delete [] array;//释放内存
}
3.定义一个Dog类,它用静态数据成员Dogs记录Dog的个体数目,静态成员函数GetDogs用来存取Dogs。设计并测试这个类。
#include < iostream >
using namespace std;
class Dog
{
private:
static int dogs;//静态数据成员,记录Dog的个体数目
public :
Dog(){}
void setDogs(int a)
{dogs = a;
}
static int getDogs(){
return dogs;
}
};
int Dog :: dogs = 25;//初始化静态数据成员
void main()
{
cout<<"未定义Dog类对象之前:x = "<< Dog::getDogs() << endl;; //x在产生对象之前即存在,输出25
Dog a, b;
cout<<"a中x:"<< a.getDogs() << endl;
cout<<"b中x:"<< b.getDogs() << endl;
a.setDogs(360);
cout<<"给对象a中的x设置值后:"<< endl;
cout<<"a中x:"<< a.getDogs() << endl;
cout<<"b中x:"<< b.getDogs() << endl;
}
4.设计一个基类,从基类派生圆柱,设计成员函数输出它们的面积和体积;
#include < iostream >
using namespace std;
class Basic//基类
{
protected:
double r;
public :
Basic(){ r = 0; }
Basic(double a):r(a){}
};
class Circular : public Basic//从基类派生圆类
{
protected:
double area;
public :
Circular(double a)
{
r = a;
area = area = 3.1415926 * r * r;
}
double getArea()//返回圆面积
{
return area;
}
};
class Column : public Circular//从圆类派生圆柱类
{
protected:
double h;
double cubage;
public :
Column(double a, double b) : Circular(a){
h = b;
cubage = getArea() * h;
}
double getCubage()//返回圆柱体积函数{
return cubage;
}
};
void main()
{
Circular circular(45);
Column column(12, 10);
cout<<"圆的面积:"<< circular.getArea() << endl;
cout<<"圆柱的体积:"<< column.getCubage() << endl;
}
5.定义一个线段类作为矩形的基类,基类有起点和终点坐标,有输出左边和长度以及线段和x轴的夹角的成员函数。矩线段对象的两个坐标作为自己一条边的位置,它具有另外一条边,能输出矩形的4个顶点坐标。给出类的定义并用程序验证它们的功能。
#include < iostream >
#include < cmath >
using namespace std;
class Point//点类
{
protected:
double x, y;
public :
Point(){}
Point(double a, double b)
{
x = a; y = b;
}
double getX()
{return x;}
double getY()
{return y;}
};
class Line
{
protected:
Point p1, p2;//Point对象做成员
double length, angle;
public:
Line(double a, double b, double c, double d):p1(a, b), p2(c, d)//用两对坐标初始化线段
{
init();
}
Line(Point a, Point b)//用两个点的对象初始化线段
{
p1 = a; p2 = b;
init();
}
void init()//计算线段长度,以及和x轴的夹角的度数
{
double x1 = p1.getX(), y1 = p1.getY();
double x2 = p2.getX(), y2 = p2.getY();
length = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
angle = atan( (y2-y1) / (x2-x1) );
angle = angle *180/3.141592653;
}
void printXY()
{
cout<<"("<< p1.getX() <<","<< p1.getY() <<"); ("<< p2.getX() <<","<< p2.getY() <<")"<< endl;
}
void printLength()
{
cout<<"线段长度:"<< length << endl;
}
void printAngle()
{
cout<<"与x轴的夹角:"<< angle <<"°"<< endl;
}
};
class Rectangle : public Line
{
protected:
Line *line;
public:
Rectangle(double a, double b, double c, double d, double e, double f, double g, double h):Line(a,b,c,d)
{
line = new Line(e,f,g,h);
}
Rectangle(Point a, Point b, Point c, Point d) : Line(a, b)//4个点对象,初始化
{
line = new Line(c, d);
}
void printPoint()
{ cout<<"矩形4个顶点:\n";
printXY();
line->printXY();
}
};
void main()
{
Point p1(0, 0), p2(4, 3), p3(12, 89), p4(10, -50);
Line l1(0,0,4,3);
l1.printXY();
l1.printLength();
l1.printAngle();
Line l2(p1, p2);
l2.printXY();
l2.printLength();
l2.printAngle();
Rectangle r1(12,45,89,10,10,23,56,1);
r1.printPoint();
Rectangle r2(p1, p2, p3, p4);
r2.printPoint();
}
6.基类是使用极坐标的点类,从它派生一个圆类,圆类用点类的左边作圆心,圆周通过极坐标原点,圆类有输出圆心直、圆半径和面积的成员函数。完成类的设计并验证之。
#include < iostream >
#include < cmath >
using namespace std;
class Point//点类
{
protected: int x, y;
public : Point(){}
};
class Circular : public Point//圆类,继承点类
{
protected:
double r, area;
public :
Circular(int a, int b)
{
x = a;
y = b;
r = sqrt( x * x + y * y );
area = 3.1415926 * r * r;
}
void printPoint()
{
cout<<"圆形直角坐标:("<< x <<", "<< y <<")"<< endl;
}
void printRadius()
{ cout<<"圆的半径:"<< r << endl;
}
void printArea(){
cout<<"圆的面积:"<< area << endl;
}
};
void main()
{
Circular c(10,25);
c.printPoint();
c.printRadius();
c.printArea();
}
7.设计一个线段基类,当创建五参数对象时,才要求用户输入长度。同样,其派生的直角三角形类也是在产生对象时要求输入两个直角边的长度。直角三角形在派生矩形类,矩形类的参数也由键盘输入。设计这些类并测试他们的功能。
#include < iostream >
#include < cmath >
using namespace std;
class Line//线段基类
{
protected:
double sizeA;
public :
Line()
{
cout<<"输入线段的长度:"<< endl;
cin>>sizeA;
}
Line(double a)
{
sizeA = a;
}
double getLength()
{
return sizeA;
}
};
class Triangle : public Line//三角形类
{
protected: double sizeB, sizeC;
public : Triangle()
{
cout<<"输入线段长度:"<< endl;
cin>>sizeB;
sizeC = sqrt( sizeB * sizeB + sizeA * sizeA );
}
void printSize()
{ cout<<"直角三角形,三条边分别为:";
cout<<"A: "<< sizeA << ", b: "<< sizeB << ", C: "<< sizeC << endl;
}
};
class Rectangle : public Triangle//矩形类
{
protected:
double sizeD;
public :
Rectangle()
{
sizeC = sizeA;
sizeD = sizeB;
}
void printSize()
{
cout<<"矩形,四条边分别为:";
cout<<"A: "<< sizeA << ", b: "<< sizeB << ", C: "<< sizeC << ", D: "<< sizeD << endl;
}
};
void main()
{
/* Line *l = new Line();
cout<<"线段长度为:"<< l->getLength() << endl;
*/
/*Triangle *t = new Triangle();
t->printSize();*/
Rectangle *r = new Rectangle();
r->printSize();
}