DOC

By Wayne Wood,2014-12-14 01:31
8 views 0

编写一个程序:声明一个类Cat,拥有静态数据成员HowManyCats,用以记录Cat的个体数目,拥有静态成员函数GetHowMany(),用以存取HowManyCats.设计程序并测试这个类.

最简单的源程序如下？

#include "iostream.h"

class cat

{

static int HowManyCats; public:

cat(){};

cat(int x){HowManyCats=x;} static void GetHowMany(int x) {HowManyCats=x;}

static int GetHowMany() {return HowManyCats;}

};

int cat::HowManyCats=0; void main()

{

cat cat1;

cout<cat1.GetHowMany(3);

cout<cat1.GetHowMany(3);

cout<}

定义一个名为complex的复数类？其属性数据为复数的实部和虚部？ //Complex.h

class Complex

{

private:

float Real;

float Imag;

public:

Complex();

Complex(float Rl,float Im);

Complex operator +(Complex &c);

Complex operator -(Complex &c);

Complex operator *(Complex &c);

Complex operator /(Complex &c);

void GetValue();

};

//Complex.cpp

#include

#include "Complex.h"

void main()

{

Complex c1(1,1);

Complex c2(1,-1);

Complex c3;

c3 = c1+c2;

c3.GetValue();

c3 = c1-c2;

c3.GetValue();

c3 = c1*c2;

c3.GetValue();

c3 = c1/c2;

c3.GetValue();

return;

}

////////////////////////////////////////////////////////

Complex::Complex()

{

Real = 0;

Imag = 0;

}

/////////////////////////////////////////////////////////

Complex::Complex(float Rl,float Im) {

Real = Rl;

Imag = Im;

}

////////////////////////////////////////////////////////

inline Complex Complex::operator +(Complex &c)

{

Complex x;

x.Real = Real+c.Real;

x.Imag = Imag+c.Imag;

return x;

}

/////////////////////////////////////////////////////// inline Complex Complex::operator -(Complex &c) {

Complex x;

x.Real = Real-c.Real;

x.Imag = Imag-c.Imag;

return x;

}

/////////////////////////////////////////////////////// inline Complex Complex::operator *(Complex &c) {

Complex x;

x.Real = Real*c.Real-Imag*c.Imag;

x.Imag = Imag*c.Real+c.Imag*Real;

return x;

}

/////////////////////////////////////////////////////// inline Complex Complex::operator /(Complex &c) {

/*(a+bi)/(c+di)

=(a+bi)*(c-di)/(c+di)*(c-di)

*/

Complex x;

x.Real = (Real*c.Real-Imag*(-c.Imag))/(c.Real*c.Real+c.Imag*c.Imag);

x.Imag = (Real*(-c.Imag)+Imag*c.Real)/(c.Real*c.Real+c.Imag*c.Imag);

return x;

}

/////////////////////////////////////////////////////// void Complex::GetValue()

{

cout<

}

定义一个名为rectangle的矩形类？其属性数据为矩形坐上角和右下角的点的坐标？能计算

矩形的面积。

struct Point

{

int x;

int y;

};

class Rectangle

{

public:

Rectangle(Point l, Point r){ cl = w; cr = h; }

void LeftTop(int x, int y){ cl.x = x; cl.y = y; }

void LeftTop(Point p){ cl = p;} void RightBottom(int x, int y){ cr.x = x; cr.y = y; }

void RightBottom(Point p){ cr = p;} int Area()

{

return (cr.x - cl.x) * (cr.y - cl.y);

}

private:

Point cl;

Point cr;

};

实现一单链表的逆置,并输出逆置前后的结果.

//C LANGUAGE

#include

struct node{

int data;

struct node *next;

};

void push(int data){

newnode->data=data;

newnode->next=stack;

stack=newnode;

}

int pop(){

int temp;

if(stack!=NULL){

top=stack;

stack=stack->next;

temp=top->data;

free(top);

return temp;

}

else

return -1;

}

main(){

int i,random,out,value;

int a[20];

i=0;

printf("输入需要逆序的序列,0结束输入\n");

scanf("%d",&value);

push(value);

while(value){

i++;

scanf("%d",&value);

push(value);

}

for(out=0;out

random=pop();

printf("%d",random);

}

}

试定义一个字符串类string,使其至少具有内容(contents)和长度(length)两个数据成员？并肯有显示安符吕、求字符串长度、给原字符串后添加一个字符串等功能。 #include

#include

using namespace std;

class STRING

{

public:

STRING(string str) : m_strContents(str), m_nLength(m_strContents.size()){};

void Display() const {cout << m_strContents << endl;}

int Size() const {return m_nLength;}

const STRING AddSTRING(STRING s) {return STRING(m_strContents += s.m_strContents);}

private:

string m_strContents;

int m_nLength;

};

int main()

{

STRING str("hello");

str.Display();

cout << str.Size() << endl;