C/C++相关问题整理(1)

随机生成整数,然后算出正负零
void getrand(int *a, int m)
{
    int i;
    srand(time(NULL));

    for (i=0; i<m; i++)
    {
        a = rand()-rand();
    }
}

int main(int argc, char* argv[])
{
        int zhengshu=0,fushu=0,zero=0;
        int n[MAX];
        int i;
       
        for(i=0;i<MAX;i++){
                scanf("%d",n+i);
        }
        //getrand(n,MAX);
        //此处是用随机数测试
        for(i=0;i<MAX;i++){
                if(n>0)zhengshu++;
                else if(n==0)zero++;
                else fushu++;
                printf("%d ",n);
        }
        printf("负数的个数是%d",fushu);
        printf("整数的个数是%d",zhengshu);
        printf("零的个数是%d",zero);
        return 0;
}
1.实现下面类的默认构造函数和必要的复制控制成员。
class TreeNode{
public:
private:
        std::string value;
        int count;
        TreeNode *left;
        TreeNode *right;
};
class TreeNode{
public:
        TreeNode():count(0),left(0),right(0){}
        TreeNode(const TreeNode &org):value(org.value),count(org.count){
                if(org.left)left=new TreeNode(*org.left);
                else left=0;
                if(org.right)right=new TreeNode(*org.right);
                else right=0;
        }
        ~TreeNode(){
                if(left)delete left;
                if(right)delete right;
        }
private:
        std::string value;
        int count;
        TreeNode *left;
        TreeNode *right;
};
2.显式(explicit)与隐式构造函数区别,看下面原因。
class A{
public:
        A(){};
        A(int   x);
        A operator+(const A& a);
private:
        int data;
}
A::A(int x) {
        data=x;
}
A A::operator+(const A& a);{
        A a;      
        a=this-> data+a.data;
        return   a;
}
int main() {
        A  a(3);
        A  b(5);
        A  c;
        c=a+b;             //调用了operator+,可以正确的运行
        c=a+7;           //因为operator+要求的是两个A类型的形参,所以7会自动的被A(int   x)转换为一个A的对象,然后再与a相加, 而你如果在A(int   x)这个构造函数前加了explicit,则这个7到a的转换不会执行,c=a+7会报错
}
3.重载下面类的<<操作符,使得输出时把两个成员的值输出。
class X{
private:
        int *a;
        int b;
public:
        X(int *p,int i):a(p),b(i){}
};
答案:class X{
private:
        int *a;
        int b;
public:
        X(int *p,int i):a(p),b(i){}
        friend std::istream& operator>>(std::iostream& os,X &object){



};
        friend std::ostream& operator<<(std::ostream& os,const X &object){
                os<<*(object.a)<<" "<<object.b;
                return os;
        };
};
注意:此题我把IO操作符写成X的成员函数,并且在用cout<<X的输出时并没有错误。
1.指出下面初始化是否正确,为什么。
class Account{
private:

        std::string str;
        int b;
public:
        Account(const Account &s){str=s.str;b=s.b;}
        Account(std::string s="",int c=0):str(s),b(c){}
        void show(){
                std::cout<<str<<b;
        }

};
Account b("abc",5);
b.show();
Account c(b);
c.show();
答案:虽然两个变量是私有的,但也可以用复制构造函数传递私有成员。
2.找出下面初始化无法编译的原因。
vector<int> v=42;
答案:vector容器没有提供公有的复制构造函数。第二个初始化是复制初始化,创建v时,编译器先接受一个int型形

参的vector构造函数,创建一个临时的vector对象,然后用复制构造函数将v初始化这个临时对象的副本。
3.为什么复制构造函数必须是引用?
答案:因为如果以传值方式传递参数时,会导致调用复制构造函数,否则就会导致复制构造函数的无穷递归调用。
4.如何防止类的复制?
答案:在private中显示声明复制构造函数,并且不做定义(防止在友元类中使用)。
5.编写下面程序的赋值操作符,并给出析构函数。
class Noname{
Noname():pstring(new std::string),i(0),d(0){}
private:
        std::string *pstring;
        int i;
        double d;
}
答案:
Noname& Noname::operator=(const Noname &rhs){
pstring=new std::string;
*pstring=*(rhs.pstring);
i=rhs.i;
d=rhs.d;
return *this;
}
Noname::~Noname(){
delete pstring;
}
6.指出下面析构函数执行几次。
void fun(const Noname *trans,Noname accum){
Noname item1(*trans),item2(accum);
}
答案:3次,item1,item2,accum。
7.写出结果分析原因。
class X{
private:
        int *a;
        int b;
public:
        X(int *p,int i):a(p),b(i){}
        void show(){
                std::cout<<"the value of a is"<<a<<'/n'
                                <<"the value of *a is"<<*a<<'/n'
                                <<"the value of &b is"<<&b<<'/n'
                                <<"the value of b is"<<b<<std::endl;
        }

};
int obj=0;
X test1(&obj,42);
X test2(test1);
test1.show();
test2.show();
答案:由于是默认的赋值,指针赋值是简单的“test1.a=test2.a”,所以都指向同一个内存地址。但b则是两个独立

空间。
8.
1.找出结果不同的选项。
#include <iostream>
using namespace std;
index=10;
int main(){
class Screen{
public:
Screen():weigh(10),index(10){};
void get_result(int index){
cout<<weigh*index;//...................A
//cout<<weigh*this->index;.............B
//cout<<weigh*Screen::index;...........C
//cout<<weigh*index;...................D
}
private:
int weigh;
int index;
};
Screen my_screen;
my_screen.get_result(100);
return 0;
}
答案:A。
2.写出程序结果。
#include <iostream>
using namespace std;
class Screen{
public:
Screen(int n):index(n),weigh(index){};
int weigh;
int index;
};
Screen my_screen(100);
cout<<my_screen.weigh<<my_screen.index;
return 0;
}
答案:第一个值不确定,第二个值为100。
3.写出下面类的构造函数,要求给每个类成员赋予初值,并且可是使用默认构造函数(其中n赋值给weigh,weigh赋值给index,n赋值给high)。
class Screen{
public:
Screen(int n=0);
private:
int weigh;
const int index;
int &high;
};
答案:Screen::Screen(int n):weigh(n),index(weigh),high(n){}。此题无法在构造函数体中给他们赋值:Screen(int n){weigh=n;index=weigh;high=n;}没有默认构造函数类类型的成员,以及const或者引用类型的成员不管那种类型,都必须在初始化列表中进行初始化。
4.写出下面类的默认构造函数其中a,b,c默认为0。
class test{
public:
test(int e=0,int f=0,int g=0):a(e),b(f),c(g){}
int a,b,c;
};
test my_test(2,3);
cout<<my_test.a<<my_test.b<<my_test.c;
5.友元函数的使用。
class abc{
private:
int a;int b;int c;
public:
abc():a(0),b(1),c(2){}


abc& showabc(){
std::cout<<a<<std::endl;
std::cout<<b<<std::endl;
std::cout<<c<<std::endl;
return *this;
}


friend class change;
};

class change{
public:
int sum(abc &m){
return m.a+m.b+m.c;
}
};
abc my_a;
my_a.showabc().showabc();
change my_change;
cout<<my_change.sum(my_a);

6.找出static成员函数错误原因。
文件1:
class a{
public:
static double rate=6.5;
static const double rate2=6.6;
}
非const的static成员必须在类外初始化。
1.写输出值(8位的unsigned char)。
        unsigned char number=257;
        cout<<(int)number<<endl;
        cout<<number<<endl;
答案:1,(标准输出笑脸,文本输出前者)。
2.写出结果
        char *str1="absde";
        char str2[]="absde";
        char str3[8]={'a',};
        char str4[]={'a','b','c','d','e','f'};
        char ch=L'a';
        sizeof(str1)=?;strlen(str1)=?;//4,5
        sizeof(str2)=?;strlen(str2)=?;//6,5
        sizeof(str3)=?;strlen(str3)=?;//8,1(后者非确定)
        sizeof(str4)=?;strlen(str4)=?;//6,9(后者非确定)
        sizeof(ch)=?;
3.找出使用extern无法编译的选项
extern int i;int i;//A
extern int i=3;int i;//B
extern int i=3;extern int i;//C
4.由于const默认为文件局部变量(而非const默认为extern),那么如何在整个程序中访问const对象?
答案://file_1.c
extern const int count=fcn();
//file_2.c
extern const int counter;//uses count from file_1
如果const变量不是用常量表达式初始化,那么那就不应该出现在头文件中,而应该像上面一样。
5.找出使用引用错误的地方
int n=1024;
int &m=n;
int &m2;//must be initialized
int &m3=10;//initializer must be an object
const int n2=1024;
const int &k=n2;
int &k2=n2;//must be a const object

const int &k3=100;
const int &k4=k3+n;
int &k5=k3+n;//k5 should be a const object




1.列举string的构造器。
答案:        string str(4,'c');
        string str2(str);
        string str3("cccc");
        string str4;
2.找出下面初始化错误的选项。
string s1="hello";
string s2="world";
string s3=s1+",";............A
string s4="hello"+",";.......B
string s5="hello"+","+s2;....C
string s6="hello"+s2+",";....D
答案:B,C。
3.区分const iterator与const_iterator。
答案:const iterator必须在定义的时候初始化,并且不能改变iterator的值;
const_iterator可以改变自身的值,但不能改变其指向的数据的值。
4.下面的代码会出现什么问题,为什么?
bitset<32> bitvec("11111000000003333333333");
cout<<bitvec<<endl;
编译成功运行失败,由于电脑无法识别3333333。
5.

1.找出错误的初始化数组。
const int buf_size=512;
sz=get_size();
int max_files=20;
char buf[buf_size];............A
double salar[max_files];.......B
int scores[get_size()];........C
int vals[sz];..................D
答案:BCD。必须是确定的值,因为B中的max_files不是const可以更改其值,以及get_size()的结果都是非确定的。
2.下面指针初始化错误的。
int ival;
int zero=0;
const int c_ival=0;
int *pi=ival;..................A
pi=zero;.......................B
pi=c_ival;.....................C
pi=0;..........................D
答案:AB,给指针赋值必须是const对象或者是字面值0。
3.指向const对象的指针(const int *cpin;)允许给指针cpin赋值,但不能改变其所指对象的值。指出下面赋值错误所在。
const int n=5;
int *m=&n;//不可以把一个const对象赋值给已给非const指针,但可以把非const对象的地址赋值给指向const对象的指针。
int n=5;
const int *m=&n;
*m=6;//错无法改变所指向对象的值。
int *k=&n;
*k=6;
cout<<*m;//值为6
const指针(int *const cpin;)指针本身无法改变,只能指向一个对象。
4.写出程序运行结果。释放动态数组 delete[] str
string *str=new string[10];
int *in=new int[10]();
cout<<*(in+2);//应该初始化0,但结果不是。我编译结果是一个负值。可能是因为编译器的原因。
5.



1.把下面unsigned long m的第27位设置为1,再改为0。
答案:m|=1UL<<27;m&=~(1UL<<27)
2.输出程序结果。
vectot<int> ivec;
vector<int>::iterator iter=ivec.begin();
while(iter!=ivec.end())
count<<*iter++<<endl;
答案:10,9,8,7,6,5,4,3,2,1。虽然解释成*(iter++),但仍然先返回原值然后再递增。
3.用括号表示下列表达式的计算顺序。
!ptr==ptr->next;//(!ptr)==(ptr->next);
ch=buf[bp++]!='/n';//ch=(buf[bp++]!='/n');
ival!=jval<kval;//ival!=(jval<kval);
4.输出下面的值。
double d=97.0;
void *p=&d;
double *dp=static_cast<double *>(p);
此时dp的值为97.0
5.解释const_cast与static_cast。
答案:前者为了修改const和volatile属性,后者隐式转换数据类型。
class B{
  public:
  int m_iNum;
  };
  void foo(){
  const B b1;
  //b1.m_iNum = 100; //comile error
  B &b2 = const_cast<B&>(b1);
  b2. m_iNum = 200; //fine
  }
6.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值