版本3和4间的区别
于2005-12-18 01:46:34修订的的版本3
大小: 2578
编辑: czk
备注:
于2005-12-18 01:50:26修订的的版本4
大小: 5110
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 57: 行号 57:
 1. {{{#!cplusplus
#include <string>
#include <iostream>
using namespace std;
class S {
public:
    S(int a0) { cout << a0 << endl; }
    S(double a0, int b0) : d(a0), b(b0){ cout << a0 << b0 << endl; }
    S(char *p0) : d(a), b(2) { cout << p << endl;}
    S(int &a0) : a(0), b(1), c("abc"), d(r), p(new int) { }
private:
    int a;
    const int b;
    string c;
    int &d;
    int *p;
};
int main() {
    S a(1);
    S b;
    S c(0.0, 1);
    S d("hello");
    S e(true);
    S f(a);
    e = a;
}
}}}
 1. {{{#!cplusplus
#include <string>
#include <iostream>
using namespace std;
class Person{
public:
    Person(string name) : name_ (name) { }
    virtual void print() const = 0 { cout << name_; }
    virtual bool compare(Person p) const { return p.name_ == name_; }
protected:
    string name_;
};
class Student : public Person {
    Student(string name, string id) { name_ = name; id_ = id; }
    Student(Person p, string id) { name_ = p.name_; id_ = id;}
    bool compare(Student s) const { return s.name_==name_ && s.id_ == id_; }
    void print() { cout << name_ << id_ << endl; }
private:
    string id_;
};

int main() {
    Person p("Mike");
    Student s("jack", "12345");
    Student t(p, "23424");
    cout << s.compare(t);
    cout << p.compare(t);
    cout << s.compare(p);
    s.print();
    s.Person::print();
}
}}}
 1. {{{#!cplusplus
class Integer{
public:
    Integer(int a = 0) : i(a) {}
    Integer operator+(int a, int b) { return a + b;}
    Integer operator-() { return -i; }
    Integer operator+() { return i; }
    Integer operator++() { return ++i; }
    Integer operator++(int) { return i++; }
    bool operator<>(int a) { return i != a; }
private:
    int i;
};
void operator>>(ostream &os, Integer i) {
    os << i;
}
istream & operator<<(istream &is, Integer i) {
    is >> i.i;
    return is;
}
Integer operator-(Integer a, Integer b) {
    return a.i - b.i;
}
int main() {
    Integer i(-1);
    Integer j;
    cin >> j;
    i = -i;
    i = i - j;
    i = i + 10;
    cout << i;
}
}}}
行号 119: 行号 211:
 1. {{{#!cplusplus
#include <iostream>
using namespace std;
class X {
public:
    X(int a) : x(a) { cout << "create X:" << x << endl; }
    ~X() { cout << "destroy X:"<< x << endl; }
private:
    int x;
};
class Y : public X{
public:
    Y() : x(1), X(2) { cout << "create Y" << endl; }
    ~Y() { cout << "destroy Y" << endl; }
private:
    X x;
};

int main() {
    X x(5);
    Y y;
}
}}}

找出以下程序中的语法错误,并作出解释。

  1.    1 #include <stdlib.h>
       2 int main() {
       3     const int size;
       4     size = 10;
       5     int a[size];
       6     int *p1 = new int[size];
       7     int *p2 = malloc(sizeof(int)* size);
       8     int *p3 = new int(size);
       9     for(int i = 0; i < size; i++) {
      10         a[i] = i;
      11         p1[i] = a[i] * i;
      12         p2[i] = p1[i] * i;
      13         p3[i] = p2[i] * i;
      14     }
      15     int sum;
      16     for(int i = 0; i < size; i++)
      17         sum += a[i] + p1[i] + p2[i] + p3[i];
      18     cout << sum;
      19     delete p1;
      20     delete [] p2;
      21     delete p3;
      22 }
    
  2.    1 #include <iostream>
       2 using namespace std;
       3 class N {
       4     static int n = 0;
       5     int i;
       6 public:
       7     void N() : i(0) { }
       8     void N(int a = 0) : i (a) { }
       9     void N(int a = 0, int b) : i(a), n(b) { }
      10     int get() const { return i++; }
      11     void set(int a) { i = a;}
      12     void print() const { cout << get() << endl;}
      13     static int getn() const { return n; }
      14     static void setn(int a) { n = a; i++; }  
      15 }
      16 int main() {
      17     cout << N::n << endl;
      18     cout << N::getn() << endl;
      19     cout << get() << endl;
      20     cout << N::get() << endl;
      21     cout << N::setn(10) << endl;
      22     N *p = new N(5);
      23     N n();
      24     n.set(10);
      25     n.setn(11);
      26     p.set(5);
      27     delete p;
      28 }
    
  3.    1 #include <string>
       2 #include <iostream>
       3 using namespace std;
       4 class S {
       5 public:
       6     S(int a0) { cout << a0 << endl; }
       7     S(double a0, int b0) : d(a0), b(b0){ cout << a0 << b0 << endl; }
       8     S(char *p0) : d(a), b(2) { cout << p << endl;} 
       9     S(int &a0) : a(0), b(1), c("abc"), d(r), p(new int) { }
      10 private:
      11     int a;
      12     const int b;
      13     string c;
      14     int &d;
      15     int *p;
      16 };
      17 int main() {
      18     S a(1);
      19     S b;
      20     S c(0.0, 1);
      21     S d("hello");
      22     S e(true);
      23     S f(a);
      24     e = a;
      25 }
    
  4.    1 #include <string>
       2 #include <iostream>
       3 using namespace std;
       4 class Person{
       5 public:
       6     Person(string name) : name_ (name) { }
       7     virtual void print() const = 0 { cout << name_; }
       8     virtual bool compare(Person p) const { return p.name_ == name_; }
       9 protected:
      10     string name_;
      11 };
      12 class Student : public Person {
      13     Student(string name, string id) { name_ = name; id_ = id; }
      14     Student(Person p, string id) { name_ = p.name_; id_ = id;}
      15     bool compare(Student s) const { return s.name_==name_ && s.id_ == id_; }
      16     void print() { cout << name_ << id_ << endl; }
      17 private:
      18     string id_;
      19 };
      20 
      21 int main() {
      22     Person p("Mike");
      23     Student s("jack", "12345");
      24     Student t(p, "23424");
      25     cout << s.compare(t);
      26     cout << p.compare(t);
      27     cout << s.compare(p);
      28     s.print();
      29     s.Person::print();
      30 }
    
  5.    1 class Integer{
       2 public:
       3     Integer(int a = 0) : i(a) {}
       4     Integer operator+(int a, int b) { return a + b;}
       5     Integer operator-() { return -i; }
       6     Integer operator+() { return i; }
       7     Integer operator++() { return ++i; }
       8     Integer operator++(int) { return i++; }
       9     bool operator<>(int a) { return i != a; }
      10 private:
      11     int i;
      12 };
      13 void operator>>(ostream &os, Integer i) {
      14     os << i;
      15 }
      16 istream & operator<<(istream &is, Integer i) {
      17     is >> i.i;
      18     return is;
      19 } 
      20 Integer operator-(Integer a, Integer b) {
      21     return a.i - b.i;
      22 }
      23 int main() {
      24     Integer i(-1);
      25     Integer j;
      26     cin >> j;
      27     i = -i;
      28     i = i - j;
      29     i = i + 10;
      30     cout << i;
      31 }
    

读程序,写出程序运行的结果

  1.    1 #include <iostream>
       2 using namespace std;
       3 int inc( int &a) {
       4     return ++a;
       5 }
       6 int dec( int a) {
       7     return --a;
       8 }
       9 int main() {
      10     int b = 10;
      11     cout << inc(b) << endl;
      12     cout << dec(b) << endl;
      13     cout << inc(b) << endl;
      14 }
    
  2.    1 #include <iostream>
       2 #include <string>
       3 using namespace std;
       4 int f( string a );
       5 int f( int b , int c = 1);
       6 int f( bool b);
       7 int main() {
       8     string s = "";
       9     cout << f(s) << endl;
      10     cout << f('\060') << endl;
      11     cout << f(1, 2) << endl;
      12     cout << f(false) << endl;
      13     cout << f(0) << endl;
      14 }
      15 int f( string a) {
      16     return a.length();
      17 }
      18 int f( int b, int c) {
      19     return b * c + b + c + 1;
      20 }
      21 int f(bool b) {
      22     return b?1:-1;
      23 }
    
  3.    1 #include <iostream>
       2 using namespace std;
       3 class Z {
       4 public:
       5     Z(int i) : id(i) { cout << id << " created" << endl; }
       6     ~Z() { cout << id << " destroyed" << endl; }
       7     Z(Z &z) : id(z.id) { cout << id << " created a copy" << endl; }
       8 private:
       9     int id;
      10 };
      11 Z z(0);
      12 int main() {
      13     cout << "main start" << endl;
      14     Z z1(1);
      15     Z z2(2);
      16     Z *z3 = new Z(3);
      17     Z z4(z1); 
      18 }
    
  4.    1 #include <iostream>
       2 using namespace std;
       3 class X {
       4 public: 
       5     X(int a) : x(a) { cout << "create X:" << x << endl; }
       6     ~X() { cout << "destroy X:"<< x << endl; }
       7 private:
       8     int x;
       9 };
      10 class Y : public X{
      11 public:
      12     Y() : x(1), X(2) { cout << "create Y" << endl; }
      13     ~Y() { cout << "destroy Y" << endl; }
      14 private:
      15     X x;
      16 };
      17 
      18 int main() {
      19     X x(5);
      20     Y  y;
      21 }
    

程序填空题

编程题,按要求编写程序

C++练习 (2008-06-27 10:53:51由218编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.