18.03.14 类和对象作业2

A:Apple

描述

程序填空,使其输出4 5 1

#include <iostream>
using namespace std;
class Apple {
// 在此处补充你的代码
static void PrintTotal() {
        cout << nTotalNumber << endl; 
    }

};
int Apple::nTotalNumber = 0;
Apple Fun(const Apple & a) {
    a.PrintTotal();
    return a;
}
int main()
{
    Apple * p = new Apple[4];
    Fun(p[2]);
    Apple p1,p2;
    Apple::PrintTotal ();
    delete [] p;
    p1.PrintTotal ();
    return 0;
}

输入无输出

4
5
1

样例输入

None

样例输出

4
5
1

来源Guo Wei

 1 #include <iostream>
 2 using namespace std;
 3 class Apple {
 4     static int nTotalNumber;
 5 public:
 6     Apple(){
 7         nTotalNumber++;
 8     }
 9     ~Apple(){
10         nTotalNumber--;
11     }
12 
13 static void PrintTotal() {
14         cout << nTotalNumber << endl; 
15     }
16 
17 };
18 int Apple::nTotalNumber = 0;
19 Apple Fun(const Apple & a) {
20     a.PrintTotal();
21     return a;
22 }
23 int main()
24 {
25     Apple * p = new Apple[4];
26     Fun(p[2]);
27     Apple p1,p2;
28     Apple::PrintTotal ();
29     delete [] p;
30     p1.PrintTotal ();
31     return 0;
32 }
View Code

B:奇怪的类复制

描述

程序填空,使其输出9 22 5

#include <iostream>
using namespace std;
class Sample {
public:
    int v;
// 在此处补充你的代码
};
void PrintAndDouble(Sample o)
{
    cout << o.v;
    cout << endl;
}
int main()
{
    Sample a(5);
    Sample b = a;
    PrintAndDouble(b);
    Sample c = 20;
    PrintAndDouble(c);
    Sample d;
    d = a;
    cout << d.v;
    return 0;
}

输入无输出

9
22
5

样例输入

None

样例输出

9
22
5

来源Guo Wei

 1 #include <iostream>
 2 using namespace std;
 3 class Sample {
 4 public:
 5     int v;
 6 Sample(const Sample&a){
 7         v=a.v+2;
 8     }
 9     Sample(int i=0){
10         v=i;
11     }
12 };
13 void PrintAndDouble(Sample o)
14 {
15     cout << o.v;
16     cout << endl;
17 }
18 int main()
19 {
20     Sample a(5);
21     Sample b = a;
22     PrintAndDouble(b);
23     Sample c = 20;
24     PrintAndDouble(c);
25     Sample d;
26     d = a;
27     cout << d.v;
28     return 0;
29 }
View Code

const很重要

C:返回什么才好呢

描述

程序填空,使其按要求输出

#include <iostream>
using namespace std;
class A {
public:
    int val;

    A(int
// 在此处补充你的代码
};
int main()
{
    int m,n;
    A a;
    cout << a.val << endl;
    while(cin >> m >> n) {
        a.GetObj() = m;
        cout << a.val << endl;
        a.GetObj() = A(n);
        cout << a.val<< endl;
    }
    return 0;
}

输入

多组数据,每组一行,是整数 m 和 n输出先输出一行: 
123 
然后,对每组数据,输出两行,第一行是m,第二行是n样例输入

2 3
4 5

样例输出

123
2
3
4
5 

来源Guo Wei

1 c=123){
2         val=c;
3     }
4     A& GetObj(){
5         return *this;
6     }
View Code

我……不知道有this指针的存在……(弱死)

D:第四周程序填空题1

描述

下面程序的输出是:

3+4i

5+6i

请补足Complex类的成员函数。不能加成员变量。

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex {
private:
    double r,i;
public:
    void Print() {
        cout << r << "+" << i << "i" << endl;
    }
// 在此处补充你的代码
};
int main() {
    Complex a;
    a = "3+4i"; a.Print();
    a = "5+6i"; a.Print();
    return 0;
}

输入

输出

3+4i
5+6

i样例输入

None

样例输出

3+4i
5+6i
1   Complex(char a[]="0"){
2         r=a[0]-'0';
3         i=a[2]-'0';
4     }
View Code

emmm……还没学重载不知道发生了啥,一开始一直RE重载了一下赋值运算符然后突然过了??

E:MyString

描述

补足MyString类,使程序输出指定结果

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class MyString {
    char * p;
public:
    MyString(const char * s) {
        if( s) {
            p = new char[strlen(s) + 1];
            strcpy(p,s);
        }
        else
            p = NULL;

    }
    ~MyString() { if(p) delete [] p; }
// 在此处补充你的代码
};
int main()
{
    char w1[200],w2[100];
    while( cin >> w1 >> w2) {
        MyString s1(w1),s2 = s1;
        MyString s3(NULL);
        s3.Copy(w1);
        cout << s1 << "," << s2 << "," << s3 << endl;

        s2 = w2;
        s3 = s2;
        s1 = s3;
        cout << s1 << "," << s2 << "," << s3 << endl;
        
    }
}

输入

多组数据,每组一行,是两个不带空格的字符串

输出

对每组数据,先输出一行,打印输入中的第一个字符串三次
然后再输出一行,打印输入中的第二个字符串三次样例输入

abc def
123 456

样例输出

abc,abc,abc
def,def,def
123,123,123
456,456,456

来源Guo Wei

 1 ~MyString() { if(p) delete [] p; }
 2     MyString(const MyString&s) {
 3         p = new char[strlen(s.p) + 1];
 4         strcpy(p, s.p);
 5     }
 6     void Copy(const char*s) {
 7         p = new char[strlen(s) + 1];
 8         strcpy(p, s);
 9     }
10     friend ostream & operator << (ostream &output, const MyString &s) {
11         output << s.p;
12         return output;
13     }
14     MyString& operator=(const char*s) {
15         delete[]p;
16         p = new char[strlen(s) + 1];
17         strcpy(p, s);
18         return *this;
19     }
20     void operator=(MyString& a) {
21         strcpy(p,a.p);
22     }
View Code

F:Big & Base 封闭类问题

描述

程序填空,输出指定结果

#include <iostream>
#include <string>
using namespace std;
class Base {
public:
    int k;
    Base(int n):k(n) { }
};
class Big
{
public:
    int v;
    Base b;
// 在此处补充你的代码
};
int main()
{
    int n;
    while(cin >>n) {
        Big a1(n);
        Big a2 = a1;
        cout << a1.v << "," << a1.b.k << endl;
        cout << a2.v << "," << a2.b.k << endl;
    }
}

输入

多组数据,每组一行,是一个整数

输出

对每组数据,输出两行,每行把输入的整数打印两遍样例输入

3
4

样例输出

3,3
3,3
4,4
4,4	

来源Guo Wei

1  Big(int n):v(n),b(n){
2     }
View Code

 

posted @ 2018-03-14 23:35  TobicYAL  阅读(3399)  评论(3编辑  收藏  举报