C++第三章习题

C++第三章习题

  1. 写出程序的运行结果:
#include <iostream>
using namespace std;
class toy
{
public:
    toy(int q, int p)
    {
        quan = q;
        price = p;
    }
    int get_quan() { return quan; }
    int get_price() { return price; }

private:
    int quan, price;
};
int main()
{
    toy op[3][2] = {toy(10, 20), toy(30, 48), toy(50, 68), toy(70, 80), toy(90, 16), toy(11, 120)};
    for (int i = 0; i < 3; i++)
    {
        cout << op[i][0].get_quan() <<,;
        cout << op[i][0].get_price() <<”\n”;
        cout << op[i][1].get_quan() <<,;
        cout << op[i][1].get_price() <<”\n”;
    }
    cout << endl;
    return 0;
}

在这里插入图片描述
13. 写出程序的运行结果:

#include <iostream>
using namespace std;
class example
{
public:
    example(int n)
    {
        i = n;
        cout <<"Constructing\n";
    }
    ~example() { cout <<"Destructing\n"; }
    int get_i() { return i; }

private:
    int i;
};
int sqr_it(example o) { return o.get_i() * o.get_i(); }
int main()
{
    example x(10);
    cout << x.get_i() << endl;
    cout << sqr_it(x) << endl;
    return 0;
}

在这里插入图片描述
14. 写出程序的运行结果:

#include <iostream>
using namespace std;
class aClass
{
public:
    aClass() { total++; }
    ~aClass() { total--; }
    int gettotal() { return total; }

private:
    static int total;
};
int aClass::total = 0;
int main()
{
    aClass o1, o2, o3;
    cout << o1.gettotal() <<"objects in existence\n";
    aClass *p;
    p = new aClass;
    if (!p)
    {
        cout <<"Allocation error \n";
        return 1;
    }
    cout << o1.gettotal();
    cout <<"ojects in existence after allocation\n";
    delete p;
    cout << o1.gettotal();
    cout <<"ojects in existence after deletion\n";
    return 0;
}

在这里插入图片描述
15. 写出程序的运行结果:

#include <iostream>
using namespace std;
class test
{
public:
    test();
    ~test() {}

private:
    int i;
};
test::test()
{
    i = 25;
    for (int ctr = 0; ctr < 10; ctr++)
    {
        cout << "Counting at" << ctr << "\n";
    }
}
test anObject;
int main() { return 0; }

在这里插入图片描述
16. 写出程序的运行结果:

#include <iostream>
using namespace std;
class A
{
    int a, b;

public:
    A()
    {
        a = 0;
        b = 0;
        cout <<"Default constructor called.\n";
    }
    A(int i, int j)
    {
        a = i;
        b = j;
        cout <<"Constructor : a ="<< a <<", b ="<< b << endl;
    }
};
int main()
{
    A a[3];
    A b[3] = {A(1, 2), A(3, 4), A(5, 6)};
    return 0;
}

在这里插入图片描述
17. 写出程序的运行结果:

#include <iostream>
using namespace std;
class Test
{
    int val;

public:
    Test() { cout <<"default."<< endl; }
    Test(int n)
    {
        val = n;
        cout <<"Con."<< endl;
    }
    Test(const Test &t)
    {
        val = t.val;
        cout <<"Copy con."<< endl;
    }
};
int main()
{
    Test t1(6);
    Test t2 = t1;
    Test t3;
    t3 = t1;
    return 0;
}

在这里插入图片描述
18. 写出程序的运行结果:

#include <iostream>
using namespace std;
class N
{
    int A;
    static int B;

public:
    N(int a)
    {
        A = a;
        B += a;
    }
    static void f1(N m);
};
void N::f1(N m)
{
    cout <<"A ="<< m.A << endl;
    cout <<"B ="<< B << endl;
}
int N::B = 0;
int main()
{
    N P(5), Q(9);
    N::f1(P);
    N::f1(Q);
    return 0;
}

在这里插入图片描述
19. 写出程序的运行结果:

#include <iostream>
using namespace std;
class M
{
    int x, y;

public:
    M() { x = y = 0; }
    M(int i, int j)
    {
        x = i;
        y = j;
    }
    void copy(M *m);
    void setxy(int i, int j)
    {
        x = i;
        y = j;
    }
    void print() { cout << x << ","<< y << endl; }
};
void M::copy(M *m)
{
    x = m->x;
    y = m->y;
}
void fun(M m1, M *m2)
{
    m1.setxy(12, 15);
    m2->setxy(22, 25);
}
int main()
{
    M p(5, 7), q;
    q.copy(&p);
    fun(p, &q);
    p.print();
    q.print();
    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值