C++复习

来源:互联网 发布:Ios11关闭4g网络 编辑:程序博客网 时间:2024/06/09 21:16

点击打开链接  习题答案

1. 定义一个复数类Complex,复数的实部Real与虚部Image定义为私有数据成员。用复数类定义复数对象c1、c2、c3c4,用带参数构造函数将c1初始化为c1=20+40i ,用默认构造函数将c2初始化为c2=0+0i,用拷贝构造函数将c3初始化为c3=20+40i。并重载+号运算符,实现c4=c1+c2,用公有成员函数Dispaly()显示c1、c2、c3 的内容。

#include<iostream>

using namespace std;

class Complex

{

private:

int real;

int image;

public:

Complex(){}

Complex(int x,int y)

{

real=x;

image=y;

}

Complex(Complex&c)

{

real=c.real;

image=c.image;

}

~Complex(){};

void display()

 {

  cout<<real<<'+'<<image<<'i'<<endl;

 }

};

void main()

{

 Complex c1(20,40);

Complex c2(0,0);

Complex c3(c1);

c1.display();

c2.display();

c3.display();

 system("pause");

}

 

 

2.编程实现一个TIME类,要求:

(1) 有带默认参数的构造函数和析构函数;

(2) 能按例如“2358:55”的形式输出时间;

(3) 能设置时间,在设置前能对设置的时间数据做合法判断。

   (4)能够实现加一秒的功能。

#include<iostream>

#include<cstdlib>

#include<Windows.h>

using namespace std;

class Time

{

private:

int hour;

int minute;

int second;

public:

Time() { cout << "众筹开始" << endl; }

~Time() { cout << "众筹结束" << endl; }

void Display(void) {

cout << hour << ":" << minute<< ":" << second<< endl;

}

void setTime(int x,int y,int z) {

if (x>23 &&x<0 && y>59 && y<0 && z>59 && z<0)

{

cout << "error" << endl;

exit(1);

}

hour = x;

minute = y;

second = z;

}

void jia1miao(void) {

if (second<59)

second += 1;

else if (minute<59)

{

minute += 1;

second = 0;

}

else if (hour<23)

{

hour += 1;

minute = 0;

second = 0;

}

else

{

hour = 0;

     minute = 0;

     second = 0;

}

this->Display();

}

};

int main(void)

{

Time T;

T.setTime(15, 33, 55);

while (1)

{

T.jia1miao();

Sleep(1000);

system("cls");

}

system("pause");

return 0;

}

 

1、定义抽象基类Shape,由它派生出三个派生类:Circle(圆形)、Rectangle( 长方形)和Triangle(三角形),定义虚函数分别计算各种图形的面积

 

#include <iostream>

using namespace std;

class Shape

{ public:

    virtual double area() const =0;

};

class Circle:public Shape

{ public:

Circle(double r):radius(r){}

    virtual double area() const {return 3.14159*radius*radius;}; 

  protected:

    double radius;  

};

class Rectangle:public Shape

{ public:

 Rectangle(double w,double h):width(w),height(h){}

virtual double area() const {return width*height;}

 protected:

  double width,height; 

};

class Triangle:public Shape

{ public:

    Triangle(double w,double h):width(w),height(h){}

    virtual double area() const {return 0.5*width*height;}

  protected:

    double width,height; 

};

int main()

{

Circle circle(12.6); 

Rectangle rectangle(4.5,8.4);  

Triangle triangle(4.5,8.4); 

  Shape *pt[3]={&circle,,&rectangle,&triangle}; 

  double areas=0.0;  

  for(int i=0;i<3;i++)

    { areas=areas+pt[i]->area();}

  cout<<"totol of all areas="<<areas<<endl; 

return 0;

}

 

 

2、 实现一个计算器控制台程序,主程序提示用户输入两个运算数和运算符,由运算类完成相应的计算并输出结果

# include <iostream>

using namespace std;

class Operation

{

     private:

           double m_numberA;

           double m_numberB;         

     public:

          virtual double GetResult()

          {

              double result = 0; 

              return result;

          }

         double getA(){ return m_numberA; }

         double getB(){ return m_numberB; }

         void setA(double numberA){ m_numberA=numberA; }

         void setB(double numberB){ m_numberB=numberB; }

} ;

class OperationAdd : public  Operation

{

     public :

     double GetResult()

     {

           double result = 0; 

           result = getA()+ getB();

           return result;

     }

} ;

class OperationSub : public  Operation

{

     public :

     double GetResult()

     {

           double result = 0; 

           result = getA()- getB();

           return result;

     }

} ;

class OperationFactory

{   public:

          static Operation*  createOperate(char operate){

               switch(operate)

               {

               case ’+’:

                   { return new OperationAdd();  }

               case ’-’:

                   { return new  OperationSub(); }

               case ’*’:

                   { return new OperationMul(); }

               case ’/’:

                  {  return new OperationDiv();  }

              }

         }

} ;

void main()

{

   Operation *oper;

   OperationFactory  operfactory;

   oper=operfactory.createOperate(’+’);

   oper->setA(1);

   oper->setB(2);

   double result = oper->GetResult();

   cout<<result;

}