C++基础语法练习

#include <iostream>
#include <cstdio>
using namespace std;

class A
{
public:
    A(){ cout << "A's default constructor called" << endl; }
    A(int i) {
        a = i;
        cout << "A's constructor called" << endl;
    }
    void print() { cout << a; }
    ~A() { cout << "A's destructor called" << endl; }
    int getData(){ return a; }
private:
    int a;
};

class B:public A
{
public:
    B(){ cout << "B's default constuctor called" << endl; }
    B(int i, int j, int k): A(i), aa(j) {
        b = k;
        cout << "B's constructor called" << endl;
    }
    void print();
    ~B() { cout << "B's destructor called" << endl; }
private:
    int b;
    A aa;
};
void B::print(){
    A::print();
    cout << b << aa.getData() << endl;
}

int main() {
    B bb[2] = { B(1, 2, 3), B(20, 30, 40) };
    for(int i = 0; i < 2; i++)
        bb[i].print();
    return 0;
}

int &f1(int &a) {
    a += a;
    return a;
}
int f2(int b) {
    b += b;
    return b;
}
int main()
{
    int x, y, z;
    x = 10;
    y = f1(x);
    z = f2(x);
    cout << x << endl;
    cout << y << endl;
    cout << z << endl;
}
class A
{
private:
    int a;
    static int b;
public:
    A(){}
    A(int i) {
        a = i;
        b += i;
    }
    void out() {
        cout << a << b << endl;
    }
};
int A::b = 0;

int main() {
    A obj1(10);
    obj1.out();
    A obj2(15);
    obj2.out();
    obj1.out();
}

int i = 0, sum = 0;
int main()
{
    for(;;){
        i += 2;
        if(i > 10) break;
        else if(i == 8) continue;
        sum += i;
    }
    cout << i << sum <<  endl;
}

int main()
{
    int a = 1, b = 2, c = 3;
    int d = 4, y = 10;
    switch(y)
    {
    case 1:
        a++; break;
    default:
        d = 1;
    case 2:
        b++; break;
    case 3:
        c++; break;
    }
    cout << a << b << c << d << endl;
}

int main()
{
    int i, j, m, n;
    i = 2;
    j = 4;
    m = ++i + j++;
    n = (++i) + (++j);
    cout << i << j << m << n << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值