类和对象——练习题

选择题:

1. 以下代码共调用多少次拷贝构造函数?

W f(W u)
{
//cout << "--------" << endl;
W v(u); 
W w = v;
//cout << "--------" << endl;
return w;
}

int main()
{
W x;
W y = f(f(x)); // 1次构造  7次拷贝  or  1次构造  5次拷贝

return 0;
}

int main()
{
W x;
W y = f(x); // 1次构造  4次拷贝

return 0;
}

 
编译器优化:


class W
{
public:
W(int x = 0)
{
cout << "W()" << endl;
}

W(const W& w)
{
cout << "W(const W& w)" << endl;
}

W& operator=(const W& w)
{
cout << "W& operator=(const W& w)" << endl;
return *this;
}

~W()
{
cout << "~W()" << endl;
}
};

void f1(W w)
{

}

void f2(const W& w)
{

}



int main()
{
W w1;
f1(w1);//加了const就没有拷贝构造了
f2(w1);
cout << endl << endl;

f1(W()); // 本来构造+拷贝构造--编译器的优化--直接构造
// 结论:连续一个表达式步骤中,连续构造一般都会优化 -- 合二为一

W w2 = 1;//优化->直接构造
 


return 0;
}
W f3()
{
W ret;
return ret;
}

// 《深度探索C++对象模型》
int main()
{
f3(); // 1次构造  1次拷贝
cout << endl << endl;

W w1 = f3(); // 本来:1次构造  2次拷贝 -- 优化:1次构造  1次拷贝

cout << endl << endl;

W w2;
w2 = f3(); // 本来:1次构造  1次拷贝 1次赋值

return 0;
}

 
 

编程题

1.求1+2+3+…+n

求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
求1+2+3+…+n

静态
对象实例化会自动调用构造函数

int i = 1;
int sum = 0;
class Sum
{
    public:
       Sum()
        {
            sum += i;
            ++i;
                    
        }
};

class Solution {
public:
    int Sum_Solution(int n) {
    Sum a[n];//调用了n次sum的构造函数
    return sum;
        
    }
};

静态——封装

class Sum
{
    pubilc:
        Sum()
        {
            _sum += _i;
            ++_i;        
        }
        static int GetSum()
        {
            return _sum;        
        }
      private:
         static int _sum;
         static int _i;  
};

int Sum::_sum = 0;
int Sum::_i = 1;

class Solution{
    public:
        int Sum_solution(int n){
            SUm a[n];
            return Sum::GetSum();        
        }
}

内部类
sum是solution的友元

class Solution{
    class Sum
    {
        public:
        Sum()
        {
            _sum += _i;
            ++_i;        
        }    
    };
    public:
        int Sum_solution(int n){
            Sum a[n];
            return _sum;        
        }
        private:
            static int _sum;
            static int _i;
};

int Solution::_sum = 0;
int Solution::_i = 1;

 
 

2. 计算日期到天数的转换

计算日期到天数转换_牛客题霸_牛客网

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
    int year,mon,day;
    int data[]={31,28,31,30,31,30,31,31,30,31,30,31};
    while(cin>>year>>mon>>day)
    {
        int sum;
        for(int i=0;i<mon-1;i++)
        {
              sum=sum+data[i];
        }
        if((year%400==0||(year%4==0&&year%100!=0))&&mon>2)
            sum=sum+1+day;
        else 
            sum=sum+day;
        cout<<sum<<endl;

    }
    return 0;
}
#include <iostream>
using namespace std;

//int main()
//{
//Date today;
//cin >> today;
//Date start;
//
//cout << today - start << endl;
//}

int main()
{
int year, month, day;
cin >> year >> month >> day;

int monthDays1_N[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

int n = monthDays1_N[month - 1] + day;

if (month > 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
n += 1;

cout << n << endl;

return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hey pear!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值