C++day03 作业

C++Day4

[C++语法规则很多,要落实下来,得通过多敲代码来理解,看N遍不如写一次;在写代码的过程中,会碰到其它你不曾碰到过的编译问题,切记程序是调试出来的;再就是通过练习,把敲代码的速度提升上来,熟悉键盘,培养写代码的感觉]

一、简答题

  1. 什么是左值与右值,拷贝构造函数中的引用与const为什么不能去掉?

    相对于表达式而言,左值表示的是一个对象的身份,右值表示的是对象的值。
    引用如果去掉,会导致递归调用。
    const如果去掉,可能导致对引用类型的参数值产生意外修改。

     

  2. this指针是什么? 有什么作用呢?

    类的每个成员函数都有一个this指针,指向调用自己的类对象.这个指针不可改变也不可赋值.

     

  3. 必须在构造函数初始化列表中初始化有哪几种情况?

1.带有const修饰的类成员 ,如const int a ;
2.包含引用成员,如 int& p;
3.类类型的成员没有默认构造函数(就是类中的另一个类类型的成员没有默认的参数为空的构造函数):

 

  1. 静态数据成员的初始化在哪里,需要注意什么?

初始化在类体外进行,一般放在该类的实现部分最合适,也可以放在其他位置,例如,放在主函数前面等;静态数据成员初始化与该类的构造函数和析构函数无关;在静态成员函数的实现中,可以直接引用静态成员,但不能直接引用非静态成员。

 

二、写出下面程序结果。

1、写出以下程序运行的结果。

#include <math.h>
#include <iostream>

using std::endl;
using std::endl;

class Point
{
public:
   Point(int xx = 0, int yy = 0)
{
X = xx;
Y = yy;
cout << "point构造函数被调用" << endl;
}


  Point(Point &p);
 
  int GetX()
  {
  return X;
  }
 
  int GetY()
  {
  return Y;
  }

private:
int X,Y;
};

Point::Point(Point &p)
{
X = p.X;
Y = p.Y;
cout << "X = " << X << " Y=" << Y << "Point拷贝构造函数被调用" << endl;
}

class Distance
{
public:
Distance(Point xp1, Point xp2);
double GetDis()
{
return dist;
}
private:
Point p1,p2;
double dist;
};

Distance::Distance(Point xp1, Point xp2)
: p1(xp1)
,p2(xp2)
{
cout << "Distance构造函数被调用" << endl;
double x = double(p1.GetX() - p2.GetX());
double y = double(p1.GetY() - p2.GetY());
dist = sqrt(x * x + y * y);
}

int main()
{
Point myp1(1,1), myp2(4,5);
Distance myd(myp1, myp2);
cout << "The distance is:" ;
cout << myd.GetDis() << endl;

return 0;
}
point构造函数被调用
point构造函数被调用
X = 4 Y=5Point拷贝构造函数被调用
X = 1 Y=1Point拷贝构造函数被调用
X = 1 Y=1Point拷贝构造函数被调用
X = 4 Y=5Point拷贝构造函数被调用
Distance构造函数被调用
The distance is:5

 

2、写出以下程序运行的结果。

#include<iostream>
using namespace std;
class MyClass
{
public:
   MyClass(int i = 0)
  {
       cout << i;
  }
   MyClass(const MyClass &x)
  {
       cout << 2;
  }
   MyClass &operator=(const MyClass &x)
  {
       cout << 3;
       return *this;
  }
   ~MyClass()
  {
       cout << 4;
  }
};
int main()
{
   MyClass obj1(1), obj2(2);
   MyClass obj3 = obj1;
   return 0;
}
122444

 

3、不考虑任何编译器优化(如:NRVO),下述代码的第10#会发生

-fno-elide-constructors

#include <iostream>

using std::cout;
using std::endl;

classB
{
public:
B()
{
       cout << "B()" << endl;
  }

   ~B()
  {
  cout << "~B()" << endl;
  }
   
   B(const B &rhs)
  {
       cout << "B(const B&)" << endl;
  }
   
   B &operator=(const B &rhs)
  {
  cout << "B &operator=(const B &s)" << endl;
   
       return  *this;
  }
};

B func(const B &rhs)
{
   cout << "B func(const B &)" << endl;
   return rhs;
}


int main(int argc, char **argv)
{
B b1,b2;
   b2=func(b1);//10#

return 0;
}
优化前比优化后多输出一次拷贝构造函数中的输出内容

 

三、编程题。

1、实现一个自定义的String类,保证main函数对正确执行

class String
{
public:
String()
String(const char *pstr);
String(const String &rhs);
String &operator=(const String &rhs);
~String();
void print();

private:
char * _pstr;
};

int main()
{
String str1;
str1.print();


String str2 = "Hello,world";
String str3("wangdao");

str2.print();
str3.print();

String str4 = str3;
str4.print();

str4 = str2;
str4.print();

return 0;
}
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class String
{
public:
String()
{
this->_pstr = nullptr;
}
String(const char *pstr)
{
this->_pstr = new char[strlen(pstr) + 1];
strcpy(this->_pstr, pstr);
}
String(const String &rhs)
{
this->_pstr = new char[strlen(rhs._pstr) + 1];
strcpy(this->_pstr, rhs._pstr);
}
String &operator=(const String &rhs)
{
if (this->_pstr)
{
delete[] this->_pstr;
}
this->_pstr = new char[strlen(rhs._pstr) + 1];
strcpy(this->_pstr, rhs._pstr);
}
~String()
{
if (this->_pstr)
{
delete[] this->_pstr;
this->_pstr = nullptr;
}
}
void print()
{
if (this->_pstr)
cout << this->_pstr << endl;
}

private:
char *_pstr;
};

int main()
{
String str1;
str1.print();

String str2 = "Hello,world";
String str3("wangdao");

str2.print();
str3.print();

String str4 = str3;
str4.print();

str4 = str2;
str4.print();

return 0;
}
jing@jing:~/code/2022-3-30$ ./sample1
Hello,world
wangdao
wangdao
Hello,world

 

四、算法题(选做)

1、二进制中1的个数

题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数。例如:把9表示成二进制是1001,有2位是1。因此如果输入9,该函数输出2。

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int func(int x)
{
   int countx = 0;
   while(x)
  {
         countx++;
         x = x&(x-1);
  }
   return countx;
}
int main()
{
   int x;
cin>>x;
int a=func(x);
cout<<a<<"个1"<<endl;
cout <<"二进制:"<<bitset<10>(x) <<endl;
}
jing@jing:~/code/2022-3-30$ ./homework_2
9
2个1
二进制:0000001001

 

 

2、数组中出现次数超过一半的数字

题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1, 2, 3, 2, 2, 2, 5, 4, 2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。

#include <bits/stdc++.h>
using namespace std;
int HalfNum(vector<int> numbers)
{
map<int, int> mp;
for (const int val : numbers)
{
++mp[val];
}
// for (auto it = mp.begin(); it != mp.end(); it++)
// {
// cout << it->first << " " << it->second << endl;
// }
int len = numbers.size() / 2;
// cout << len << endl;
for (auto it = mp.begin(); it != mp.end(); it++)
{
if (it->second >= len)
{
cout << "the most number: " << it->first << endl;
cout<<"the count: "<<it->second<<endl;
}
}
}
int main()
{
vector<int> numbers;
int x;
for (int i = 0; i < 10; i++)
{
cin >> x;
numbers.push_back(x);
}
HalfNum(numbers);
return 0;
}
jing@jing:~/code/2022-3-30$ ./homework_1
1 2 3 2 2 2 5 4 2 2
the most number: 2
the count: 6

 

posted @   Fancele  阅读(335)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示