2010年复试题目

1. 输入n个十进制数转换成二进制写到文件,n是随机得到。

  1. 第一种方法:C风格字符串转化
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>

using namespace std;

char* decToBin(int);

int main()
{
    srand(time(0));
    int num = rand()%10 + 1;
    int dec;
    ofstream outFile("output.txt",ios::app|ios::out);
    if(!outFile)
    {
        cout << "File cann't be opened!" << endl;
        exit(1);
    }
    cout << "输入"  << num << "个十进制数" << endl;
    while(num !=0 )
    {
        cin >> dec;
        outFile << decToBin(dec);
        outFile << '\n';
        num--;

    }
    outFile.close();
    return 0;
}

char* decToBin(int dec)
{
    int i = 0, j = 0;
    char ch[32];
    while(dec!=0)
    {
        ch[i++] = dec%2 + '0'; //不是字符串相加,是加法运算然后根据ASCII显示。
        dec /= 2;
    }
    i--;
    char *p = new char[j];
    for(;i>=0;i--,j++)
        p[j] = ch[i];
    p[j] = '\0';
    return p;
}

扩展:输入二进制转化为十进制并写进文件中

#include <iostream>
//二进制转化为十进制存到文件中
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <math.h>
using namespace std;

int binToDec(char *,int);

int main()
{
    const int size = 32;
    srand(time(0));
    int num = rand()%10+1;
    ofstream outFile("output.txt",ios::out);
    if(!outFile)
    {
        cout << "File cann't be opened!" << endl;
        exit(1);
    }
    cout << ".输入" << num << ".个二进制数" << endl;
    char bin[size];
    int dec = 0;
    while(num!=0)
    {
        cin >> bin;
        int i = 0;
        while(bin[i] != '\0')
            i++;     //统计字符串长度

        dec = binToDec(bin,i);
        outFile << dec << '\n';
        num--;
    }
    return 0;
}

int binToDec(char *bin,int length)
{
    int dec = 0, i = 0;
    for(;i<length;i++)
    {
        dec += (bin[i] - '0')*pow(2,i);
    }
    return dec;
}
  1. 第二种方法:我比较欣赏这种方法。二进制运算,输出高位
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;

void writeBits(unsigned , ofstream &);

int main()
{
    srand(time(0));
    int num = rand()%10 + 1;
    unsigned dec;
    ofstream outFile("output.txt",ios::app|ios::out);
    cout << "输入"  << num << "个十进制数" << endl;
    while(num !=0 )
    {
        cin >> dec;
        writeBits(dec,outFile);
        num--;

    }
    outFile.close();
    return 0;
}
void writeBits(unsigned dec, ofstream &outFile)
{
    const int SHIFT = 8 * sizeof(unsigned) - 1;
    const unsigned MASK = 1 << SHIFT;
    cout << setw(10) << dec << " = ";
    for(unsigned c = 0; c <= SHIFT; c++)
    {
        cout << (dec & MASK ? '1' : '0');
        outFile << (dec & MASK ? '1' : '0');
        dec <<= 1;
        if((c+1) %8 == 0)
        {
            outFile << ' ';
            cout << ' ';
        }

    }
    outFile << '\n';
}
  1. 第三种方法:更加容易理解
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>

using namespace std;

void decToBin(int,ofstream &);

int main()
{
    srand(time(0));
    int num = rand()%10 + 1;
    int dec;
    ofstream outFile("output.txt",ios::out);
    if(!outFile)
    {
        cout << "File cann't be opened!" << endl;
        exit(1);
    }
    cout << ".输入"  << num << ".个十进制数" << endl;
    while(num !=0 )
    {
        cin >> dec;
        decToBin(dec, outFile);
        num--;

    }
    outFile.close();
    return 0;
}

void decToBin(int dec, ofstream &outFile)
{
    char ch[32];
    int count = 0;
    while(dec){      //反向求二进制
        ch[count] = dec%2 + '0';
        //outFile << ch[count];  //写入正好相反
        count ++;
        dec/=2;
    }
    ch[count] = '\0';
    int left = 0;
    int right = count - 1;
    char c;
    while(left < right){
        c = ch[left];
        ch[left] = ch[right];
        ch[right] = c;
        left++;
        right--;
    }
    outFile << ch << '\n';
}
  1. 第四种方法:利用栈的特点
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <stack>

using namespace std;

void decToBin(int,ofstream &);

int main()
{
    srand(time(0));
    int num = rand()%10 + 1;
    int dec;
    ofstream outFile("output.txt",ios::app|ios::out);
    if(!outFile)
    {
        cout << "File cann't be opened!" << endl;
        exit(1);
    }
    cout << "输入"  << num << "个十进制数" << endl;
    while(num !=0 )
    {
        cin >> dec;
        decToBin(dec,outFile);
        num--;

    }
    outFile.close();
    return 0;
}

void decToBin(int dec, ofstream &outFile)
{
    stack<int> s;
    while(dec!=0)
    {
        s.push(dec%2);
        dec/=2;
    }
    while(!s.empty())
    {
        outFile << s.top();
        s.pop();
    }
    outFile << '\n';
}

2. 写两个模板函数:插入排序法的迭代实现与递归实现 。

非递归:

#include <iostream>

using namespace std;

template<typename T>   //typename和class都可以 template<class T>
void insert_Sort(T a[], int len)
{
    for(int i = 0; i < len;  i++)
        for(int j = 0; j < i; j++)
        if(a[i] < a[j])
        swap(a[i],a[j]);
}

template<typename T>
void swap(T &a, T &b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a[10] = {8,1,4,5,6,2,7,3,10,9};
    for(int i = 0; i < 10; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    insert_Sort(a,10);
    for(int i = 0; i < 10; i++)
    {
        cout << a[i] << " ";
    }
    return 0;
}

递归:

#include <iostream>

using namespace std;

template<typename T>   //typename和class都可以 template<class T>
void insert_Sort(T a[], int len)
{
    T temp;
    if(len == 0)
        return;
    insert_Sort(a,len-1);
    temp = a[len];
    int i;
    for( i = 0; i < len; i++)
        if(temp < a[i])
            swap(temp,a[i]);
    a[i] = temp;
}

template<typename T>
void swap(T &a, T &b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a[10] = {8,1,4,5,6,2,7,3,10,9};
    for(int i = 0; i < 10; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    insert_Sort(a,10);
    for(int i = 0; i < 10; i++)
        cout << a[i] << " ";
    return 0;
}

3. 处理字符串(025)87234865-987,用 strtok 处理,以“区号 电话 分机号”的格式输出。

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;

int main()
{
    char str[19], *areaCode, *phoneNumber, *Ext;
    cout << "Enter 19 number like  (123)1233456789-123" << endl;
    cin >> str;
    areaCode =  strtok(str,"()");
    phoneNumber = strtok(NULL,"-");
    Ext = strtok(NULL,"");
    cout << setw(10) << left << "areaCode" << setw(14) << left << "phoneNumber" << setw(8) << "Ext" << endl;
    cout << setw(10) << left << areaCode << setw(14) << left << phoneNumber << setw(8) << Ext << endl;
    return 0;
}

4. 设计一个多项式 Polynomial( 包括构造函数、复制构造函数、析构函数、赋值函数、实现 两个多项式相加)

官方:
Polynomial.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

class Polynomial
{
public:
   static const int maxTerms = 100; // maximum number of terms

   Polynomial();
   Polynomial operator+( const Polynomial & ) const; // addition
   Polynomial operator-( const Polynomial & ) const; // subtraction
   Polynomial operator*( const Polynomial & ) const; // multiplication
   Polynomial &operator=( const Polynomial & ); // assignment
   Polynomial &operator+=( const Polynomial & );
   Polynomial &operator-=( const Polynomial & );
   Polynomial &operator*=( const Polynomial & );

   void enterTerms();            //输入多项式
   void printPolynomial() const;   //输出多项式

   int getNumberOfTerms() const; // user can only retrieve value

   int getTermExponent( int ) const;

   int getTermCoefficient( int ) const;
   void setCoefficient( int, int ); // set coefficient of a specific term

   ~Polynomial(); // destructor

private:
   int numberOfTerms;   //有几个多项式
   int exponents[ maxTerms ]; // exponent array    指数
   int coefficients[ maxTerms ]; // coefficients array    系数
   static void polynomialCombine( Polynomial & ); // combine common terms 结合相同项
}; // end class Polynomial

#endif

Polynomial.cpp

#include <iostream>
#include <iomanip>
#include "Polynomial.h"
using namespace std;

Polynomial::Polynomial()  //初始化系数
{
   for ( int t = 0; t < maxTerms; t++ )
   {
      coefficients[ t ] = 0;
      exponents[ t ] = 0;
   } // end for

   numberOfTerms = 0;
} // end Polynomial constructor

void Polynomial::printPolynomial() const
{
   int start;
   bool zero = false;

   if ( coefficients[ 0 ] ) // output constants
   {
      cout << coefficients[ 0 ];
      start = 1;
      zero = true; // at least one term exists
   }
   else
   {
      if ( coefficients[ 1 ] )
      {
         cout << coefficients[ 1 ] << 'x';  // constant does not exist
                                            // so output first term
                                            // without a sign
         if ( ( exponents[ 1 ] != 0 ) && ( exponents[ 1 ] != 1 ) )
            cout << '^' << exponents[ 1 ];

         zero = true;  // at least one term exists
      } // end inner if

      start = 2;
   } // end else

   // output remaining polynomial terms
   for ( int x = start; x < maxTerms; x++ )
   {
      if ( coefficients[ x ] != 0 )
      {
         cout << showpos << coefficients[ x ] << noshowpos << 'x';

         if ( ( exponents[ x ] != 0 ) && ( exponents[ x ] != 1 ) )
            cout << '^' << exponents[ x ];

         zero = true; // at least one term exists
      } // end if
   } // end for

   if ( !zero ) // no terms exist in the polynomial
      cout << '0';

   cout << endl;
} // end function printPolynomial

Polynomial &Polynomial::operator=( const Polynomial &r )
{
   exponents[ 0 ] = r.exponents[ 0 ];
   coefficients[ 0 ] = r.coefficients[ 0 ];

   for ( int s = 1; s < maxTerms; s++ )
   {
       if ( r.exponents[ s ] != 0 )
       {
          exponents[ s ] = r.exponents[ s ];
          coefficients[ s ] = r.coefficients[ s ];
       }
       else
       {
          if ( exponents[ s ] == 0 )
             break;

          exponents[ s ] = 0;
          coefficients[ s ] = 0;
      }  // end else
   } // end for

   return *this;
} // end function operator=

Polynomial Polynomial::operator+( const Polynomial &r ) const
{
   Polynomial temp;
   bool exponentExists;
   int s;

   // process element with a zero exponent
   temp.coefficients[ 0 ] = coefficients[ 0 ] + r.coefficients[ 0 ];

   // copy right arrays into temp object; s will be used to keep
   // track of first open coefficient element
   for ( s = 1; ( s < maxTerms ) && ( r.exponents[ s ] != 0 ); s++ )
   {
      temp.coefficients[ s ] = r.coefficients[ s ];
      temp.exponents[ s ] = r.exponents[ s ];
   } // end for

   for ( int x = 1; x < maxTerms; x++ )
   {
      exponentExists = false; // assume exponent will not be found

      for ( int t = 1; ( t < maxTerms ) && ( !exponentExists ); t++ )
         if ( exponents[ x ] == temp.exponents[ t ] )
         {
            temp.coefficients[ t ] += coefficients[ x ];
            exponentExists = true;  // exponent found
         } // end if

      // exponent was not found, insert into temp
      if ( !exponentExists )
      {
         temp.exponents[ s ] = exponents[ x ];
         temp.coefficients[ s ] += coefficients[ x ];
         s++;
      } // end if
   } // end for

   return temp;
} // end function operator+

Polynomial &Polynomial::operator+=( const Polynomial &r )
{
   *this = *this + r;
   return *this;
} // end function operator+=

Polynomial Polynomial::operator-( const Polynomial &r ) const
{
   Polynomial temp;
   bool exponentExists;
   int s;

   // process element with a zero exponent
   temp.coefficients[ 0 ] = coefficients[ 0 ] - r.coefficients[ 0 ];

   // copy left arrays into temp object; s will be used to keep
   // track of first open coefficient element
   for ( s = 1; ( s < maxTerms ) && ( exponents[ s ] != 0 ); s++ )
   {
      temp.coefficients[ s ] = coefficients[ s ];
      temp.exponents[ s ] = exponents[ s ];
   } // end for

   for ( int x = 1; x < maxTerms; x++ )
   {
      exponentExists = false; // assume exponent will not be found

      for ( int t = 1; ( t < maxTerms ) && ( !exponentExists ); t++ )

         if ( r.exponents[ x ] == temp.exponents[ t ] )
         {
            temp.coefficients[ t ] -= r.coefficients[ x ];
            exponentExists = true;  // exponent found
         } // end if

      // exponent was not found, insert into temp
      if ( !exponentExists )
      {
         temp.exponents[ s ] = r.exponents[ x ];
         temp.coefficients[ s ] -= r.coefficients[ x ];
         s++;
      } // end if
   } // end for

   return temp;
} // end function operator-

Polynomial &Polynomial::operator-=( const Polynomial &r )
{
   *this = *this - r;
   return *this;
} // end function operator-=

Polynomial Polynomial::operator*( const Polynomial &r ) const
{
   Polynomial temp;
   int s = 1; // subscript location for temp coefficient and exponent

   for ( int x = 0; ( x < maxTerms ) &&
      ( x == 0  || coefficients[ x ] != 0 ); x++ )

      for ( int y = 0; ( y < maxTerms ) &&
         ( y == 0 || r.coefficients[ y ] != 0 ); y++ )

         if ( coefficients[ x ] * r.coefficients[ y ] )

            if ( ( exponents[ x ] == 0 ) && ( r.exponents[ y ] == 0 ) )
               temp.coefficients[ 0 ] +=
                  coefficients[ x ] * r.coefficients[ y ];
            else
            {
               temp.coefficients[ s ] =
                  coefficients[ x ] * r.coefficients[ y ];
               temp.exponents[ s ] = exponents[ x ] + r.exponents[ y ];
               s++;
            } // end else

   polynomialCombine( temp ); // combine common terms
   return temp;
} // end function operator*

void Polynomial::polynomialCombine( Polynomial &w )
{
   Polynomial temp = w;

   // zero out elements of w
   for ( int x = 0; x < maxTerms; x++ )
   {
      w.coefficients[ x ] = 0;
      w.exponents[ x ] = 0;
   } // end for

   for ( int x = 1; x < maxTerms; x++ )
   {
      for ( int y = x + 1; y < maxTerms; y++ )
         if ( temp.exponents[ x ] == temp.exponents[ y ] )
         {
            temp.coefficients[ x ] += temp.coefficients[ y ];
            temp.exponents[ y ] = 0;
            temp.coefficients[ y ] = 0;
         } // end if
   } // end outer for

   w = temp;
} // end function polynomialCombine

Polynomial &Polynomial::operator*=( const Polynomial &r )
{
   *this = *this * r;
   return *this;
} // end function operator*=

void Polynomial::enterTerms()
{
   bool found = false;
   int c, e, term;

   cout << "\nEnter number of polynomial terms: ";
   cin >> numberOfTerms;

   for ( int n = 0; n < maxTerms && n < numberOfTerms; n++ )
   {
      cout << "\nEnter coefficient: ";
      cin >> c;
      cout << "Enter exponent: ";
      cin >> e;

      if ( c != 0 )
      {
         // exponents of zero are forced into first element
         if ( e == 0 )
         {
            coefficients[ 0 ] += c;
            continue;
         } // end if

         for ( term = 1; ( term < maxTerms ) &&
                ( coefficients[ term ] != 0 ); term++ )

            if ( e == exponents[ term ] )
            {
               coefficients[ term ] += c;
               exponents[ term ] = e;
               found = true;  // existing exponent updated
            } // end if

         if ( !found ) // add term    found为false执行
         {
            coefficients[ term ] += c;
            exponents[ term ] = e;
         } // end if
      } // end outer if
   } // end outer for
} // end function endTerms

int Polynomial::getNumberOfTerms() const
{
   return numberOfTerms;
} // end function getNumberOfTerms

int Polynomial::getTermExponent( int term ) const
{
   return exponents[ term ];
} // end function getTermExponent

int Polynomial::getTermCoefficient( int term ) const
{
   return coefficients[ term ];
} // end function getTermsCoefficient

void Polynomial::setCoefficient( int term, int coefficient )
{
   if ( coefficients[ term ] == 0 ) // no term at this location
      cout << "No term at this location, cannot set term." << endl;
   else // otherwise, set term
      coefficients[ term ] = coefficient;
} // end function setTerm

// destructor
Polynomial::~Polynomial()
{
   // empty destructor
} // end destructor

main.cpp

#include <iostream>
#include "Polynomial.h"
using namespace std;

int main()
{
   Polynomial a, b, c, t;

   a.enterTerms();
   b.enterTerms();
   t = a;   // save the value of a
   cout << "First polynomial is:\n";
   a.printPolynomial();
   cout << "Second polynomial is:\n";
   b.printPolynomial();
   cout << "\nAdding the polynomials yields:\n";
   c = a + b;
   c.printPolynomial();
   cout << "\n+= the polynomials yields:\n";
   a += b;
   a.printPolynomial();
   cout << "\nSubtracting the polynomials yields:\n";
   a = t;  // reset a to original value
   c = a - b;
   c.printPolynomial();
   cout << "\n-= the polynomials yields:\n";
   a -= b;
   a.printPolynomial();
   cout << "\nMultiplying the polynomials yields:\n";
   a = t;  // reset a to original value
   c = a * b;
   c.printPolynomial();
   cout << "\n*= the polynomials yields:\n";
   a *= b;
   a.printPolynomial();
   cout << endl;
} // end main

myself: 用数组表示,下表表示指数,值表示系数

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>

class Polynomial
{
    static const int SIZE = 30;
    public:
        Polynomial();
        Polynomial(const Polynomial &);
        void enterTerm();
        void printPoly()const;
        Polynomial operator+(const Polynomial &)const;
        Polynomial operator=(const Polynomial &)const;
        virtual ~Polynomial();

    protected:

    private:
        int coe[SIZE];
        int maxExp;
};

#endif // POLYNOMIAL_H

#include "Polynomial.h"
#include <iostream>
#include <iomanip>

using namespace std;

Polynomial::Polynomial()
{
    for(int i = 0; i <= maxExp; ++i)
    {
        coe[i] = 0;
    }
    maxExp = 0;
}
Polynomial::Polynomial(const Polynomial &p)
{
    if(!maxExp)
        Polynomial();
    else
    {
        maxExp = p.maxExp;
        for(int i = 0; i <=maxExp; i++)
            coe[i] = p.coe[i];
    }
}
void Polynomial::enterTerm()
{
    int e;
    int c;
    maxExp = 0;
    int cnt;
    cout << "Enter the number of polynomial term: ";  //输入的项数
    cin >> cnt;
    for(int i = 1; i <= cnt; i++)
    {
        cout << "Enter the coefficient:";
        cin >> c;
        cout << "Enter the exponent:";
        cin >> e;
        if(e>maxExp)
            maxExp = e;
        coe[e] = c;
    }
}

Polynomial Polynomial::operator+(const Polynomial &p) const
{
    Polynomial temp;
    temp.maxExp = (maxExp >= p.maxExp)? maxExp:p.maxExp;
    for(int i = 0; i <= temp.maxExp; ++i)
        temp.coe[i] = coe[i] + p.coe[i];
    return temp;
}

Polynomial Polynomial::operator=(const Polynomial &p)const
{
    Polynomial temp;
    temp.maxExp = p.maxExp;
    for(int i = 0; i <= p.maxExp; ++i)
        temp.coe[i] = p.coe[i];
    return temp;
}

void Polynomial::printPoly() const
{
    if(maxExp == 0)
    {
        cout << "there is no term!!" << endl;
        return ;
    }
    bool flag = false;
    if(coe[0])
    {
        cout << coe[0];
        flag = true;
    }
    for(int i = 1;i <= maxExp; ++i)
    {
        if(coe[i])
        {
            if(!flag)
            {
                 cout << coe[i] << "X^" << i;
                 flag = true;
            }
            else
            {
                cout << showpos << coe[i] << "X^" << noshowpos << i;
            }
        }
    }

    cout << endl;
}

Polynomial::~Polynomial()
{
    //dtor
}


#include <iostream>
#include "Polynomial.h"

using namespace std;

int main()
{
    Polynomial p1;
    p1.enterTerm();
    cout << "P1 is:" << endl;
    p1.printPoly();
    Polynomial p2=p1;
    cout << "P2 is:" << endl;
    p2.printPoly();
    Polynomial p3 = p1+p2;
    cout << "P3 is:" << endl;
    p3.printPoly();


    Polynomial p4(p3);
    cout << "P4 is:" << endl;
    p4.printPoly();


    Polynomial p5;
    p5.enterTerm();
    cout << "P5 is:" << endl;
    p5.printPoly();

    Polynomial p6 = p1+p5;
    cout << "P6 is:" << endl;
    p6.printPoly();
    return 0;
}

5. 几个类(Vehicle 类 Car 类 Streetwheel 类 Brake 类 )有着必然的联系,设计类并实现。

#ifndef BRAKE_H
#define BRAKE_H


class Brake
{
    public:
        Brake(const bool state);
        Brake(const Brake&);
        void setBrakeState(bool);
        bool getBrakeState()const; // 不能修改该类的成员数据

        virtual ~Brake();

    protected:

    private:
        bool brakeState;
};

#endif // BRAKE_H

#include "Brake.h"

Brake::Brake(const bool state)
{
    brakeState = state;
}

Brake::Brake(const Brake &b)
{
    brakeState = b.brakeState;
}

void Brake::setBrakeState(bool state)
{
    brakeState = state;
}

bool Brake::getBrakeState() const
{

    return brakeState;
}

Brake::~Brake()
{
    //dtor
}

#ifndef STEERINGWHEEL_H
#define STEERINGWHEEL_H


class SteeringWheel
{
    public:
        SteeringWheel(double = 0, int = 0);
        SteeringWheel(const SteeringWheel &);
        void setAngle(double);
        void setDirection(int);
        double getAngle() const;
        int getDirection() const;

        void print()const;
        virtual ~SteeringWheel();

    protected:

    private:
        double angle;
        int direction;
};

#endif // STEERINGWHEEL_H

#include "SteeringWheel.h"
#include <iostream>

using namespace std;

SteeringWheel::SteeringWheel(double ang,int dir)
{
    angle = ang;
    direction = dir;
}

SteeringWheel::SteeringWheel(const SteeringWheel &s)
{
    angle = s.angle;
    direction = s.direction;
}

void SteeringWheel::setAngle(double ang)
{
    angle = ang;
}

void SteeringWheel::setDirection(int dir)
{
    direction = dir;
}

double SteeringWheel::getAngle() const
{
    return angle;
}

int SteeringWheel::getDirection() const
{
    return direction;
}

void SteeringWheel::print() const
{
    cout<<"Now the car's direction is:" <<( direction < 0 ? "left " : "right ")<<
    angle << " degree\n";
}



SteeringWheel::~SteeringWheel()
{
    //dtor
}
#ifndef VEHICLE_H
#define VEHICLE_H
#include <cstring>

class Vehicle
{
    public:
        Vehicle(const char*);
        char *getName();
        virtual ~Vehicle();

    protected:

    private:
        char name[100];
};

#endif // VEHICLE_H

#include "Vehicle.h"
#include <iostream>
#include <cstring>

using namespace std;

Vehicle::Vehicle(const char* names)
{
    strcpy(name,names);
    cout << "Create a new Vehicle name is: " << name << endl;
}

char *Vehicle::getName()
{
    return name;
}

Vehicle::~Vehicle()
{
    //dtor
}
#ifndef CAR_H
#define CAR_H

#include "Vehicle.h"
#include "Brake.h"
#include "SteeringWheel.h"

class Car:public Vehicle
{
    public:
        Car(char [], const Brake &, const SteeringWheel &);
        void setValue(bool state)
        {
            brake.setBrakeState(state);
        }
        void setValue(double ang,int dir)
        {
            steeringWheel.setAngle(ang);
            steeringWheel.setDirection(dir);
        }
        void print();
        virtual ~Car();

    protected:

    private:
        Brake brake;
        SteeringWheel steeringWheel;
};

#endif // CAR_H

#include "Car.h"
#include <iostream>

using namespace std;

Car::Car(char names[],const Brake &b, const SteeringWheel &sw)
:Vehicle(names),brake(b),steeringWheel(sw)
{
    //ctor
}
void Car::print( )
{
    cout<<"The car's name is:"<< getName() <<endl;
    cout<<"The brake is now:"<< (brake.getBrakeState( ) ? "on ":"off")<<endl ;
    steeringWheel.print( );
}
Car::~Car()
{
    //dtor
}
#include <iostream>
#include "Car.h"

using namespace std;

using namespace std;

int main()
{
    Brake brake(false);
    SteeringWheel streetWheel( 30 , -1 ) ;
    Car car("BMW", brake , streetWheel ) ;
    car.print();
    car.setValue(true);
    car.setValue(20,1);
    car.print( ) ;


    cout << "Hello world!" << endl;
    return 0;
}

6. 一个基类 Shape,在基类的基础上继承编写一个二维图形类,在继承写一个三维图形类, 设计并实现。

13.13 P479

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值